-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.go
60 lines (46 loc) · 1.13 KB
/
environment.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
package main
import (
"encoding/json"
"os"
)
func getEnvironment(environment string) string {
type Environment struct {
Environment string `json:"enviroment"`
VMID string `json:"vmid"`
}
var environments map[string]Environment
// get the environment from environment.json
environmentsFile, readErr := os.ReadFile(findFile())
if readErr != nil {
logger.Fatal("Error opening environments.json")
}
unmarshalErr := json.Unmarshal(environmentsFile, &environments)
if unmarshalErr != nil {
logger.Fatal("Error unmarshalling environments.json")
}
// get the environment
for _, env := range environments {
if env.Environment == environment {
return env.VMID
}
}
logger.Fatal("Environment not found")
return ""
}
func findFile() string {
possible_locations := []string{
"$HOME/.config/proxmox-cli/environments.json",
"/etc/proxmox-cli/environments.json",
}
for _, location := range possible_locations {
if checkForFile(location) {
return location
}
}
logger.Fatal("environments.json not found")
return ""
}
func checkForFile(file string) bool {
_, err := os.Stat(file)
return !os.IsNotExist(err)
}