Unexport the remaining vars in hugo command

This commit is contained in:
Bjørn Erik Pedersen 2016-02-05 22:58:17 +01:00
parent a89b2f41d6
commit 74539dea87
6 changed files with 44 additions and 36 deletions

View file

@ -74,7 +74,7 @@ func init() {
convertCmd.AddCommand(toTOMLCmd) convertCmd.AddCommand(toTOMLCmd)
convertCmd.AddCommand(toYAMLCmd) convertCmd.AddCommand(toYAMLCmd)
convertCmd.PersistentFlags().StringVarP(&outputDir, "output", "o", "", "filesystem path to write files to") convertCmd.PersistentFlags().StringVarP(&outputDir, "output", "o", "", "filesystem path to write files to")
convertCmd.PersistentFlags().StringVarP(&Source, "source", "s", "", "filesystem path to read files relative from") convertCmd.PersistentFlags().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from")
convertCmd.PersistentFlags().BoolVar(&unsafe, "unsafe", false, "enable less safe operations, please backup first") convertCmd.PersistentFlags().BoolVar(&unsafe, "unsafe", false, "enable less safe operations, please backup first")
convertCmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{}) convertCmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
} }

View file

@ -137,10 +137,18 @@ var (
verbose bool verbose bool
verboseLog bool verboseLog bool
buildWatch bool buildWatch bool
forceSync bool
) )
var forceSync bool var (
var Source, CacheDir, Destination, Theme, BaseURL, CfgFile, LogFile string baseURL string
cacheDir string
cfgFile string
destination string
logFile string
theme string
source string
)
// Execute adds all child commands to the root command HugoCmd and sets flags appropriately. // Execute adds all child commands to the root command HugoCmd and sets flags appropriately.
func Execute() { func Execute() {
@ -189,7 +197,7 @@ func initHugoBuilderFlags(cmd *cobra.Command) {
// initCoreCommonFlags initializes common flags used by Hugo core commands // initCoreCommonFlags initializes common flags used by Hugo core commands
// such as hugo itself, server, check, config and benchmark. // such as hugo itself, server, check, config and benchmark.
func initCoreCommonFlags(cmd *cobra.Command) { func initCoreCommonFlags(cmd *cobra.Command) {
cmd.Flags().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)")
// For bash-completion // For 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) cmd.Flags().SetAnnotation("config", cobra.BashCompFilenameExt, validConfigFilenames)
@ -207,14 +215,14 @@ func initHugoBuildCommonFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&disableRSS, "disableRSS", false, "Do not build RSS files") cmd.Flags().BoolVar(&disableRSS, "disableRSS", false, "Do not build RSS files")
cmd.Flags().BoolVar(&disableSitemap, "disableSitemap", false, "Do not build Sitemap file") cmd.Flags().BoolVar(&disableSitemap, "disableSitemap", false, "Do not build Sitemap file")
cmd.Flags().BoolVar(&disableRobotsTXT, "disableRobotsTXT", false, "Do not build Robots TXT file") cmd.Flags().BoolVar(&disableRobotsTXT, "disableRobotsTXT", false, "Do not build Robots TXT file")
cmd.Flags().StringVarP(&Source, "source", "s", "", "filesystem path to read files relative from") cmd.Flags().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from")
cmd.Flags().StringVarP(&CacheDir, "cacheDir", "", "", "filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/") cmd.Flags().StringVarP(&cacheDir, "cacheDir", "", "", "filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/")
cmd.Flags().BoolVarP(&ignoreCache, "ignoreCache", "", false, "Ignores the cache directory for reading but still writes to it") cmd.Flags().BoolVarP(&ignoreCache, "ignoreCache", "", false, "Ignores the cache directory for reading but still writes to it")
cmd.Flags().StringVarP(&Destination, "destination", "d", "", "filesystem path to write files to") cmd.Flags().StringVarP(&destination, "destination", "d", "", "filesystem path to write files to")
cmd.Flags().StringVarP(&Theme, "theme", "t", "", "theme to use (located in /themes/THEMENAME/)") cmd.Flags().StringVarP(&theme, "theme", "t", "", "theme to use (located in /themes/THEMENAME/)")
cmd.Flags().BoolVar(&uglyURLs, "uglyURLs", false, "if true, use /filename.html instead of /filename/") cmd.Flags().BoolVar(&uglyURLs, "uglyURLs", false, "if true, use /filename.html instead of /filename/")
cmd.Flags().BoolVar(&canonifyURLs, "canonifyURLs", false, "if true, all relative URLs will be canonicalized using baseURL") cmd.Flags().BoolVar(&canonifyURLs, "canonifyURLs", false, "if true, all relative URLs will be canonicalized using baseURL")
cmd.Flags().StringVarP(&BaseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. http://spf13.com/") cmd.Flags().StringVarP(&baseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. http://spf13.com/")
cmd.Flags().BoolVar(&nitro.AnalysisOn, "stepAnalysis", false, "display memory and timing of different steps of the program") cmd.Flags().BoolVar(&nitro.AnalysisOn, "stepAnalysis", false, "display memory and timing of different steps of the program")
cmd.Flags().BoolVar(&pluralizeListTitles, "pluralizeListTitles", true, "Pluralize titles in lists using inflect") cmd.Flags().BoolVar(&pluralizeListTitles, "pluralizeListTitles", true, "Pluralize titles in lists using inflect")
@ -232,7 +240,7 @@ func initBenchmarkBuildingFlags(cmd *cobra.Command) {
func init() { func init() {
hugoCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output") hugoCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
hugoCmd.PersistentFlags().BoolVar(&logging, "log", false, "Enable Logging") hugoCmd.PersistentFlags().BoolVar(&logging, "log", false, "Enable Logging")
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")
initHugoBuilderFlags(hugoCmd) initHugoBuilderFlags(hugoCmd)
@ -296,12 +304,12 @@ func LoadDefaultSettings() {
// A Hugo command that calls initCoreCommonFlags() can pass itself // A Hugo command that calls initCoreCommonFlags() can pass itself
// as an argument to have its command-line flags processed here. // as an argument to have its command-line flags processed here.
func InitializeConfig(subCmdVs ...*cobra.Command) error { func InitializeConfig(subCmdVs ...*cobra.Command) error {
viper.SetConfigFile(CfgFile) viper.SetConfigFile(cfgFile)
// See https://github.com/spf13/viper/issues/73#issuecomment-126970794 // See https://github.com/spf13/viper/issues/73#issuecomment-126970794
if Source == "" { if source == "" {
viper.AddConfigPath(".") viper.AddConfigPath(".")
} else { } else {
viper.AddConfigPath(Source) viper.AddConfigPath(source)
} }
err := viper.ReadInConfig() err := viper.ReadInConfig()
if err != nil { if err != nil {
@ -322,7 +330,7 @@ func InitializeConfig(subCmdVs ...*cobra.Command) error {
viper.Set("Verbose", verbose) viper.Set("Verbose", verbose)
} }
if flagChanged(cmdV.PersistentFlags(), "logFile") { if flagChanged(cmdV.PersistentFlags(), "logFile") {
viper.Set("LogFile", LogFile) viper.Set("LogFile", logFile)
} }
if flagChanged(cmdV.Flags(), "cleanDestinationDir") { if flagChanged(cmdV.Flags(), "cleanDestinationDir") {
viper.Set("cleanDestinationDir", cleanDestination) viper.Set("cleanDestinationDir", cleanDestination)
@ -366,43 +374,43 @@ func InitializeConfig(subCmdVs ...*cobra.Command) error {
} }
if BaseURL != "" { if baseURL != "" {
if !strings.HasSuffix(BaseURL, "/") { if !strings.HasSuffix(baseURL, "/") {
BaseURL = BaseURL + "/" baseURL = baseURL + "/"
} }
viper.Set("BaseURL", BaseURL) viper.Set("BaseURL", baseURL)
} }
if !viper.GetBool("RelativeURLs") && viper.GetString("BaseURL") == "" { if !viper.GetBool("RelativeURLs") && viper.GetString("BaseURL") == "" {
jww.ERROR.Println("No 'baseurl' set in configuration or as a flag. Features like page menus will not work without one.") jww.ERROR.Println("No 'baseurl' set in configuration or as a flag. Features like page menus will not work without one.")
} }
if Theme != "" { if theme != "" {
viper.Set("theme", Theme) viper.Set("theme", theme)
} }
if Destination != "" { if destination != "" {
viper.Set("PublishDir", Destination) viper.Set("PublishDir", destination)
} }
if Source != "" { if source != "" {
dir, _ := filepath.Abs(Source) dir, _ := filepath.Abs(source)
viper.Set("WorkingDir", dir) viper.Set("WorkingDir", dir)
} else { } else {
dir, _ := os.Getwd() dir, _ := os.Getwd()
viper.Set("WorkingDir", dir) viper.Set("WorkingDir", dir)
} }
if CacheDir != "" { if cacheDir != "" {
if helpers.FilePathSeparator != CacheDir[len(CacheDir)-1:] { if helpers.FilePathSeparator != cacheDir[len(cacheDir)-1:] {
CacheDir = CacheDir + helpers.FilePathSeparator cacheDir = cacheDir + helpers.FilePathSeparator
} }
isDir, err := helpers.DirExists(CacheDir, hugofs.SourceFs) isDir, err := helpers.DirExists(cacheDir, hugofs.SourceFs)
utils.CheckErr(err) utils.CheckErr(err)
if isDir == false { if isDir == false {
mkdir(CacheDir) mkdir(cacheDir)
} }
viper.Set("CacheDir", CacheDir) viper.Set("CacheDir", cacheDir)
} else { } else {
viper.Set("CacheDir", helpers.GetTempDir("hugo_cache", hugofs.SourceFs)) viper.Set("CacheDir", helpers.GetTempDir("hugo_cache", hugofs.SourceFs))
} }

View file

@ -25,7 +25,7 @@ import (
func init() { func init() {
listCmd.AddCommand(listDraftsCmd) listCmd.AddCommand(listDraftsCmd)
listCmd.AddCommand(listFutureCmd) listCmd.AddCommand(listFutureCmd)
listCmd.PersistentFlags().StringVarP(&Source, "source", "s", "", "filesystem path to read files relative from") listCmd.PersistentFlags().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from")
listCmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{}) listCmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
} }

View file

@ -44,7 +44,7 @@ func init() {
newSiteCmd.Flags().Bool("force", false, "Init inside non-empty directory") newSiteCmd.Flags().Bool("force", false, "Init inside non-empty directory")
newCmd.Flags().StringVarP(&configFormat, "format", "f", "toml", "frontmatter format") newCmd.Flags().StringVarP(&configFormat, "format", "f", "toml", "frontmatter format")
newCmd.Flags().StringVarP(&contentType, "kind", "k", "", "Content type to create") newCmd.Flags().StringVarP(&contentType, "kind", "k", "", "Content type to create")
newCmd.PersistentFlags().StringVarP(&Source, "source", "s", "", "filesystem path to read files relative from") newCmd.PersistentFlags().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from")
newCmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{}) newCmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
newCmd.Flags().StringVar(&contentEditor, "editor", "", "edit new content with this editor, if provided") newCmd.Flags().StringVar(&contentEditor, "editor", "", "edit new content with this editor, if provided")

View file

@ -128,7 +128,7 @@ func server(cmd *cobra.Command, args []string) error {
viper.Set("port", serverPort) viper.Set("port", serverPort)
BaseURL, err := fixURL(viper.GetString("BaseURL")) BaseURL, err := fixURL(baseURL)
if err != nil { if err != nil {
return err return err
} }
@ -139,7 +139,7 @@ func server(cmd *cobra.Command, args []string) error {
} }
// If a Destination is provided via flag write to disk // If a Destination is provided via flag write to disk
if Destination != "" { if destination != "" {
renderToDisk = true renderToDisk = true
} }

View file

@ -44,11 +44,11 @@ func TestFixURL(t *testing.T) {
for i, test := range tests { for i, test := range tests {
viper.Reset() viper.Reset()
BaseURL = test.CLIBaseURL baseURL = test.CLIBaseURL
viper.Set("BaseURL", test.CfgBaseURL) viper.Set("BaseURL", test.CfgBaseURL)
serverAppend = test.AppendPort serverAppend = test.AppendPort
serverPort = test.Port serverPort = test.Port
result, err := fixURL(BaseURL) result, err := fixURL(baseURL)
if err != nil { if err != nil {
t.Errorf("Test #%d %s: unexpected error %s", i, test.TestName, err) t.Errorf("Test #%d %s: unexpected error %s", i, test.TestName, err)
} }