]> ruderich.org/simon Gitweb - linux-network-namespace-labs/linux-network-namespace-labs.git/commitdiff
up: change to configuration directory before running commands
authorSimon Ruderich <simon@ruderich.org>
Sun, 12 Jul 2026 05:51:12 +0000 (07:51 +0200)
committerSimon Ruderich <simon@ruderich.org>
Sun, 12 Jul 2026 05:51:12 +0000 (07:51 +0200)
Suggested-by: Luca <luca@daerich.com>
ci/examples
main.go

index ff5a73d227a39dfd70184f33d84b0eb428fe8ff4..945cb3dab47001b0513fdd213f140224ad6e0bf0 100755 (executable)
@@ -28,15 +28,15 @@ export PATH
 test -z "$(ip netns)"
 
 
-cd "$examples/bird-ospf"
-linux-network-namespace-labs up lab.conf
+# No `cd` here to test that relative paths work
+linux-network-namespace-labs up "$examples/bird-ospf/lab.conf"
 # Wait until OSPF is ready
 wait_for_route r1 3fff::1
 wait_for_route r1 3fff::2
 ip netns exec r1 ping -c1 r1-loop
 ip netns exec r1 ping -c1 r2-loop
 ip netns exec r1 ping -c1 r3-loop
-linux-network-namespace-labs down lab.conf
+linux-network-namespace-labs down "$examples/bird-ospf/lab.conf"
 test -z "$(ip netns)"
 test "$(cd /etc/netns; find)" = "." # directory empty
 
diff --git a/main.go b/main.go
index 700afd920b1e81a3ccdb1e31f034ad484e55d024..44297e09a03ab3e18c6be099544c701be724c51c 100644 (file)
--- a/main.go
+++ b/main.go
@@ -10,6 +10,7 @@ package main
 import (
        "log"
        "os"
+       "path/filepath"
 )
 
 func main() {
@@ -37,6 +38,14 @@ func main() {
                        log.Fatalf("invalid arguments; expected: \"up\" <config>")
                }
                cfg := loadConfig()
+
+               // Automatically change to the directory of the configuration file so
+               // that relative paths work
+               err := os.Chdir(filepath.Dir(os.Args[2]))
+               if err != nil {
+                       log.Fatal(err)
+               }
+
                mustUp(cfg)
 
        case "down":