1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/config/flags.go
Oliver Nocon c1eb9f5c70
Provide first parts for golang implementation (#905)
* Provide first parts for golang implementation
2019-10-22 15:41:27 +02:00

44 lines
1.3 KiB
Go

package config
import (
"fmt"
"os"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
)
// AvailableFlagValues returns all flags incl. values which are available to the command.
func AvailableFlagValues(cmd *cobra.Command, filters *StepFilters) map[string]interface{} {
flagValues := map[string]interface{}{}
flags := cmd.Flags()
//only check flags where value has been set
flags.Visit(func(pflag *flag.Flag) {
switch pflag.Value.Type() {
case "string":
flagValues[pflag.Name] = pflag.Value.String()
case "stringSlice":
flagValues[pflag.Name], _ = flags.GetStringSlice(pflag.Name)
case "bool":
flagValues[pflag.Name], _ = flags.GetBool(pflag.Name)
default:
fmt.Printf("Meta data type not set or not known: '%v'\n", pflag.Value.Type())
os.Exit(1)
}
filters.Parameters = append(filters.Parameters, pflag.Name)
})
return flagValues
}
// MarkFlagsWithValue marks a flag as changed if value is available for the flag through the step configuration.
func MarkFlagsWithValue(cmd *cobra.Command, stepConfig StepConfig) {
flags := cmd.Flags()
flags.VisitAll(func(pflag *flag.Flag) {
//mark as available in case default is available or config is available
if len(pflag.Value.String()) > 0 || stepConfig.Config[pflag.Name] != nil {
pflag.Changed = true
}
})
}