From 99c130f7f78824226c6b2a24554ab1ee9c42ac34 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Mon, 4 Nov 2024 08:48:08 +0100 Subject: [PATCH] Use separate "up" and "dot" commands --- main.go | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index eb3f150..9ca00ad 100644 --- 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 []", os.Args[0]) + log.Fatalf("usage: %[1]s \n\n"+ + "Available commands:\n"+ + " Start lab: %[1]s up \n"+ + " Stop lab: %[1]s down \n"+ + " Create dot graph: %[1]s dot \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\" ") + } + cfg := loadConfig() + mustUp(cfg) + + case "dot": + // Write dot file showing the network architecture + if len(os.Args) != 4 { + log.Fatalf("invalid arguments; expected: \"dot\" ") + } + 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: -- 2.45.2