]> ruderich.org/simon Gitweb - nsscash/nsscash.git/commitdiff
nsscash: add "fetch" sub-command for the default action
authorSimon Ruderich <simon@ruderich.org>
Sat, 8 Jun 2019 10:39:55 +0000 (12:39 +0200)
committerSimon Ruderich <simon@ruderich.org>
Sat, 8 Jun 2019 10:41:24 +0000 (12:41 +0200)
In preparation for more sub-commands in the future.

main.go

diff --git a/main.go b/main.go
index 50263877f2a71380b1b774e40f3984498324a8c6..3b2dba8eac16e385fcebe8e4b9ab6f8e353dbe8d 100644 (file)
--- a/main.go
+++ b/main.go
 package main
 
 import (
+       "flag"
+       "fmt"
        "log"
        "os"
 )
 
 func main() {
-       if len(os.Args) != 2 {
-               log.SetFlags(0)
-               log.Fatalf("usage: %s <path/to/config>\n", os.Args[0])
+       flag.Usage = func() {
+               fmt.Fprintf(os.Stderr,
+                       "usage: %[1]s [options] fetch <config>\n"+
+                               "",
+                       os.Args[0])
+               flag.PrintDefaults()
        }
+       flag.Parse()
 
-       cfg, err := LoadConfig(os.Args[1])
+       args := flag.Args()
+       if len(args) == 0 {
+               flag.Usage()
+               os.Exit(1)
+       }
+
+       switch args[0] {
+       case "fetch":
+               if len(args) != 2 {
+                       break
+               }
+
+       cfg, err := LoadConfig(args[1])
        if err != nil {
                log.Fatal(err)
        }
@@ -46,4 +64,10 @@ func main() {
        if err != nil {
                log.Fatal(err)
        }
+
+               return
+       }
+
+       flag.Usage()
+       os.Exit(1)
 }