-
Notifications
You must be signed in to change notification settings - Fork 1
/
genie.go
153 lines (129 loc) · 3.22 KB
/
genie.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/imdario/mergo"
"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v2"
)
type environment struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
type command struct {
Command string `yaml:"command"`
Environment []environment `yaml:"environment"`
}
type configFile struct {
Commands map[string][]command `yaml:"commands"`
Shell string `yaml:"shell"`
}
func findCommandFiles() []string {
var discoveredFiles []string
homeDirectory, _ := homedir.Dir()
discoveredFiles = append(discoveredFiles, homeDirectory+"/.genie-commands.yaml")
path, err := os.Getwd()
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Base(path) == "genie.yaml" {
discoveredFiles = append(discoveredFiles, path)
}
return nil
})
if err != nil {
log.Println(err)
}
return discoveredFiles
}
func initCommandsFile() string {
path, _ := os.Getwd()
targetFile := path + "/genie.yaml"
_, err := os.Stat(targetFile)
if err == nil {
log.Fatalf("genie.yaml file already exists at " + path)
}
commandMap := make(map[string][]command)
commandMap["example"] = []command{
command{Command: "echo this is an example command"},
}
t := configFile{
Shell: "/bin/bash",
Commands: commandMap,
}
content, _ := yaml.Marshal(&t)
err = ioutil.WriteFile(targetFile, []byte(content), 0644)
if err != nil {
log.Fatalf("Unable to create file " + path)
}
return path
}
func (c *configFile) getConf() *configFile {
files := findCommandFiles()
for k := range files {
yamlFile, err := ioutil.ReadFile(files[k])
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
tempStruct := configFile{}
err = yaml.Unmarshal(yamlFile, &tempStruct)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
mergo.Merge(c, tempStruct)
}
return c
}
func (c *configFile) getAvailableCommands() {
fmt.Println("Available commands:")
fmt.Println("\tinit - create a commands.yaml file in the current directory")
for k := range c.Commands {
fmt.Println("\t" + k)
}
}
func main() {
var dryRun bool
flag.BoolVar(&dryRun, "dry", false, "Run in dry mode to print commands")
flag.Parse()
var c configFile
c.getConf()
args := os.Args[1:]
if 0 == len(args) {
c.getAvailableCommands()
os.Exit(0)
}
commandName := flag.Args()[0]
if commandName == "init" {
path := initCommandsFile()
fmt.Printf("Created commands file at " + path)
}
for key := range c.Commands[commandName] {
if dryRun == true {
fmt.Printf(">\t%s\n", c.Commands[commandName][key].Command)
} else {
command := c.Commands[commandName][key].Command
env := c.Commands[commandName][key].Environment
cmd := exec.Command("bash", "-c", command)
cmd.Env = append(os.Environ(), envBuilder(env)...)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("%s", string(out))
log.Fatalf("Command failed with %s\n", err)
}
fmt.Printf("%s", string(out))
}
}
}
func envBuilder(env []environment) []string {
var result []string
for _, row := range env {
result = append(result, fmt.Sprintf("%s=%s\n", row.Name, row.Value))
}
return result
}