Rewrite run-dev.sh to use golang program
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func panicError(errorString string, params ...any) {
|
||||
fmt.Fprintf(os.Stderr, fmt.Sprintf("ERROR: %v\n", errorString), params...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func run(name string, arg ...string) {
|
||||
cmd := exec.Command(name, arg...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
panicError("could not run command %v: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
func runAttach(name string, arg ...string) {
|
||||
cmd := exec.Command(name, arg...)
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
panicError("could not run command %v: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
func runCompose(args []string) {
|
||||
runAttach("docker", append([]string{"compose"}, args...)...)
|
||||
}
|
||||
|
||||
func createDirs() {
|
||||
run("mkdir", "-p", "_working/go")
|
||||
run("mkdir", "-p", "_working/jobs")
|
||||
}
|
||||
|
||||
func currentContainers() string {
|
||||
bytes, err := os.ReadFile("_working/current_containers")
|
||||
if err != nil {
|
||||
panicError("could not read current containers: %v", err)
|
||||
}
|
||||
return string(bytes)
|
||||
}
|
||||
|
||||
func composeFlags() []string {
|
||||
containers := currentContainers()
|
||||
flags := []string{"-f", "docker/docker-compose.yml"}
|
||||
switch containers {
|
||||
case "gitea":
|
||||
flags = append(flags, "--profile", "gitea")
|
||||
}
|
||||
return flags
|
||||
}
|
||||
|
||||
func runContainers(containers string) {
|
||||
err := os.WriteFile("_working/current_containers", []byte(containers), 0633)
|
||||
if err != nil {
|
||||
panicError("could not write current_containers file: %v", err)
|
||||
}
|
||||
runCompose(append(composeFlags(), "up", "--build", "-d"))
|
||||
runCompose(append(composeFlags(), "logs", "-f"))
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
panicError("not enough arguments passed")
|
||||
}
|
||||
|
||||
createDirs()
|
||||
|
||||
switch os.Args[1] {
|
||||
case "default", "gitea":
|
||||
runContainers(os.Args[1])
|
||||
case "runprev":
|
||||
runContainers(currentContainers())
|
||||
case "stop":
|
||||
runCompose(append(composeFlags(), "down"))
|
||||
case "dbshell":
|
||||
runCompose(append(composeFlags(), "exec", "cursorius-db", "psql", "--user=cursorius"))
|
||||
case "logs":
|
||||
runCompose(append(composeFlags(), "logs", "-f"))
|
||||
case "ps":
|
||||
runCompose(append(composeFlags(), "ps"))
|
||||
case "help":
|
||||
fmt.Println("commands: default, gitea, runprev, stop, dbshell, logs, ps, help")
|
||||
default:
|
||||
panicError("Unknown subcommand: %v", os.Args[1])
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user