package main import ( "bytes" "flag" "log" "os" "os/exec" "path/filepath" "strings" ) var ( repo = "" commit = "" ) func init() { flag.StringVar(&repo, "repo", "", "") flag.StringVar(&commit, "commit", "", "") } func main() { flag.Parse() log.SetFlags(0) defer log.Println("Exiting craft") log.Println("Starting craft") workbench := prepareWorkbench(repo, commit) defer os.RemoveAll(workbench) craft(workbench) } func runCommand(dir string, name string, args ...string) { log.Printf("%s %s\n", name, strings.Join(args, " ")) command := exec.Command(name, args...) command.Dir = dir command.Stderr = os.Stderr command.Stdout = os.Stdout err := command.Run() if err != nil { log.Fatal(err) } } func craft(workbench string) { script := `#!/bin/sh source craft.sh build ` cmd := exec.Command("busybox", "ash") cmd.Dir = workbench cmd.Stdin = bytes.NewBufferString(script) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err := cmd.Run() if err != nil { log.Fatal(err) } } func prepareWorkbench(repo string, commit string) string { workbench, err := os.MkdirTemp("", "*-craft") if err != nil { log.Fatal(err) } runCommand( workbench, "git", "clone", repo, workbench, ) runCommand( workbench, "git", "--git-dir", filepath.Join(workbench, ".git"), "--work-tree", workbench, "checkout", commit, ) runCommand( workbench, "git", "--git-dir", filepath.Join(workbench, ".git"), "--work-tree", workbench, "submodule", "update", "--depth=1", "--init", "--recursive", ) return workbench }