-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
202 lines (170 loc) · 4.27 KB
/
main.go
File metadata and controls
202 lines (170 loc) · 4.27 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"sync"
"syscall"
)
// Define a map of color names to ANSI color codes
var colorCodes = map[string]string{
"red": "\033[31m",
"green": "\033[32m",
"yellow": "\033[33m",
"blue": "\033[34m",
"magenta": "\033[35m",
"cyan": "\033[36m",
"gray": "\033[90m",
"orange": "\033[38;5;214m",
"pink": "\033[38;5;207m",
"lime": "\033[38;5;10m",
"white": "\033[37m", // Default color
}
// Function to colorize a string
func colorize(text, color string) string {
// Get the ANSI code for the color; default to white if not found
ansiCode, exists := colorCodes[color]
if !exists {
ansiCode = colorCodes["white"]
}
// Return the colored string with a reset at the end
return fmt.Sprintf("%s%s\033[0m", ansiCode, text)
}
type Command struct {
Label string
Color string
CmdStr string
ExecPath string
}
type ConfigFile struct {
Commands []Command
}
func createConfigFile(config ConfigFile) error {
workingDir, err := os.Getwd()
if err != nil {
return err
}
filePath := filepath.Join(workingDir, "config.xrun.json")
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
if err := encoder.Encode(config); err != nil {
return err
}
return nil
}
func readConfigFile() (ConfigFile, error) {
workingDir, err := os.Getwd()
if err != nil {
return ConfigFile{}, err
}
filePath := filepath.Join(workingDir, "config.xrun.json")
file, err := os.Open(filePath)
if err != nil {
return ConfigFile{}, err
}
defer file.Close()
var config ConfigFile
decoder := json.NewDecoder(file)
if err := decoder.Decode(&config); err != nil {
return ConfigFile{}, err
}
return config, nil
}
func runCommand(cmdStruct Command, wg *sync.WaitGroup) {
defer wg.Done()
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/C", fmt.Sprintf("cd %s && %s", cmdStruct.ExecPath, cmdStruct.CmdStr))
} else {
cmd = exec.Command("sh", "-c", fmt.Sprintf("cd %s && %s", cmdStruct.ExecPath, cmdStruct.CmdStr))
}
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
if err := cmd.Start(); err != nil {
fmt.Println("Error starting command:", err)
return
}
fmt.Println(colorize(fmt.Sprintf("Running command: %s", cmdStruct.Label), cmdStruct.Color))
outputScanner := bufio.NewScanner(stdout)
errorScanner := bufio.NewScanner(stderr)
wg.Add(1)
go func() {
defer wg.Done()
for outputScanner.Scan() {
fmt.Printf("%s %s\n", colorize(fmt.Sprintf("[%s]", cmdStruct.Label), cmdStruct.Color), outputScanner.Text())
}
}()
wg.Add(1)
go func() {
defer wg.Done()
for errorScanner.Scan() {
fmt.Printf("%s %s\n", colorize(fmt.Sprintf("[%s]", cmdStruct.Label), cmdStruct.Color), errorScanner.Text())
}
}()
}
func showCommandsInfo(config ConfigFile) {
fmt.Println(colorize("Configured Commands:", "yellow"))
for _, cmd := range config.Commands {
fmt.Printf("%s %s\n", colorize(cmd.Label+" -> ", cmd.Color), cmd.CmdStr)
}
fmt.Println(colorize("========================", "yellow"))
}
func showArtBanner() {
banner := `
▄ ▄ ▄▄▄ █ ▐▌▄▄▄▄
▀▄▀ █ ▀▄▄▞▘█ █
▄▀ ▀▄ █ █ █
`
fmt.Println(colorize(banner, "cyan"))
}
func main() {
isInit := flag.Bool("init", false, "Create a new xrun configuration")
flag.Parse()
if *isInit {
config := ConfigFile{
Commands: []Command{
{
Label: "echo",
Color: "green",
CmdStr: "echo 'Hello, World!'",
ExecPath: ".",
},
},
}
createConfigFile(config)
fmt.Println("Creating a new xrun configuration")
} else {
config, err := readConfigFile()
if err != nil {
fmt.Println("Error reading config file:", err)
os.Exit(1)
}
showArtBanner()
showCommandsInfo(config)
var wg sync.WaitGroup
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
for _, cmd := range config.Commands {
wg.Add(1)
go runCommand(cmd, &wg)
}
go func() {
<-signalChan
fmt.Println("\nReceived interrupt. Stopping all commands...")
wg.Wait() // Ensure all goroutines finish
os.Exit(0)
}()
wg.Wait()
}
}