-
Notifications
You must be signed in to change notification settings - Fork 30
/
commands.go
220 lines (201 loc) · 5.83 KB
/
commands.go
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
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
package main
import (
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
"github.com/creativeprojects/resticprofile/clog"
"github.com/creativeprojects/resticprofile/config"
"github.com/creativeprojects/resticprofile/constants"
"github.com/creativeprojects/resticprofile/schedule"
"github.com/creativeprojects/resticprofile/systemd"
)
type ownCommand struct {
name string
description string
action func(*config.Config, commandLineFlags, []string) error
needConfiguration bool // true if the action needs a configuration file loaded
hide bool
}
var (
ownCommands = []ownCommand{
{
name: "profiles",
description: "display profile names from the configuration file",
action: displayProfilesCommand,
needConfiguration: true,
},
{
name: "self-update",
description: "update resticprofile to latest version (does not update restic)",
action: selfUpdate,
needConfiguration: false,
},
{
name: "systemd-unit",
description: "create a user systemd timer",
action: createSystemdTimer,
needConfiguration: true,
},
{
name: "show",
description: "show all the details of the current profile",
action: showProfile,
needConfiguration: true,
hide: false,
},
{
name: "panic",
description: "(debug only) simulates a panic",
action: panicCommand,
needConfiguration: false,
hide: true,
},
{
name: "schedule",
description: "schedule a backup",
action: createSchedule,
needConfiguration: true,
hide: false,
},
{
name: "unschedule",
description: "remove a scheduled backup",
action: removeSchedule,
needConfiguration: true,
hide: false,
},
}
)
func displayOwnCommands() {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
for _, command := range ownCommands {
if command.hide {
continue
}
_, _ = fmt.Fprintf(w, "\t%s\t%s\n", command.name, command.description)
}
_ = w.Flush()
}
func isOwnCommand(command string, configurationLoaded bool) bool {
for _, commandDef := range ownCommands {
if commandDef.name == command && commandDef.needConfiguration == configurationLoaded {
return true
}
}
return false
}
func runOwnCommand(configuration *config.Config, command string, flags commandLineFlags, args []string) error {
for _, commandDef := range ownCommands {
if commandDef.name == command {
return commandDef.action(configuration, flags, args)
}
}
return fmt.Errorf("command not found: %v", command)
}
func displayProfilesCommand(configuration *config.Config, _ commandLineFlags, _ []string) error {
displayProfiles(configuration)
displayGroups(configuration)
return nil
}
func displayProfiles(configuration *config.Config) {
profileSections := configuration.GetProfileSections()
keys := sortedMapKeys(profileSections)
if profileSections == nil || len(profileSections) == 0 {
fmt.Println("\nThere's no available profile in the configuration")
} else {
fmt.Println("\nProfiles available:")
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
for _, name := range keys {
sections := profileSections[name]
sort.Strings(sections)
if sections == nil || len(sections) == 0 {
_, _ = fmt.Fprintf(w, "\t%s:\t(n/a)\n", name)
} else {
_, _ = fmt.Fprintf(w, "\t%s:\t(%s)\n", name, strings.Join(sections, ", "))
}
}
_ = w.Flush()
}
fmt.Println("")
}
func displayGroups(configuration *config.Config) {
groups := configuration.GetProfileGroups()
if groups == nil || len(groups) == 0 {
return
}
fmt.Println("Groups available:")
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
for name, groupList := range groups {
_, _ = fmt.Fprintf(w, "\t%s:\t%s\n", name, strings.Join(groupList, ", "))
}
_ = w.Flush()
fmt.Println("")
}
func selfUpdate(_ *config.Config, flags commandLineFlags, args []string) error {
err := confirmAndSelfUpdate(flags.verbose)
if err != nil {
return err
}
return nil
}
func createSystemdTimer(_ *config.Config, flags commandLineFlags, args []string) error {
if len(args) != 1 {
return fmt.Errorf("OnCalendar argument required")
}
systemd.Generate(flags.name, args[0])
return nil
}
func panicCommand(_ *config.Config, _ commandLineFlags, _ []string) error {
panic("you asked for it")
}
func sortedMapKeys(data map[string][]string) []string {
keys := make([]string, 0, len(data))
for key := range data {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
func showProfile(c *config.Config, flags commandLineFlags, args []string) error {
// Show global section first
global, err := c.GetGlobalSection()
if err != nil {
return fmt.Errorf("cannot show global: %w", err)
}
fmt.Printf("\n%s:\n", constants.SectionConfigurationGlobal)
config.ShowStruct(os.Stdout, global)
// Then show profile
profile, err := c.GetProfile(flags.name)
if err != nil {
return fmt.Errorf("cannot show profile '%s': %w", flags.name, err)
}
if profile == nil {
return fmt.Errorf("profile '%s' not found", flags.name)
}
fmt.Printf("\n%s:\n", flags.name)
config.ShowStruct(os.Stdout, profile)
return nil
}
func createSchedule(c *config.Config, flags commandLineFlags, args []string) error {
profile, err := c.GetProfile(flags.name)
if err != nil {
return fmt.Errorf("cannot load profile '%s': %w", flags.name, err)
}
if profile == nil {
return fmt.Errorf("profile '%s' not found", flags.name)
}
err = schedule.CreateJob(flags.config, profile)
if err == nil {
clog.Info("scheduled job created")
}
return err
}
func removeSchedule(c *config.Config, flags commandLineFlags, args []string) error {
err := schedule.RemoveJob(flags.name)
if err == nil {
clog.Info("scheduled job removed")
}
return err
}