Skip to content

Commit

Permalink
Refactoring resticprofile commands
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Jun 24, 2020
1 parent a8ff142 commit d71f45f
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 85 deletions.
6 changes: 5 additions & 1 deletion Makefile
Expand Up @@ -37,7 +37,7 @@ ifeq ($(UNAME),Darwin)
TMP_MOUNT=${TMP_MOUNT_DARWIN}
endif

.PHONY: all test test-ci build build-mac build-linux build-windows build-all coverage clean test-docker build-docker ramdisk passphrase rest-server
.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

all: test build

Expand Down Expand Up @@ -107,3 +107,7 @@ rest-server:

docker pull ${REST_IMAGE}
docker run -d -p 8000:8000 -v ${REST_DATA}:/data --name ${REST_CONTAINER} --restart always -e "OPTIONS=${REST_OPTIONS}" ${REST_IMAGE}

nightly:
go install github.com/goreleaser/goreleaser
goreleaser --snapshot --skip-publish --rm-dist
118 changes: 118 additions & 0 deletions commands.go
@@ -0,0 +1,118 @@
package main

import (
"fmt"
"os"
"strings"
"text/tabwriter"

"github.com/creativeprojects/resticprofile/config"
"github.com/creativeprojects/resticprofile/systemd"
)

type ownCommand struct {
name string
description string
action func(commandLineFlags, []string) error
}

var (
ownCommands = []ownCommand{
{
name: "profiles",
description: "display profile names from the configuration file",
action: displayProfilesCommand,
},
{
name: "self-update",
description: "update resticprofile to latest version (does not update restic)",
action: selfUpdate,
},
{
name: "systemd-unit",
description: "create a user systemd timer",
action: createSystemdTimer,
},
}
)

func displayOwnCommands() {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
for _, command := range ownCommands {
_, _ = fmt.Fprintf(w, "\t%s\t%s\n", command.name, command.description)
}
_ = w.Flush()
}

func isOwnCommand(command string) bool {
for _, commandDef := range ownCommands {
if commandDef.name == command {
return true
}
}
return false
}

func runOwnCommand(command string, flags commandLineFlags, args []string) error {
for _, commandDef := range ownCommands {
if commandDef.name == command {
return commandDef.action(flags, args)
}
}
return fmt.Errorf("command not found: %v", command)
}

func displayProfilesCommand(commandLineFlags, []string) error {
displayProfiles()
displayGroups()
return nil
}

func displayProfiles() {
profileSections := config.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, sections := range profileSections {
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() {
groups := config.ProfileGroups()
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(flags commandLineFlags, args []string) error {
err := confirmAndSelfUpdate(flags.verbose)
if err != nil {
return err
}
return nil
}

func createSystemdTimer(flags commandLineFlags, args []string) error {
if len(args) != 1 {
return fmt.Errorf("OnCalendar argument required")
}
systemd.Generate(flags.name, args[0])
return nil
}
16 changes: 7 additions & 9 deletions constants/command.go
@@ -1,13 +1,11 @@
package constants

// Commands
// Restic commands
const (
CommandBackup = "backup"
CommandCheck = "check"
CommandForget = "forget"
CommandInit = "init"
CommandPrune = "prune"
CommandSnapshots = "snapshots"
CommandProfiles = "profiles"
CommandSystemdUnit = "systemd-unit"
CommandBackup = "backup"
CommandCheck = "check"
CommandForget = "forget"
CommandInit = "init"
CommandPrune = "prune"
CommandSnapshots = "snapshots"
)
5 changes: 4 additions & 1 deletion flags.go
Expand Up @@ -30,6 +30,8 @@ func loadFlags() (*pflag.FlagSet, commandLineFlags) {
fmt.Println("\tresticprofile [resticprofile flags] [command] [restic flags]")
fmt.Println("\nresticprofile flags:")
flagset.PrintDefaults()
fmt.Println("\nresticprofile own commands:")
displayOwnCommands()
fmt.Println("")
}

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

// stop at the first non flag found; the rest will be sent to the restic command line
Expand Down
20 changes: 16 additions & 4 deletions go.mod
Expand Up @@ -7,18 +7,30 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/fatih/color v1.9.0
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/google/go-cmp v0.4.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mitchellh/mapstructure v1.2.2 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/onsi/ginkgo v1.12.0 // indirect
github.com/onsi/gomega v1.9.0 // indirect
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/rhysd/go-github-selfupdate v1.2.1
github.com/smartystreets/assertions v1.0.0 // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.6.2
github.com/stretchr/testify v1.5.1
golang.org/x/sys v0.0.0-20200331124033-c3d80250170d
golang.org/x/text v0.3.2 // indirect
github.com/stretchr/testify v1.6.1
github.com/ulikunitz/xz v0.5.7 // indirect
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.24.0 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/ini.v1 v1.55.0 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)

0 comments on commit d71f45f

Please sign in to comment.