-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
144 lines (119 loc) · 3.39 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
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
package main
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
"gopkg.in/yaml.v2"
)
type Config struct {
ShortCommands []ShortCommand `yaml:"shortcommands"`
}
type ShortCommand struct {
Name string `yaml:"name"`
Commands []Command `yaml:"commands"`
}
type Command struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
CurrentWorkingDirectory string `yaml:"cwd"`
Do []string `yaml:"do"`
}
func fileExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
var ConsoleColors = map[string]string{
"Reset": "\033[0m",
"Red": "\033[31m",
}
func executeCommand(commandToRun string, currentWorkingDirectory string) error {
cmd := exec.Command("sh", "-c", commandToRun)
if currentWorkingDirectory != "" {
currentWorkingDirectoryResolved := os.ExpandEnv(currentWorkingDirectory)
if strings.HasPrefix(currentWorkingDirectoryResolved, "~") {
home, _ := os.UserHomeDir()
currentWorkingDirectoryResolved = home + currentWorkingDirectoryResolved[1:]
}
if !fileExists(currentWorkingDirectoryResolved) {
fmt.Printf("%sUnable to find given working directory: %s%s\n", ConsoleColors["Red"], currentWorkingDirectory, ConsoleColors["Reset"])
}
cmd.Dir = currentWorkingDirectoryResolved
}
stdout := io.Writer(os.Stdout)
stderr := io.Writer(os.Stderr)
cmd.Stderr = stderr
cmd.Stdout = stdout
cmd.Start()
return cmd.Wait()
}
func main() {
shortcommandPath := os.Getenv("SHORTCOMMAND_CONFIG")
if shortcommandPath == "" {
fmt.Println("SHORTCOMMAND_CONFIG env not found or empty")
return
}
yamlFile, err := os.ReadFile(shortcommandPath)
if err != nil {
fmt.Println("Unable to find file specified in SHORTCOMMAND_CONFIG: ", shortcommandPath)
return
}
var parsedConfig Config
err = yaml.Unmarshal(yamlFile, &parsedConfig)
if err != nil {
fmt.Println("Invalid config file given")
return
}
if len(os.Args) < 2 {
fmt.Println("Available commands")
for _, shortCommand := range parsedConfig.ShortCommands {
fmt.Printf("%s\n", shortCommand.Name)
for _, command := range shortCommand.Commands {
fmt.Printf("%s%s%s%s\n", " ", command.Name, "\t", command.Description)
}
}
return
}
if len(os.Args) < 3 {
for _, shortCommand := range parsedConfig.ShortCommands {
if shortCommand.Name == os.Args[1] {
fmt.Printf("Available sub commands for %s\n", os.Args[1])
for _, command := range shortCommand.Commands {
fmt.Printf("%s%s%s\n", command.Name, "\t", command.Description)
}
break
}
}
return
}
for _, shortCommand := range parsedConfig.ShortCommands {
if shortCommand.Name == os.Args[1] {
for _, command := range shortCommand.Commands {
if command.Name == os.Args[2] {
fmt.Println(shortCommand.Name, ">", command.Name, ":", command.Description)
for i, commandToRun := range command.Do {
fmt.Println(">", commandToRun)
err = executeCommand(commandToRun, command.CurrentWorkingDirectory)
if err != nil {
if i < len(command.Do)-1 {
fmt.Printf("%sNot continuing with the next command in do sequence as previous command errored out%s\n", ConsoleColors["Red"], ConsoleColors["Reset"])
return
}
}
}
fmt.Println()
return
}
}
break
}
}
fmt.Println("No matching command found for given arguments")
}