Skip to content

Commit

Permalink
refactor: removed all subcommand opts, moved all argument's to argpar…
Browse files Browse the repository at this point in the history
…se.Opts
  • Loading branch information
pspiagicw committed Apr 12, 2024
1 parent 78d4e5c commit f5e45b7
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 46 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added --config option
- Added --example-config option.
- Added --ignore option to backup subcommand.
- Parse config and backup files accordingly
Expand Down
8 changes: 8 additions & 0 deletions pkg/argparse/argparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ type Opts struct {
ExampleConfig bool
Args []string
Version string

// Config file path
Config string

// Backup options
DryRun bool
Ignore bool
}

func ParseArguments(version string) *Opts {
Expand All @@ -20,6 +27,7 @@ func ParseArguments(version string) *Opts {
help.PrintHelp(version)
}
flag.BoolVar(&opts.ExampleConfig, "example-config", false, "Print example config.")
flag.StringVar(&opts.Config, "config", "", "Path to the alternate config file.")
flag.Usage = Usage
flag.Parse()

Expand Down
54 changes: 24 additions & 30 deletions pkg/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,34 @@ import (

"github.com/AlecAivazis/survey/v2"
"github.com/kballard/go-shellquote"
"github.com/pspiagicw/dotback/pkg/argparse"
"github.com/pspiagicw/dotback/pkg/config"
"github.com/pspiagicw/dotback/pkg/help"
"github.com/pspiagicw/goreland"
)

type opts struct {
dryRun bool
args []string
ignore bool
}

func parseBackupOpts(args []string) *opts {
opts := new(opts)
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.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(args)
flag.Parse(opts.Args)

opts.args = flag.Args()

return opts
opts.Args = flag.Args()
}

func PerformBackup(args []string) {
func PerformBackup(opts *argparse.Opts) {

parseBackupOpts(opts)

opts := parseBackupOpts(args)
targets := opts.Args

configFile := preBackup(opts)

if len(args) != 0 {
if len(targets) != 0 {
backupSelective(configFile, opts)
} else {
if opts.ignore {
if opts.Ignore {
goreland.LogFatal("Can't ignore all rules. Please specify the rules to ignore.")
}
backupAll(configFile, opts)
Expand All @@ -50,7 +44,7 @@ func PerformBackup(args []string) {
goreland.LogSuccess("Backup successful!")
}

func preBackup(opts *opts) *config.Config {
func preBackup(opts *argparse.Opts) *config.Config {
configFile := config.GetConfig()
confirmBackup()
ensureStorePath(configFile)
Expand All @@ -72,15 +66,15 @@ func confirmBackup() {
}

}
func backupSelective(configFile *config.Config, opts *opts) {
if opts.ignore {
func backupSelective(configFile *config.Config, opts *argparse.Opts) {
if opts.Ignore {
ignoreRules(configFile, opts)
} else {
executeSelectiveBackup(configFile, opts)
}
}
func ignoreRules(configFile *config.Config, opts *opts) {
ignoredRules := opts.args
func ignoreRules(configFile *config.Config, opts *argparse.Opts) {
ignoredRules := opts.Args
for name, _ := range configFile.Rules {
if !contains(ignoredRules, name) {
executeRule(configFile, name, opts)
Expand All @@ -97,8 +91,8 @@ func contains(rules []string, name string) bool {
}
return false
}
func executeSelectiveBackup(configFile *config.Config, opts *opts) {
for _, name := range opts.args {
func executeSelectiveBackup(configFile *config.Config, opts *argparse.Opts) {
for _, name := range opts.Args {
executeRule(configFile, name, opts)
}
}
Expand All @@ -113,15 +107,15 @@ func getRule(name string, configFile *config.Config) *config.BackupRule {
return rule

}
func executeRule(configFile *config.Config, name string, opts *opts) {
func executeRule(configFile *config.Config, name string, opts *argparse.Opts) {

rule := getRule(name, configFile)

goreland.LogInfo("Backing up [%s]", name)

src, dest := getPath(configFile, rule)

if !opts.dryRun {
if !opts.DryRun {
performCopy(src, dest, configFile.Ignore)
} else {
goreland.LogInfo("Move %s -> %s", src, dest)
Expand All @@ -133,7 +127,7 @@ func getPath(configFile *config.Config, rule *config.BackupRule) (string, string
dest := filepath.Join(storeDir, filepath.Base(src))
return src, dest
}
func backupAll(configFile *config.Config, opt *opts) {
func backupAll(configFile *config.Config, opt *argparse.Opts) {
for name, _ := range configFile.Rules {
executeRule(configFile, name, opt)
}
Expand All @@ -148,9 +142,9 @@ func confirmAfterBackUp() {
goreland.LogFatal("User cancelled the after-backup procedure")
}
}
func runAfterBackup(configfile *config.Config, opts *opts) {
func runAfterBackup(configfile *config.Config, opts *argparse.Opts) {

if opts.dryRun {
if opts.DryRun {
goreland.LogInfo("DRY RUN: Run after-backup commands.")
return
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/config/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import (
"flag"
"fmt"

"github.com/pspiagicw/dotback/pkg/argparse"
"github.com/pspiagicw/dotback/pkg/help"
"github.com/pspiagicw/goreland"
)

func parseConfigArgs(args []string) {
func parseConfigArgs(opts *argparse.Opts) {
flag := flag.NewFlagSet("dotback config", flag.ExitOnError)

flag.Usage = help.HelpConfig

flag.Parse(args)
flag.Parse(opts.Args)
}

func PrintConfig(args []string) {
parseConfigArgs(args)
func PrintConfig(opts *argparse.Opts) {
parseConfigArgs(opts)
fmt.Println("DOTBACK CONFIG")
config := GetConfig()
fmt.Printf("Location dir: %s\n", config.StoreDir)
Expand All @@ -26,13 +27,13 @@ func PrintConfig(args []string) {

}
func printAfterBackup(config *Config) {
fmt.Println("\nThe after-backup commands:\n")
fmt.Println("\nThe after-backup commands:")
for _, command := range config.AfterBackup {
goreland.LogExec(command)
}
}
func printRules(config *Config) {
fmt.Println("\nConfigured backup rules:\n")
fmt.Println("\nConfigured backup rules:")
headers := []string{
"Name",
"Location",
Expand Down
16 changes: 7 additions & 9 deletions pkg/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,28 @@ func HandleArgs(opts *argparse.Opts) {

cmd := opts.Args[0]

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

handler, exists := handlers[cmd]

opts.Args = opts.Args[1:]

if exists {
handler(opts.Args[1:])
handler(opts)
} else {
help.PrintHelp(opts.Version)
goreland.LogFatal("No command named %s", cmd)
}
}
func notImplemented(args []string) {
goreland.LogError("This feature is not implemented yet!")
}
func checkExampleConfig(opts *argparse.Opts) {
if opts.ExampleConfig {
help.HelpExampleConfig()
Expand Down
9 changes: 8 additions & 1 deletion pkg/help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
)

const EXAMPLE_CONFIG = `
# A folder to store the backup, it will be created if it does not exist.
storeDir = "~/.local/state/backup"
Expand Down Expand Up @@ -44,12 +43,20 @@ func PrintHelp(version string) {
PrintVersion(version)
printHeader()
printCommands()
printFlags()

pelp.Examples("examples", []string{"dotback backup", "dotback config"})

printFooter()
}

func printFlags() {
flags := []string{"example-config", "config"}
messages := []string{"Print example config", "Path to the alternate config file."}

pelp.Flags("flags", flags, messages)
}

func printFooter() {
pelp.HeaderWithDescription("more help", []string{"Use 'dotback help [command]' for more info about a command."})
}
Expand Down

0 comments on commit f5e45b7

Please sign in to comment.