Browse Source

Config: ability to mark field as required

Volodymyr Tkach 2 years ago
parent
commit
f29a707101
1 changed files with 30 additions and 0 deletions
  1. 30 0
      utils/penv/penv.go

+ 30 - 0
utils/penv/penv.go

@@ -51,6 +51,24 @@ func generateFlagName(name string) string {
 	return strings.ToLower(generate(name))
 	return strings.ToLower(generate(name))
 }
 }
 
 
+func isEnvPassed(name string) bool {
+	if _, ok := os.LookupEnv(name); ok {
+		return true
+	}
+	return false
+}
+
+func isFlagPassed(name string) bool {
+	res := false
+	flag.Visit(func(f *flag.Flag) {
+		if f.Name == name {
+			res = true
+			return
+		}
+	})
+	return res
+}
+
 func stringToInt(value string) (int, error) {
 func stringToInt(value string) (int, error) {
 	return strconv.Atoi(value)
 	return strconv.Atoi(value)
 }
 }
@@ -188,5 +206,17 @@ func ProcessConfig(config any) error {
 		}
 		}
 	}
 	}
 
 
+	// Required
+	for i := 0; i < t.NumField(); i++ {
+		nameEnv := generateEnvName(t.Field(i).Name)
+		nameFlag := generateFlagName(t.Field(i).Name)
+		required := t.Field(i).Tag.Get("required")
+		if required == "1" || required == "true" {
+			if !(isEnvPassed(nameEnv) || isFlagPassed(nameFlag)) {
+				return fmt.Errorf("variable '" + nameEnv + "' or flag '" + nameFlag + "' is not set")
+			}
+		}
+	}
+
 	return nil
 	return nil
 }
 }