Skip to content

Commit d71f45f

Browse files
Refactoring resticprofile commands
1 parent a8ff142 commit d71f45f

File tree

11 files changed

+251
-85
lines changed

11 files changed

+251
-85
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ ifeq ($(UNAME),Darwin)
3737
TMP_MOUNT=${TMP_MOUNT_DARWIN}
3838
endif
3939

40-
.PHONY: all test test-ci build build-mac build-linux build-windows build-all coverage clean test-docker build-docker ramdisk passphrase rest-server
40+
.PHONY: all test test-ci build build-mac build-linux build-windows build-all coverage clean test-docker build-docker ramdisk passphrase rest-server nightly
4141

4242
all: test build
4343

@@ -107,3 +107,7 @@ rest-server:
107107

108108
docker pull ${REST_IMAGE}
109109
docker run -d -p 8000:8000 -v ${REST_DATA}:/data --name ${REST_CONTAINER} --restart always -e "OPTIONS=${REST_OPTIONS}" ${REST_IMAGE}
110+
111+
nightly:
112+
go install github.com/goreleaser/goreleaser
113+
goreleaser --snapshot --skip-publish --rm-dist

commands.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}

constants/command.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package constants
22

3-
// Commands
3+
// Restic commands
44
const (
5-
CommandBackup = "backup"
6-
CommandCheck = "check"
7-
CommandForget = "forget"
8-
CommandInit = "init"
9-
CommandPrune = "prune"
10-
CommandSnapshots = "snapshots"
11-
CommandProfiles = "profiles"
12-
CommandSystemdUnit = "systemd-unit"
5+
CommandBackup = "backup"
6+
CommandCheck = "check"
7+
CommandForget = "forget"
8+
CommandInit = "init"
9+
CommandPrune = "prune"
10+
CommandSnapshots = "snapshots"
1311
)

flags.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ func loadFlags() (*pflag.FlagSet, commandLineFlags) {
3030
fmt.Println("\tresticprofile [resticprofile flags] [command] [restic flags]")
3131
fmt.Println("\nresticprofile flags:")
3232
flagset.PrintDefaults()
33+
fmt.Println("\nresticprofile own commands:")
34+
displayOwnCommands()
3335
fmt.Println("")
3436
}
3537

@@ -44,7 +46,8 @@ func loadFlags() (*pflag.FlagSet, commandLineFlags) {
4446
flagset.BoolVar(&flags.noAnsi, "no-ansi", false, "disable ansi control characters (disable console colouring)")
4547
flagset.StringVar(&flags.theme, "theme", constants.DefaultTheme, "console colouring theme (dark, light, none)")
4648
flagset.BoolVar(&flags.selfUpdate, "self-update", false, "auto update of resticprofile (does not update restic)")
47-
flagset.StringVar(&flags.saveConfigAs, "save-config-as", "", "save configuration to a new file (file extension to choose which format)")
49+
flagset.StringVar(&flags.saveConfigAs, "save-config-as", "", "save configuration to a new file (the file extension is used to decide the new format)")
50+
_ = flagset.MarkHidden("self-update")
4851
_ = flagset.MarkHidden("save-config-as")
4952

5053
// stop at the first non flag found; the rest will be sent to the restic command line

go.mod

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,30 @@ require (
77
github.com/blang/semver v3.5.1+incompatible
88
github.com/fatih/color v1.9.0
99
github.com/fsnotify/fsnotify v1.4.9 // indirect
10+
github.com/golang/protobuf v1.4.2 // indirect
11+
github.com/google/go-cmp v0.4.1 // indirect
12+
github.com/kr/text v0.2.0 // indirect
1013
github.com/mattn/go-colorable v0.1.6 // indirect
1114
github.com/mitchellh/mapstructure v1.2.2 // indirect
15+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
16+
github.com/onsi/ginkgo v1.12.0 // indirect
17+
github.com/onsi/gomega v1.9.0 // indirect
1218
github.com/pelletier/go-toml v1.6.0 // indirect
1319
github.com/rhysd/go-github-selfupdate v1.2.1
20+
github.com/smartystreets/assertions v1.0.0 // indirect
1421
github.com/spf13/afero v1.2.2 // indirect
1522
github.com/spf13/cast v1.3.1 // indirect
1623
github.com/spf13/jwalterweatherman v1.1.0 // indirect
1724
github.com/spf13/pflag v1.0.5
1825
github.com/spf13/viper v1.6.2
19-
github.com/stretchr/testify v1.5.1
20-
golang.org/x/sys v0.0.0-20200331124033-c3d80250170d
21-
golang.org/x/text v0.3.2 // indirect
26+
github.com/stretchr/testify v1.6.1
27+
github.com/ulikunitz/xz v0.5.7 // indirect
28+
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
29+
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
30+
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980
31+
google.golang.org/appengine v1.6.6 // indirect
32+
google.golang.org/protobuf v1.24.0 // indirect
33+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
2234
gopkg.in/ini.v1 v1.55.0 // indirect
23-
gopkg.in/yaml.v2 v2.2.8 // indirect
35+
gopkg.in/yaml.v2 v2.3.0 // indirect
2436
)

0 commit comments

Comments
 (0)