aboutsummaryrefslogtreecommitdiff
path: root/nix/builder/install/install.go
blob: 4f770b019a85ae45bcaa1c6dbe1c16c37ddfa041 (plain) (blame)
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
package main

import (
	"fmt"
	"go/parser"
	"go/token"
	"io"
	"os"
	"os/exec"
	"strconv"
)

const filename = "tools.go"

func main() {
	fset := token.NewFileSet()

	var src []byte
	{
		f, err := os.Open(filename)
		if err != nil {
			panic(err)
		}

		src, err = io.ReadAll(f)
		if err != nil {
			panic(err)
		}
	}

	f, err := parser.ParseFile(fset, filename, src, parser.ImportsOnly)
	if err != nil {
		fmt.Println(err)
		return
	}

	for _, s := range f.Imports {
		path, err := strconv.Unquote(s.Path.Value)
		if err != nil {
			panic(err)
		}

		cmd := exec.Command("go", "install", path)

		fmt.Printf("Executing '%s'\n", cmd)

		err = cmd.Start()
		if err != nil {
			panic(err)
		}

		err = cmd.Wait()
		if err != nil {
			panic(err)
		}
	}
}