Skip to content

Commit

Permalink
code: more detailed configuration checking
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlancehu committed Jul 17, 2023
1 parent 775e65b commit 4652759
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

var (
version = "Development"
version = "Dev"
ua = "QCIP/" + version
httpClient = &http.Client{
Timeout: time.Second * 10,
Expand All @@ -44,7 +44,7 @@ type IPResponse struct {
}

func main() {
fmt.Printf("QCIP \033[32mv%s\033[0m\n", version)
fmt.Printf("QCIP \033[1;32mv%s\033[0m\n", version)
configData := getconfig()
maxRetries, _ := strconv.Atoi(configData.MaxRetries)
ip := getip(configData.GetIPAPI, int(maxRetries))
Expand All @@ -53,6 +53,7 @@ func main() {
} else if configData.MType == "cvm" {
cvmmain(configData, ip)
}
os.Exit(0)
}

// 轻量应用服务器主函数
Expand All @@ -67,10 +68,8 @@ func lhmain(configData Config, ip string) {
fmt.Printf("IP is different, start updating\n")
lhmodifyrules(credential, configData.InstanceRegion, configData.InstanceId, res)
fmt.Printf("Successfully modified the firewall rules\n")
os.Exit(1)
} else {
fmt.Printf("IP is the same\n")
os.Exit(1)
}
}

Expand Down Expand Up @@ -129,15 +128,28 @@ func getconfig() Config {
} else if configData.MType == "cvm" {
requiredKeys = []string{"SecretId", "SecretKey", "SecurityGroupId", "SecurityGroupRegion", "Rules"}
} else {
fmt.Printf("Error machine type: %s\n", configData.MType)
os.Exit(1)
if configData.MType == "" {
fmt.Println("Machine type is empty")
os.Exit(1)
} else {
fmt.Printf("Error machine type: %s\n", configData.MType)
os.Exit(1)
}
}
checkPassing := true
for _, key := range requiredKeys {
if _, ok := reflect.TypeOf(configData).FieldByName(key); !ok {
fmt.Println(key + " not found in config file")
checkPassing = false
}
if reflect.ValueOf(configData).FieldByName(key).String() == "" {
fmt.Println(key + " is empty")
checkPassing = false
}
if reflect.ValueOf(configData).FieldByName(key).String() == key {
fmt.Println(key + " is incorrect")
checkPassing = false
}
}
if !checkPassing {
os.Exit(1)
Expand Down

0 comments on commit 4652759

Please sign in to comment.