]> ruderich.org/simon Gitweb - linux-network-namespace-labs/linux-network-namespace-labs.git/commitdiff
Use separate "up" and "dot" commands
authorSimon Ruderich <simon@ruderich.org>
Mon, 4 Nov 2024 07:48:08 +0000 (08:48 +0100)
committerSimon Ruderich <simon@ruderich.org>
Mon, 4 Nov 2024 07:48:08 +0000 (08:48 +0100)
main.go

diff --git a/main.go b/main.go
index eb3f15075a3c3d04a029081522f61c9e5c3420ea..9ca00ad9b67d901e6798e8c0a984f97b09f89307 100644 (file)
--- a/main.go
+++ b/main.go
@@ -13,25 +13,46 @@ import (
 )
 
 func main() {
-       if len(os.Args) != 2 && len(os.Args) != 3 {
+       if len(os.Args) < 2 {
                log.SetFlags(0)
-               log.Fatalf("usage: %s <config> [<dot-output>]", os.Args[0])
+               log.Fatalf("usage: %[1]s <command> <args...>\n\n"+
+                       "Available commands:\n"+
+                       "  Start lab:        %[1]s up <config>\n"+
+                       "  Stop lab:         %[1]s down <config>\n"+
+                       "  Create dot graph: %[1]s dot <config> <output>\n",
+                       os.Args[0])
        }
 
-       cfg, err := LoadConfig(os.Args[1])
-       if err != nil {
-               log.Fatalf("config %q: %v", os.Args[1], err)
+       loadConfig := func() *Config {
+               cfg, err := LoadConfig(os.Args[2])
+               if err != nil {
+                       log.Fatalf("config %q: %v", os.Args[2], err)
+               }
+               return cfg
        }
 
-       // Write dot file showing the network architecture
-       if len(os.Args) == 3 {
-               err := writeDot(cfg, os.Args[2])
+       switch os.Args[1] {
+       case "up":
+               if len(os.Args) != 3 {
+                       log.Fatalf("invalid arguments; expected: \"up\" <config>")
+               }
+               cfg := loadConfig()
+               mustUp(cfg)
+
+       case "dot":
+               // Write dot file showing the network architecture
+               if len(os.Args) != 4 {
+                       log.Fatalf("invalid arguments; expected: \"dot\" <config> <output>")
+               }
+               cfg := loadConfig()
+               err := writeDot(cfg, os.Args[3])
                if err != nil {
                        log.Fatal(err)
                }
-       }
 
-       mustUp(cfg)
+       default:
+               log.Fatalf("invalid command %q", os.Args[1])
+       }
 }
 
 // vi: set noet ts=4 sw=4 sts=4: