|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "text/tabwriter" |
| 8 | + |
| 9 | + "github.com/creativeprojects/resticprofile/config" |
| 10 | + "github.com/creativeprojects/resticprofile/systemd" |
| 11 | +) |
| 12 | + |
| 13 | +type ownCommand struct { |
| 14 | + name string |
| 15 | + description string |
| 16 | + action func(commandLineFlags, []string) error |
| 17 | +} |
| 18 | + |
| 19 | +var ( |
| 20 | + ownCommands = []ownCommand{ |
| 21 | + { |
| 22 | + name: "profiles", |
| 23 | + description: "display profile names from the configuration file", |
| 24 | + action: displayProfilesCommand, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "self-update", |
| 28 | + description: "update resticprofile to latest version (does not update restic)", |
| 29 | + action: selfUpdate, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "systemd-unit", |
| 33 | + description: "create a user systemd timer", |
| 34 | + action: createSystemdTimer, |
| 35 | + }, |
| 36 | + } |
| 37 | +) |
| 38 | + |
| 39 | +func displayOwnCommands() { |
| 40 | + w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) |
| 41 | + for _, command := range ownCommands { |
| 42 | + _, _ = fmt.Fprintf(w, "\t%s\t%s\n", command.name, command.description) |
| 43 | + } |
| 44 | + _ = w.Flush() |
| 45 | +} |
| 46 | + |
| 47 | +func isOwnCommand(command string) bool { |
| 48 | + for _, commandDef := range ownCommands { |
| 49 | + if commandDef.name == command { |
| 50 | + return true |
| 51 | + } |
| 52 | + } |
| 53 | + return false |
| 54 | +} |
| 55 | + |
| 56 | +func runOwnCommand(command string, flags commandLineFlags, args []string) error { |
| 57 | + for _, commandDef := range ownCommands { |
| 58 | + if commandDef.name == command { |
| 59 | + return commandDef.action(flags, args) |
| 60 | + } |
| 61 | + } |
| 62 | + return fmt.Errorf("command not found: %v", command) |
| 63 | +} |
| 64 | + |
| 65 | +func displayProfilesCommand(commandLineFlags, []string) error { |
| 66 | + displayProfiles() |
| 67 | + displayGroups() |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +func displayProfiles() { |
| 72 | + profileSections := config.ProfileSections() |
| 73 | + if profileSections == nil || len(profileSections) == 0 { |
| 74 | + fmt.Println("\nThere's no available profile in the configuration") |
| 75 | + } else { |
| 76 | + fmt.Println("\nProfiles available:") |
| 77 | + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) |
| 78 | + for name, sections := range profileSections { |
| 79 | + if sections == nil || len(sections) == 0 { |
| 80 | + _, _ = fmt.Fprintf(w, "\t%s:\t(n/a)\n", name) |
| 81 | + } else { |
| 82 | + _, _ = fmt.Fprintf(w, "\t%s:\t(%s)\n", name, strings.Join(sections, ", ")) |
| 83 | + } |
| 84 | + } |
| 85 | + _ = w.Flush() |
| 86 | + } |
| 87 | + fmt.Println("") |
| 88 | +} |
| 89 | + |
| 90 | +func displayGroups() { |
| 91 | + groups := config.ProfileGroups() |
| 92 | + if groups == nil || len(groups) == 0 { |
| 93 | + return |
| 94 | + } |
| 95 | + fmt.Println("Groups available:") |
| 96 | + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) |
| 97 | + for name, groupList := range groups { |
| 98 | + _, _ = fmt.Fprintf(w, "\t%s:\t%s\n", name, strings.Join(groupList, ", ")) |
| 99 | + } |
| 100 | + _ = w.Flush() |
| 101 | + fmt.Println("") |
| 102 | +} |
| 103 | + |
| 104 | +func selfUpdate(flags commandLineFlags, args []string) error { |
| 105 | + err := confirmAndSelfUpdate(flags.verbose) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func createSystemdTimer(flags commandLineFlags, args []string) error { |
| 113 | + if len(args) != 1 { |
| 114 | + return fmt.Errorf("OnCalendar argument required") |
| 115 | + } |
| 116 | + systemd.Generate(flags.name, args[0]) |
| 117 | + return nil |
| 118 | +} |
0 commit comments