diff --git a/cmd/radioctl/radioctl.go b/cmd/radioctl/radioctl.go index 61741c41d1bed9d7576bac84b73263c81147a905..28521f14098abfc5eda448fc11b4ff65c52d2125 100644 --- a/cmd/radioctl/radioctl.go +++ b/cmd/radioctl/radioctl.go @@ -587,6 +587,73 @@ func (cmd *showMountCommand) Run(args []string) { printMount(m) } +// Backup mount configuration. +type backupCommand struct { + BaseCommand +} + +func newBackupCommand() *backupCommand { + return &backupCommand{ + BaseCommand{ + UsageLine: "backup", + Short: "Backup mount configuration", + Long: ` +Dump the autoradio configuration to stdout, in a format that is +understood by the "restore" command. +`, + }, + } +} + +func (cmd *backupCommand) Run(args []string) { + if len(args) != 0 { + log.Fatal("Too many arguments") + } + + mounts, err := getClient().ListMounts() + if err != nil { + log.Fatalf("ERROR: %v", err) + } + if err := json.NewEncoder(os.Stdout).Encode(mounts); err != nil { + log.Fatalf("ERROR: %v", err) + } +} + +// Restore mount configuration. +type restoreCommand struct { + BaseCommand +} + +func newRestoreCommand() *restoreCommand { + return &restoreCommand{ + BaseCommand{ + UsageLine: "restore", + Short: "Restore mount configuration", + Long: ` +Read a configuration dump from standard input and restore it. +`, + }, + } +} + +func (cmd *restoreCommand) Run(args []string) { + if len(args) != 0 { + log.Fatal("Too many arguments") + } + + var mounts []*autoradio.Mount + if err := json.NewDecoder(os.Stdin).Decode(&mounts); err != nil { + log.Fatalf("ERROR: %v", err) + } + + client := getClient() + for _, m := range mounts { + if err := client.SetMount(m); err != nil { + log.Printf("ERROR: creating mount %s: %v", m.Name, err) + } + } +} + var cmdr = &commander.Command{ UsageLine: "radioctl <command> [<args>...]", Short: "Manage `autoradio' configuration", @@ -611,6 +678,8 @@ func init() { addCommand(newDeleteMountCommand()) addCommand(newListMountsCommand()) addCommand(newShowMountCommand()) + addCommand(newBackupCommand()) + addCommand(newRestoreCommand()) flag.Usage = usage }