Change behaviour of 1-arg and 3-arg commands

This commit is contained in:
Late Night Defender 2024-06-04 09:10:46 +07:00
parent 663d89bded
commit 07e844d0f6

32
main.go
View file

@ -68,7 +68,6 @@ func ExecSystem(command string) {
} }
func main() { func main() {
flag.Parse() flag.Parse()
@ -80,12 +79,21 @@ func main() {
os.Exit(1) os.Exit(1)
} }
// ./compose-runner should run docker compose up -d in current working directory
if len(os.Args) == 1 { if len(os.Args) == 1 {
fmt.Println("Usage: compose-runner [-v] [path]") workDir, err := os.Getwd()
os.Exit(2)
if err != nil {
panic(err)
}
os.Chdir(workDir)
fmt.Println("Running docker compose up -d in", workDir)
ExecSystem("docker compose up -d")
} }
if len(os.Args) == 2 { if len(os.Args) == 2 {
// ./compose-runner -v should run docker compose up -d in current working directory, verbose output
if *verbosePtr { if *verbosePtr {
workDir, err := os.Getwd() workDir, err := os.Getwd()
@ -94,25 +102,25 @@ func main() {
} }
os.Chdir(workDir) os.Chdir(workDir)
fmt.Println("Running docker compose up -d in", workDir)
ExecSystem("docker compose up -d") ExecSystem("docker compose up -d")
} else { } else {
// ./compose-runner /path should run docker compose up -d in /path
workDir := os.Args[1] workDir := os.Args[1]
os.Chdir(workDir) os.Chdir(workDir)
fmt.Println("Running docker compose up -d in", workDir)
ExecSystem("docker compose up -d") ExecSystem("docker compose up -d")
} }
} }
// ./compose-runner -v /path should run docker compose up -d in /path, verbose output
if len(os.Args) == 3 { if len(os.Args) == 3 {
if os.Args[1] == "-v" { workDir := os.Args[2]
workDir := os.Args[2] os.Chdir(workDir)
os.Chdir(workDir) fmt.Println("Running docker compose up -d in", workDir)
ExecSystem("docker compose up -d") ExecSystem("docker compose up -d")
} else {
workDir := os.Args[1]
os.Chdir(workDir)
ExecSystem("docker compose up -d")
}
} }
} }