mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
feat: allow setting config and connection with UNCLOUD_CONFIG and UNCLOUD_CONNECT env vars
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// ExpandCommaSeparatedValues takes a slice of strings and expands any comma-separated values into individual elements.
|
||||
@@ -21,3 +25,12 @@ func ExpandCommaSeparatedValues(values []string) []string {
|
||||
|
||||
return expanded
|
||||
}
|
||||
|
||||
// BindEnvToFlag assigns the value of an environment variable to the given command flag if the flag has not been set.
|
||||
func BindEnvToFlag(cmd *cobra.Command, flagName, envVar string) {
|
||||
if value := os.Getenv(envVar); value != "" && !cmd.Flags().Changed(flagName) {
|
||||
if err := cmd.Flags().Set(flagName, value); err != nil {
|
||||
log.Fatalf("Failed to bind environment variable '%s' to flag '%s': %v", envVar, flagName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user