radioctl.go 6.54 KB
Newer Older
1
2
3
4
5
6
7
package main

import (
	"flag"
	"fmt"
	"hash/crc32"
	"log"
8
	"net/url"
ale's avatar
ale committed
9
	"os"
10
11
	"strings"

ale's avatar
ale committed
12
	"git.autistici.org/ale/autoradio"
ale's avatar
ale committed
13
14
	"github.com/gonuts/commander"
	gonutsflag "github.com/gonuts/flag"
15
16
)

ale's avatar
ale committed
17
18
type HasAddFlags interface {
	AddFlags(*gonutsflag.FlagSet)
19
20
}

ale's avatar
ale committed
21
22
23
type CommandInterface interface {
	Run([]string)
	Command() *commander.Command
24
25
}

ale's avatar
ale committed
26
27
28
29
type BaseCommand struct {
	UsageLine string
	Short     string
	Long      string
30
31
}

ale's avatar
ale committed
32
33
34
35
36
func (b *BaseCommand) Command() *commander.Command {
	return &commander.Command{
		UsageLine: b.UsageLine,
		Short:     b.Short,
		Long:      b.Long,
37
38
39
	}
}

ale's avatar
ale committed
40
func (b *BaseCommand) Run(args []string) {
41
42
}

ale's avatar
ale committed
43
44
45
func getClient() *autoradio.RadioAPI {
	etc := autoradio.NewEtcdClient()
	return autoradio.NewRadioAPI(etc)
46
47
}

ale's avatar
ale committed
48
49
func generateUsername(path string) string {
	return fmt.Sprintf("source%d", crc32.ChecksumIEEE([]byte(path)))
50
51
}

ale's avatar
ale committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
func setRelay(m *autoradio.Mount, relayUrl string) {
	if relayUrl == "" {
		// Randomly generate source credentials.
		m.Username = generateUsername(m.Name)
		m.Password = autoradio.GeneratePassword()
	} else {
		// Validate the given relay URL.
		u, err := url.Parse(relayUrl)
		if err != nil {
			log.Fatal(err)
		}
		m.RelayUrl = u.String()
		m.Username = ""
		m.Password = ""
66
	}
ale's avatar
ale committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
}

// Edit a mountpoint.
type editMountCommand struct {
	BaseCommand
	relay    string
	fallback string
}

var UNSET = "UNSET"

func newEditMountCommand() *editMountCommand {
	return &editMountCommand{
		BaseCommand: BaseCommand{
			UsageLine: "edit-mount <path>",
			Short:     "Edit an existing mountpoint",
			Long: `
Modify parameters of the specified mountpoint, such as the relay
and the fallback URL. If the relay option is set, the mountpoint
will not accept source connections anymore. To revert to the
default, non-relay behavior, set the relay to the empty string
(with --relay="").
`,
		},
91
92
93
	}
}

ale's avatar
ale committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
func (cmd *editMountCommand) AddFlags(f *gonutsflag.FlagSet) {
	// Note that we use special values to figure out whether a
	// flag has been specified or not. There might be better way
	// to do this.
	f.StringVar(&cmd.relay, "relay", UNSET, "Upstream URL to relay")
	f.StringVar(&cmd.fallback, "fallback", UNSET, "Fallback stream URL")
}

func (cmd *editMountCommand) Run(args []string) {
	if len(args) != 1 {
		log.Fatal("Wrong number of arguments")
	}

	client := getClient()
	mount, err := client.GetMount(args[0])
109
110
111
112
113
114
	if err != nil {
		log.Fatal(err)
	}
	if mount == nil {
		log.Fatal("Mount not found")
	}
ale's avatar
ale committed
115
116
117
118
119
120
121
122
123
124
125
126

	if cmd.fallback != UNSET {
		mount.Fallback = cmd.fallback
	}
	if cmd.relay != UNSET {
		setRelay(mount, cmd.relay)
	}

	if err := client.SetMount(mount); err != nil {
		log.Fatal(err)
	}

127
128
129
	fmt.Printf("%+v\n", mount)
}

ale's avatar
ale committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Create a new mountpoint.
type createMountCommand struct {
	BaseCommand
	relay    string
	fallback string
}

func newCreateMountCommand() *createMountCommand {
	return &createMountCommand{
		BaseCommand: BaseCommand{
			UsageLine: "create-mount <path>",
			Short:     "Create a new mountpoint",
			Long: `
Create a new mountpoint at the specified PATH (which will become the
absolute URL of the stream).
`,
		},
	}
148
149
}

ale's avatar
ale committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
func (cmd *createMountCommand) AddFlags(f *gonutsflag.FlagSet) {
	f.StringVar(&cmd.relay, "relay", "", "Upstream URL to relay")
	f.StringVar(&cmd.fallback, "fallback", "", "Fallback stream URL")
}

