summaryrefslogtreecommitdiff
path: root/main.go
blob: 84d1032c674e61b861cfc6eb95c50701b8a3a90b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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)

	log.Println("Starting craft")
	defer log.Println("Exiting 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
}