-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
68 lines (56 loc) · 1.12 KB
/
main.go
File metadata and controls
68 lines (56 loc) · 1.12 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
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
)
type Command struct {
Name string `json:"name"`
Command string `json:"command"`
}
type Commands []Command
func main() {
args := os.Args[1:]
if len(args) == 0 {
fmt.Printf("No command specified\n")
os.Exit(1)
}
home, err := os.UserHomeDir()
if err != nil {
fmt.Printf("Error getting home directory\n")
os.Exit(1)
}
path := home + "/.rconfig"
_, err = os.Stat(path)
if err != nil {
fmt.Printf("File %s does not exist\n", path)
os.Exit(1)
}
buffer, err := os.ReadFile(path)
if err != nil {
fmt.Printf("Error reading file %s\n", path)
os.Exit(1)
}
cmds := Commands{}
err = json.Unmarshal(buffer, &cmds)
if err != nil {
fmt.Printf("Error unmarshalling file %s\n", path)
os.Exit(1)
}
for _, arg := range args {
for _, cmd := range cmds {
if cmd.Name == arg {
fmt.Printf("%s\n", cmd.Command)
c := exec.Command("Rscript", "-e", cmd.Command)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
err := c.Run()
if err != nil {
fmt.Printf("Error running command %s\n", cmd.Command)
os.Exit(1)
}
}
}
}
}