func (cmd *createMountCommand) Run(args []string) {
	if len(args) != 1 {
		log.Fatal("Wrong number of arguments")
	}
	fmt.Printf("args: %v\n", args)

	path := args[0]
	if !strings.HasPrefix(path, "/") {
		path = "/" + path
164
165
166
167
	}

	// Check if the mount already exists.
	client := getClient()
168
	if oldm, _ := client.GetMount(path); oldm != nil {
169
170
171
		log.Fatal("A mount with that name already exists!")
	}

172
	// Create the new mount and set the relevant fields (depending
ale's avatar
ale committed
173
	// on the options passed to the command).
ale's avatar
ale committed
174
	m := &autoradio.Mount{Name: path}
ale's avatar
ale committed
175
176
177
	setRelay(m, cmd.relay)
	if cmd.fallback != "" {
		m.Fallback = cmd.fallback
178
179
180
181
182
183
184
185
186
	}

	if err := client.SetMount(m); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%+v\n", m)
}

ale's avatar
ale committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Delete an existing mountpoint.
type deleteMountCommand struct {
	BaseCommand
}

func newDeleteMountCommand() *deleteMountCommand {
	return &deleteMountCommand{
		BaseCommand{
			UsageLine: "delete-mount <path>",
			Short:     "Delete a mountpoint",
			Long: `
Delete the specified mountpoint.
`,
		},
	}
}

func (cmd *deleteMountCommand) Run(args []string) {
	if len(args) != 1 {
		log.Fatal("Wrong number of arguments")
	}
	if err := getClient().DelMount(args[0]); err != nil {
		log.Fatal(err)
	}
	log.Printf("mountpoint %s removed", args[0])
}

// List known mountpoints.
type listMountsCommand struct {
	BaseCommand
}

func newListMountsCommand() *listMountsCommand {
	return &listMountsCommand{
		BaseCommand{
			UsageLine: "list-mounts",
			Short:     "List all configured mountpoints",
			Long: `
Outputs a list of all the currently configured mountpoints.
`,
		},
	}
}

func (cmd *listMountsCommand) Run(args []string) {
	if len(args) != 0 {
		log.Fatal("Too many arguments")
	}

	mounts, err := getClient().ListMounts()
	if err != nil {
		log.Fatal(err)
	}
	for _, m := range mounts {
		fmt.Println(m.Name)
	}
}

// Show mountpoint information.
type showMountCommand struct {
	BaseCommand
}

func newShowMountCommand() *showMountCommand {
	return &showMountCommand{
		BaseCommand{
			UsageLine: "show-mount <path>",
			Short:     "Show mountpoint information",
			Long: `
Print information about the specified mountpoint (including
the source credentials).
`,
		},
	}
}

func (cmd *showMountCommand) Run(args []string) {
	if len(args) != 1 {
		log.Fatal("Wrong nubmer of arguments")
	}
	mount, err := getClient().GetMount(args[0])
	if err != nil {
		log.Fatal(err)
	}
	if mount == nil {
		log.Fatal("Mount not found")
	}
	fmt.Printf("%+v\n", mount)
}

var cmdr = &commander.Command{
	UsageLine: "radioctl <command> [<args>...]",
	Short:     "Manage `autoradio' configuration",
280
281
}

ale's avatar
ale committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
func addCommand(c CommandInterface) {
	cmd := c.Command()
	cmd.Run = func(unused *commander.Command, args []string) error {
		c.Run(args)
		return nil
	}
	if afc, ok := c.(HasAddFlags); ok {
		afc.AddFlags(&cmd.Flag)
	}
	cmdr.Subcommands = append(cmdr.Subcommands, cmd)
}

func init() {
	addCommand(newCreateMountCommand())
	addCommand(newEditMountCommand())
	addCommand(newDeleteMountCommand())
	addCommand(newListMountsCommand())
	addCommand(newShowMountCommand())
	flag.Usage = usage
301
302
}

ale's avatar
ale committed
303
304
305
306
func usage() {
	fmt.Fprintf(os.Stderr, "Usage: %s\n\nGlobal options:\n", cmdr.UsageLine)
	flag.PrintDefaults()
	fmt.Fprintln(os.Stderr, "\nType \"radioctl help\" for more documentation.\n")
307
308
309
}

func main() {
ale's avatar
ale committed
310
	flag.Parse()
311
	log.SetFlags(0)
ale's avatar
ale committed
312
313
314
	if err := cmdr.Dispatch(flag.Args()); err != nil {
		log.Fatal(err)
	}
315
}