-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsutils.go
103 lines (85 loc) · 3.06 KB
/
csutils.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
package csutils
import (
"bufio"
"encoding/json"
"fmt"
"os"
"strings"
)
// CloudSecureInfo struct holds the API credentials and tenant ID
type CloudSecureInfo struct {
APIKey string `json:"apiKey"`
APISecret string `json:"apiSecret"`
TenantID string `json:"tenantID"`
}
// CloudSecureConfig struct holds the configuration
type CloudSecureConfig struct {
CloudSecures map[string]CloudSecureInfo `json:"cloudsecures"`
DefaultCloudName string `json:"default_cloud_name"`
}
// LoadOrCreateCloudSecureConfig loads or creates the configuration
func LoadOrCreateCloudSecureConfig(configFile string) (CloudSecureConfig, error) {
var config CloudSecureConfig
// Check if the specified config file exists
if _, err := os.Stat(configFile); os.IsNotExist(err) {
fmt.Println("Configuration file not found, please provide the details for the first CloudSecure:")
config.CloudSecures = make(map[string]CloudSecureInfo)
cloudSecureInfo := CreateNewCloudSecureInfo()
fmt.Print("CloudSecure Name: ")
reader := bufio.NewReader(os.Stdin)
cloudSecureName, _ := reader.ReadString('\n')
cloudSecureName = strings.TrimSpace(cloudSecureName)
config.CloudSecures[cloudSecureName] = cloudSecureInfo
config.DefaultCloudName = cloudSecureName
SaveCloudSecureConfig(configFile, config)
fmt.Println("Configuration saved to", configFile)
} else {
configData, err := os.ReadFile(configFile)
if err != nil {
return config, err
}
json.Unmarshal(configData, &config)
}
// Check if any CloudSecure is missing or if default CloudSecure name is not set
if len(config.CloudSecures) == 0 || config.DefaultCloudName == "" {
fmt.Println("Invalid configuration. Adding a new CloudSecure:")
cloudSecureInfo := CreateNewCloudSecureInfo()
fmt.Print("CloudSecure Name: ")
reader := bufio.NewReader(os.Stdin)
cloudSecureName, _ := reader.ReadString('\n')
cloudSecureName = strings.TrimSpace(cloudSecureName)
if config.CloudSecures == nil {
config.CloudSecures = make(map[string]CloudSecureInfo)
}
config.CloudSecures[cloudSecureName] = cloudSecureInfo
if config.DefaultCloudName == "" {
config.DefaultCloudName = cloudSecureName
}
SaveCloudSecureConfig(configFile, config)
fmt.Println("Updated and saved configuration to", configFile)
}
return config, nil
}
// CreateNewCloudSecureInfo prompts the user for CloudSecure information
func CreateNewCloudSecureInfo() CloudSecureInfo {
reader := bufio.NewReader(os.Stdin)
fmt.Print("API Key: ")
apiKey, _ := reader.ReadString('\n')
fmt.Print("API Secret: ")
apiSecret, _ := reader.ReadString('\n')
fmt.Print("Tenant ID: ")
tenantID, _ := reader.ReadString('\n')
return CloudSecureInfo{
APIKey: strings.TrimSpace(apiKey),
APISecret: strings.TrimSpace(apiSecret),
TenantID: strings.TrimSpace(tenantID),
}
}
// SaveCloudSecureConfig saves the configuration to a JSON file
func SaveCloudSecureConfig(configFile string, config CloudSecureConfig) error {
configData, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
return os.WriteFile(configFile, configData, 0644)
}