aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-09-27 19:55:57 +0200
committern-peugnet <n.peugnet@free.fr>2021-09-27 19:55:57 +0200
commit53640f33362d8d6dc65db403e3b31a6776b49796 (patch)
treef18c69aafc8b614978e4c5d9e10a987240eff5a0 /main.go
parentccb97e0bb5fb0e0c257411d442183d49dd734822 (diff)
downloaddna-backup-53640f33362d8d6dc65db403e3b31a6776b49796.tar.gz
dna-backup-53640f33362d8d6dc65db403e3b31a6776b49796.zip
add CLI with subcommands
Diffstat (limited to 'main.go')
-rw-r--r--main.go78
1 files changed, 68 insertions, 10 deletions
diff --git a/main.go b/main.go
index c0e55d8..cf208c9 100644
--- a/main.go
+++ b/main.go
@@ -8,32 +8,90 @@ import (
"github.com/n-peugnet/dna-backup/logger"
)
+type command struct {
+ Flag *flag.FlagSet
+ Usage string
+ Help string
+ Run func([]string) error
+}
+
const (
- usage = "usage: dna-backup [<options>] [--] <source> <dest>\n\noptions:\n"
+ name = "dna-backup"
+ baseUsage = "<command> [<options>] [--] <args>"
+ commitUsage = "[<options>] [--] <source> <dest>"
+ commitHelp = "Create a new version of folder <source> into repo <dest>"
+ restoreUsage = "[<options>] [--] <source> <dest>"
+ restoreHelp = "Restore the last version from repo <source> into folder <dest>"
)
var (
- logLevel int
+ logLevel int
+ commitCmd = flag.NewFlagSet("commit", flag.ExitOnError)
+ restoreCmd = flag.NewFlagSet("restore", flag.ExitOnError)
+ subcommands = map[string]command{
+ commitCmd.Name(): {commitCmd, commitUsage, commitHelp, commitMain},
+ restoreCmd.Name(): {restoreCmd, restoreUsage, restoreHelp, restoreMain},
+ }
)
func init() {
- flag.IntVar(&logLevel, "v", 3, "log verbosity level (0-4)")
+ // init default help message
+ flag.Usage = func() {
+ fmt.Fprintf(flag.CommandLine.Output(), "usage: %s %s\n\ncommands:\n", name, baseUsage)
+ for _, s := range subcommands {
+ fmt.Printf(" %s %s\n", s.Flag.Name(), s.Help)
+ }
+ os.Exit(1)
+ }
+ // setup subcommands
+ for _, s := range subcommands {
+ s.Flag.IntVar(&logLevel, "v", 3, "log verbosity level (0-4)")
+ }
}
func main() {
- flag.Usage = func() {
- fmt.Fprintf(flag.CommandLine.Output(), usage)
- flag.PrintDefaults()
- }
flag.Parse()
- logger.Init(logLevel)
- if len(flag.Args()) != 2 {
+
+ args := flag.Args()
+ if len(args) < 1 {
+ flag.Usage()
+ }
+ cmd, exists := subcommands[args[0]]
+ if !exists {
+ fmt.Fprintf(flag.CommandLine.Output(), "error: unknown command %s\n\n", args[0])
flag.Usage()
+ }
+ cmd.Flag.Usage = func() {
+ fmt.Fprintf(cmd.Flag.Output(), "usage: %s %s %s\n\noptions:\n", name, cmd.Flag.Name(), cmd.Usage)
+ cmd.Flag.PrintDefaults()
os.Exit(1)
}
- args := flag.Args()
+ cmd.Flag.Parse(args[1:])
+ logger.Init(logLevel)
+ if err := cmd.Run(cmd.Flag.Args()); err != nil {
+ fmt.Fprintf(cmd.Flag.Output(), "error: %s\n\n", err)
+ cmd.Flag.Usage()
+ }
+}
+
+func commitMain(args []string) error {
+ if len(args) != 2 {
+ return fmt.Errorf("wrong number of args")
+ }
source := args[0]
dest := args[1]
repo := NewRepo(dest)
repo.Commit(source)
+ return nil
+}
+
+func restoreMain(args []string) error {
+ if len(args) != 2 {
+ return fmt.Errorf("wrong number args")
+ }
+ source := args[0]
+ dest := args[1]
+ repo := NewRepo(source)
+ repo.Restore(dest)
+ return nil
}