From af8f52e2e4c879935656dd93c2fb564c6e2ce515 Mon Sep 17 00:00:00 2001 From: flu0r1ne Date: Thu, 22 Jul 2021 00:41:30 -0500 Subject: Parse within subcommands --- cmd/cat.go | 11 ++++++++++- cmd/diff.go | 18 ++++++++++-------- cmd/helpers.go | 16 ++++++++++++++++ cmd/list.go | 24 ++++++++++++++++++++---- cmd/overwrite.go | 20 ++++++++++---------- 5 files changed, 66 insertions(+), 23 deletions(-) create mode 100644 cmd/helpers.go (limited to 'cmd') diff --git a/cmd/cat.go b/cmd/cat.go index 4f54abc..d5a275d 100644 --- a/cmd/cat.go +++ b/cmd/cat.go @@ -6,7 +6,16 @@ import ( ) func Cat(params []string) { + if(len(params) == 0) { + die.Fatal("Reference file is required") + } + + if(len(params) > 3) { + die.Fatal("Too many arguments provided") + } + snapRef := snap.ToRelative(params[0]) + reference := params[1] - fmt.Println(snapRef); + fmt.Println(snapRef, reference); } \ No newline at end of file diff --git a/cmd/diff.go b/cmd/diff.go index 7caf96b..46327b3 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -1,21 +1,23 @@ package cmd import ( - "flag" "fmt" + "golang.flu0r1ne.net/zfdiff/snap" ) func Diff(params []string) { - flags := flag.NewFlagSet("diff", flag.ExitOnError) - var walk bool + if(len(params) == 0) { + die.Fatal("Reference file is required") + } - const WALK_HELP = "walk backwards through the diff history" + if(len(params) > 2) { + die.Fatal("Too many arguments provided") + } - flags.BoolVar(&walk, "walk", false, WALK_HELP); - flags.BoolVar(&walk, "w", false, WALK_HELP); + from, to := snap.ParseDiff(params[0]) - flags.Parse(params); + reference := params[1] - fmt.Printf("Your flag is: %t", walk); + fmt.Println(from, to, reference); } diff --git a/cmd/helpers.go b/cmd/helpers.go new file mode 100644 index 0000000..858cdfd --- /dev/null +++ b/cmd/helpers.go @@ -0,0 +1,16 @@ +package cmd + +import ( + "os" + "log" + "flag" +) + +var die = log.New(os.Stderr, "", 0) + +func aliasedBoolVar(f * flag.FlagSet, p * bool, value bool, usage string) func(string) { + + return func(name string) { + f.BoolVar(p, name, value, usage) + } +} \ No newline at end of file diff --git a/cmd/list.go b/cmd/list.go index d2b9a03..99d5f24 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -1,8 +1,8 @@ package cmd import ( - "flag" "fmt" + "flag" ) func List(params []string) { @@ -10,12 +10,28 @@ func List(params []string) { var withPaths bool - const WITH_PATHS_HELP = "print paths to the provided reference within the snapshot" + withName := aliasedBoolVar( + flags, + &withPaths, + false, + "print paths to the provided reference within the snapshot", + ) - flags.BoolVar(&withPaths, "paths", false, WITH_PATHS_HELP); - flags.BoolVar(&withPaths, "p", false, WITH_PATHS_HELP); + withName("paths") + withName("p") flags.Parse(params); + if(flags.NArg() == 0) { + die.Fatal("Reference file is required") + } + + if(flags.NArg() > 1) { + die.Fatal("Too many arguments provided") + } + + reference := flags.Arg(0) + + fmt.Printf(reference) fmt.Printf("Your flag is: %t", withPaths); } diff --git a/cmd/overwrite.go b/cmd/overwrite.go index 6f4a00b..324dfb4 100644 --- a/cmd/overwrite.go +++ b/cmd/overwrite.go @@ -1,21 +1,21 @@ package cmd import ( - "flag" "fmt" + "golang.flu0r1ne.net/zfdiff/snap" ) func Overwrite(params [] string) { - flags := flag.NewFlagSet("overwrite", flag.ExitOnError); + if(len(params) == 0) { + die.Fatal("Reference file is required") + } - var withBackup bool + if(len(params) > 3) { + die.Fatal("Too many arguments provided") + } - const WITH_BACKUP_HELP = "backup the file by creating a snapshot" + snapRef := snap.ToRelative(params[0]) + reference := params[1] - flags.BoolVar(&withBackup, "backup", false, WITH_BACKUP_HELP); - flags.BoolVar(&withBackup, "b", false, WITH_BACKUP_HELP); - - flags.Parse(params); - - fmt.Printf("Your flag is: %t", withBackup); + fmt.Print(snapRef, reference) } \ No newline at end of file -- cgit v1.2.3