-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
91 lines (74 loc) · 1.86 KB
/
main.go
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
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"runtime"
"github.com/zpratt/jig/adapters"
"gopkg.in/yaml.v2"
utilsexec "k8s.io/utils/exec"
)
type JigConfig struct {
Tools []string `yaml:"tools"`
}
func main() {
desiredState, err := desiredStateFactory()
if err != nil {
log.Fatal("Unable to load OS specific setup:\n", err.Error())
}
jigConfig := parseConfig()
notInstalled := findMissingPackages(jigConfig)
if len(notInstalled) > 0 {
for _, notInstalledTool := range notInstalled {
desiredState.InstallPackage(notInstalledTool)
}
} else {
log.Printf("all tools installed")
}
}
func findMissingPackages(jigConfig JigConfig) []string {
var notInstalled []string
for _, tool := range jigConfig.Tools {
_, err := exec.LookPath(tool)
if err != nil {
notInstalled = append(notInstalled, tool)
}
}
return notInstalled
}
func parseConfig() JigConfig {
config := JigConfig{}
jigConfigFile := "jig.yaml"
_, err := os.Stat(jigConfigFile)
if err != nil {
log.Fatalf("%s config does not exist", jigConfigFile)
}
file, _ := ioutil.ReadFile(jigConfigFile)
err = yaml.Unmarshal(file, &config)
if err != nil {
log.Fatalf("failed to parse %s", jigConfigFile)
}
return config
}
func desiredStateFactory() (*DesiredState, error) {
execInst := utilsexec.New()
factories := map[string]func() *DesiredState{
"darwin": func() *DesiredState {
return &DesiredState{PlatformAdapter: adapters.NewDarwinAdapter(execInst)}
},
"linux": func() *DesiredState {
return &DesiredState{PlatformAdapter: adapters.NewLinuxAdapter(execInst)}
},
"windows": func() *DesiredState {
return &DesiredState{PlatformAdapter: adapters.NewWindowsAdapter(execInst)}
},
}
if factory, ok := factories[runtime.GOOS]; ok {
return factory(), nil
} else {
return nil, errors.New(fmt.Sprint("Unsupported OS: ", runtime.GOOS))
}
}