File: commands.go

package info (click to toggle)
pat 0.19.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,296 kB
  • sloc: javascript: 3,891; sh: 124; makefile: 11
file content (190 lines) | stat: -rw-r--r-- 5,559 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package cli

import (
	"context"
	"log"

	"github.com/la5nta/pat/app"
)

var Commands = []app.Command{
	{
		Str:        "init",
		Desc:       "Initial configuration setup.",
		HandleFunc: InitHandle,
		Usage:      "Interactive basic setup with Winlink account verification.",
	},
	{
		Str:        "configure",
		Desc:       "Open configuration file for editing.",
		HandleFunc: ConfigureHandle,
	},
	{
		Str:        "connect",
		Desc:       "Connect to a remote station.",
		HandleFunc: ConnectHandle,
		Usage:      UsageConnect,
		Example:    ExampleConnect,
		MayConnect: true,
	},
	{
		Str:   "interactive",
		Desc:  "Run interactive mode.",
		Usage: "[options]",
		Options: map[string]string{
			"--http, -h": "Start http server for web UI in the background.",
		},
		HandleFunc: InteractiveHandle,
		MayConnect: true,
		LongLived:  true,
	},
	{
		Str:   "http",
		Desc:  "Run http server for web UI.",
		Usage: "[options]",
		Options: map[string]string{
			"--addr, -a": "Listen address. Default is :8080.",
		},
		HandleFunc: HTTPHandle,
		MayConnect: true,
		LongLived:  true,
	},
	{
		Str:  "compose",
		Desc: "Compose a new message.",
		Usage: "[options]\n" +
			"\tIf no options are passed, composes interactively.\n" +
			"\tIf options are passed, reads message from stdin similar to mail(1).",
		Options: map[string]string{
			"--from, -r":        "Address to send from. Default is your call from config or --mycall, but can be specified to use tactical addresses.",
			"--forward":         "Forward given message (full path or mid)",
			"--in-reply-to":     "Compose in reply to given message (full path or mid)",
			"--reply-all":       "Reply to all (only applicable in combination with --in-reply-to)",
			"--template":        "Compose using template file. Uses the --forms directory as root for relative paths.",
			"--subject, -s":     "Subject",
			"--attachment , -a": "Attachment path (may be repeated)",
			"--cc, -c":          "CC Address(es) (may be repeated)",
			"--p2p-only":        "Send over peer to peer links only (avoid CMS)",
			"":                  "Recipient address (may be repeated)",
		},
		HandleFunc: ComposeMessage,
	},
	{
		Str:        "read",
		Desc:       "Read messages.",
		HandleFunc: ReadHandle,
	},
	{
		Str:     "composeform",
		Aliases: []string{"formPath"},
		Desc:    "Post form-based report. (DEPRECATED)",
		Usage:   "[options]",
		Options: map[string]string{
			"--template": "path to the form template file. Uses the --forms directory as root. Defaults to 'ICS USA Forms/ICS213.txt'",
		},
		HandleFunc: func(ctx context.Context, app *app.App, args []string) {
			log.Println("DEPRECATED: Use `compose --template` instead")
			if len(args) == 0 || args[0] == "" {
				args = []string{"ICS USA Forms/ICS213.txt"}
			}
			ComposeMessage(ctx, app, append([]string{"--template"}, args...))
		},
	},
	{
		Str:     "position",
		Aliases: []string{"pos"},
		Desc:    "Post a position report (GPSd or manual entry).",
		Usage:   "[options]",
		Options: map[string]string{
			"--latlon":      "latitude,longitude in decimal degrees for manual entry. Will use GPSd if this is empty.",
			"--comment, -c": "Comment to be included in the position report.",
		},
		Example:    ExamplePosition,
		HandleFunc: PositionHandle,
	},
	{
		Str:        "extract",
		Desc:       "Extract attachments from a message file.",
		Usage:      "[full path or mid]",
		HandleFunc: ExtractMessageHandle,
	},
	{
		Str:   "rmslist",
		Desc:  "Print/search in list of RMS nodes.",
		Usage: "[options] [search term]",
		Options: map[string]string{
			"--mode, -m":              "Mode filter.",
			"--band, -b":              "Band filter (e.g. '80m').",
			"--force-download, -d":    "Force download of latest list from winlink.org.",
			"--sort-distance, -s":     "Sort by distance",
			"--sort-link-quality, -q": "Sort by predicted link quality (requires VOACAP)",
		},
		HandleFunc: RMSListHandle,
	},
	{
		Str:  "updateforms",
		Desc: "Download the latest form templates. (DEPRECATED)",
		HandleFunc: func(ctx context.Context, a *app.App, args []string) {
			log.Println("DEPRECATED: Use `templates update` instead")
			TemplatesHandle(ctx, a, []string{"update"})
		},
	},
	{
		Str:        "templates",
		Desc:       "Manage message templates and HTML forms.",
		Usage:      TemplatesUsage,
		Example:    TemplatesExample,
		HandleFunc: TemplatesHandle,
	},
	{
		Str:        "account",
		Desc:       "Get and set Winlink.org account settings.",
		Usage:      AccountUsage,
		Example:    AccountExample,
		HandleFunc: AccountHandle,
	},
	{
		Str:        "mps",
		Desc:       "Manage message pickup stations.",
		Usage:      MPSUsage,
		Example:    MPSExample,
		HandleFunc: MPSHandle,
	},
	{
		Str:   "version",
		Desc:  "Print the application version.",
		Usage: "[options]",
		Options: map[string]string{
			"--check, -c": "Check if a new version is available",
		},
		HandleFunc: VersionHandle,
	},
	{
		Str:        "env",
		Desc:       "List environment variables.",
		HandleFunc: EnvHandle,
	},
	{
		Str:  "help",
		Desc: "Print detailed help for a given command.",
		// Avoid initialization loop by invoking helpHandler in main
	},
}

func FindCommand(args []string) (cmd app.Command, pre, post []string, err error) {
	cmdMap := make(map[string]app.Command, len(Commands))
	for _, c := range Commands {
		cmdMap[c.Str] = c
		for _, alias := range c.Aliases {
			cmdMap[alias] = c
		}
	}

	for i, arg := range args {
		if cmd, ok := cmdMap[arg]; ok {
			return cmd, args[1:i], args[i+1:], nil
		}
	}
	err = app.ErrNoCmd
	return
}