Skip to content

Commit

Permalink
refactor: refactored argparsing, backup
Browse files Browse the repository at this point in the history
  • Loading branch information
pspiagicw committed Apr 12, 2024
1 parent 3a3d8d5 commit df1ec80
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 32 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ var VERSION string = "unversioned"

func main() {
opts := argparse.ParseArguments(VERSION)
handler.HandleArgs(opts)
handler.Handle(opts)
}
30 changes: 19 additions & 11 deletions pkg/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,43 @@ import (
)

func parseBackupOpts(opts *argparse.Opts) {

flag := flag.NewFlagSet("groom backup", flag.ExitOnError)

flag.BoolVar(&opts.DryRun, "dry-run", false, "Dry run the backup")
flag.BoolVar(&opts.Ignore, "ignore", false, "Ignore the backup")

flag.Usage = help.HelpBackup
flag.Parse(opts.Args)

opts.Args = flag.Args()
}

func PerformBackup(opts *argparse.Opts) {
func Backup(opts *argparse.Opts) {

parseBackupOpts(opts)
configFile := getConfig(opts)

targets := opts.Args

configFile := preBackup(opts)
executeBackup(configFile, opts)
postBackup(configFile, opts)
}

if len(targets) != 0 {
func executeBackup(configFile *config.Config, opts *argparse.Opts) {
if len(opts.Args) != 0 {
backupSelective(configFile, opts)
} else {
if opts.Ignore {
goreland.LogFatal("Can't ignore all rules. Please specify the rules to ignore.")
}
backupAll(configFile, opts)

}
}

func postBackup(configFile *config.Config, opts *argparse.Opts) {
goreland.LogInfo("Backup complete!")
runAfterBackup(configFile, opts)
goreland.LogSuccess("Backup successful!")
}

func preBackup(opts *argparse.Opts) *config.Config {
configFile := config.GetConfig(opts)
func getConfig(opts *argparse.Opts) *config.Config {
configFile := config.NewConfig(opts)
confirmBackup()
ensureStorePath(configFile)
return configFile
Expand Down Expand Up @@ -128,6 +132,10 @@ func getPath(configFile *config.Config, rule *config.BackupRule) (string, string
return src, dest
}
func backupAll(configFile *config.Config, opt *argparse.Opts) {
if opt.Ignore {
goreland.LogFatal("Can't ignore all rules")
}

for name, _ := range configFile.Rules {
executeRule(configFile, name, opt)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func assertConfigFile() {
goreland.LogFatal("Run dotback help config for more information.")
}
}
func GetConfig(opts *argparse.Opts) *Config {
func NewConfig(opts *argparse.Opts) *Config {

path := getConfigPath()

Expand Down
2 changes: 1 addition & 1 deletion pkg/config/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func parseConfigArgs(opts *argparse.Opts) {
func PrintConfig(opts *argparse.Opts) {
parseConfigArgs(opts)
fmt.Println("DOTBACK CONFIG")
config := GetConfig(opts)
config := NewConfig(opts)
fmt.Printf("Location dir: %s\n", config.StoreDir)
printAfterBackup(config)
printRules(config)
Expand Down
36 changes: 19 additions & 17 deletions pkg/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,38 @@ import (
"github.com/pspiagicw/goreland"
)

func HandleArgs(opts *argparse.Opts) {
var handlers map[string]func(*argparse.Opts) = map[string]func(*argparse.Opts){
"version": func(opts *argparse.Opts) {
help.PrintVersion(opts.Version)
},
"backup": backup.Backup,
"config": config.PrintConfig,
"help": func(opts *argparse.Opts) {
help.Handle(opts.Args, opts.Version)
},
}

func Handle(opts *argparse.Opts) {

checkExampleConfig(opts)
checkArgLen(opts)
execCmd(opts)
}

func execCmd(opts *argparse.Opts) {
cmd := opts.Args[0]

handlers := map[string]func(*argparse.Opts){
"version": func(*argparse.Opts) {
help.PrintVersion(opts.Version)
},
"backup": backup.PerformBackup,
"config": config.PrintConfig,
"help": func(opts *argparse.Opts) {
help.HelpArgs(opts.Args, opts.Version)
},
}

handler, exists := handlers[cmd]

opts.Args = opts.Args[1:]

if exists {
handler := handlers[cmd]

if handler != nil {
handler(opts)
} else {
help.PrintHelp(opts.Version)
goreland.LogFatal("No command named %s", cmd)
}
}

func checkExampleConfig(opts *argparse.Opts) {
if opts.ExampleConfig {
help.HelpExampleConfig()
Expand All @@ -50,5 +53,4 @@ func checkArgLen(opts *argparse.Opts) {
help.PrintHelp(opts.Version)
goreland.LogFatal("No command provided")
}

}
2 changes: 1 addition & 1 deletion pkg/help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func printCommands() {
messages := []string{"Backup your dotfiles", "Show version info", "Print the current config", "Show this message"}
pelp.Aligned("commands", commands, messages)
}
func HelpArgs(args []string, version string) {
func Handle(args []string, version string) {
if len(args) == 0 {
PrintHelp(version)
return
Expand Down

0 comments on commit df1ec80

Please sign in to comment.