-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: avoid flag name colision in viper
Signed-off-by: Kaitian Xie <kaitian96@outlook.com>
- Loading branch information
Showing
2 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cli | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type PreRunFunc func(cmd *cobra.Command, args []string) | ||
|
||
// BindPFlags accepts a list of flag names and binds the corresponding flags to Viper. This function servers | ||
// as a workaround to a known bug in Viper, in which two commands can't share the same flag name. | ||
// To learn more about the bug, see: https://github.com/spf13/viper/issues/233 | ||
func BindPFlags(flagNames []string) PreRunFunc { | ||
return func(cmd *cobra.Command, args []string) { | ||
for _, name := range flagNames { | ||
if err := viper.BindPFlag(name, cmd.Flags().Lookup(name)); err != nil { | ||
log.Fatalf("Failed to bind flag %s: %s", name, err) | ||
} | ||
} | ||
} | ||
} |