/
main.go
85 lines (72 loc) · 2.03 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os/exec"
"strings"
"os"
)
//"sw_vers -productVersion" for current macOS python3 --version for current python3version
type Command []struct {
Executable string `json:"executable"`
Commands []string `json:"commands"`
}
func main() {
deleteFile()
executes := openCommands() //gets the json struct
//loops through Command struct
for x:=0; x<len(executes); x++ {
writeFile(executes[x].Executable + ":")
//loops through all the commands for executable
for y:=0; y<len(executes[x].Commands); y++{
temp := strings.Fields(executes[x].Commands[y]) //parses command string into words
fmt.Println(temp)
//checks of the command is a "where" command
if temp[0] == "where" {
path, err := exec.LookPath(temp[1])
errorCatch(err)
writeFile(executes[x].Commands[y] + ": " + path)
} else{ //other wise run the bash command as normal
cmd, err := exec.Command(temp[0], temp[1]).Output()
errorCatch(err)
temp := string(cmd)
temp = strings.TrimSuffix(temp, "\n")
writeFile(executes[x].Commands[y] + ": " + temp)
}
}
writeFile("\n")
}
}
//error thrower
func errorCatch(err error) {
if err != nil {
panic(err)
}
}
//opens the json file and sotres the content in the Command struct
func openCommands() Command {
//open file and error check it
fileContents, err := ioutil.ReadFile("commands.json")
errorCatch((err))
//creates a struct to hold json information and reads the json file into it
temp := Command{}
json.Unmarshal(fileContents, &temp)
return temp
}
//writes the version numbers to the text file
func writeFile(fileContents string) {
file, err := os.OpenFile("versions.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
errorCatch((err))
defer file.Close()
if _, err = file.WriteString(fileContents + "\n"); err != nil {
panic(err)
}
}
//clears the text file
func deleteFile() {
if _, err := os.Stat("versions.txt"); os.IsNotExist(err) { return }
// delete file
var err = os.Remove("versions.txt")
errorCatch(err)
}