)
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: