Make config flag global

Fixes #2261
This commit is contained in:
Bjørn Erik Pedersen 2016-07-07 18:06:48 +02:00
parent 223073c6fd
commit b581bbd856
2 changed files with 7 additions and 10 deletions

View file

@ -198,17 +198,15 @@ func AddCommands() {
// initHugoBuilderFlags initializes all common flags, typically used by the // initHugoBuilderFlags initializes all common flags, typically used by the
// core build commands, namely hugo itself, server, check and benchmark. // core build commands, namely hugo itself, server, check and benchmark.
func initHugoBuilderFlags(cmd *cobra.Command) { func initHugoBuilderFlags(cmd *cobra.Command) {
initCoreCommonFlags(cmd)
initHugoBuildCommonFlags(cmd) initHugoBuildCommonFlags(cmd)
} }
// initCoreCommonFlags initializes common flags used by Hugo core commands. func initRootPersistentFlags() {
func initCoreCommonFlags(cmd *cobra.Command) { HugoCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is path/config.yaml|json|toml)")
cmd.Flags().StringVar(&cfgFile, "config", "", "config file (default is path/config.yaml|json|toml)")
// Set bash-completion // Set bash-completion
validConfigFilenames := []string{"json", "js", "yaml", "yml", "toml", "tml"} validConfigFilenames := []string{"json", "js", "yaml", "yml", "toml", "tml"}
cmd.Flags().SetAnnotation("config", cobra.BashCompFilenameExt, validConfigFilenames) HugoCmd.PersistentFlags().SetAnnotation("config", cobra.BashCompFilenameExt, validConfigFilenames)
} }
// initHugoBuildCommonFlags initialize common flags related to the Hugo build. // initHugoBuildCommonFlags initialize common flags related to the Hugo build.
@ -257,6 +255,7 @@ func init() {
HugoCmd.PersistentFlags().StringVar(&logFile, "logFile", "", "Log File path (if set, logging enabled automatically)") HugoCmd.PersistentFlags().StringVar(&logFile, "logFile", "", "Log File path (if set, logging enabled automatically)")
HugoCmd.PersistentFlags().BoolVar(&verboseLog, "verboseLog", false, "verbose logging") HugoCmd.PersistentFlags().BoolVar(&verboseLog, "verboseLog", false, "verbose logging")
initRootPersistentFlags()
initHugoBuilderFlags(HugoCmd) initHugoBuilderFlags(HugoCmd)
initBenchmarkBuildingFlags(HugoCmd) initBenchmarkBuildingFlags(HugoCmd)
@ -319,8 +318,6 @@ func loadDefaultSettings() {
} }
// InitializeConfig initializes a config file with sensible default configuration flags. // InitializeConfig initializes a config file with sensible default configuration flags.
// A Hugo command that calls initCoreCommonFlags() can pass itself
// as an argument to have its command-line flags processed here.
func InitializeConfig(subCmdVs ...*cobra.Command) error { func InitializeConfig(subCmdVs ...*cobra.Command) error {
viper.AutomaticEnv() viper.AutomaticEnv()
viper.SetEnvPrefix("hugo") viper.SetEnvPrefix("hugo")

View file

@ -15,10 +15,11 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"reflect" "reflect"
"sort" "sort"
"github.com/spf13/cobra"
"github.com/spf13/viper"
) )
var configCmd = &cobra.Command{ var configCmd = &cobra.Command{
@ -28,7 +29,6 @@ var configCmd = &cobra.Command{
} }
func init() { func init() {
initCoreCommonFlags(configCmd)
configCmd.RunE = config configCmd.RunE = config
} }