From 831356cabc3a9ccd7e45bc85ad7435e222a1640a Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 17:26:45 +0000 Subject: [PATCH 01/16] Create api.go --- api/api.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 api/api.go diff --git a/api/api.go b/api/api.go new file mode 100644 index 00000000000..139cf7755d5 --- /dev/null +++ b/api/api.go @@ -0,0 +1,15 @@ +package api + +import ( + "github.com/spf13/hugo/commands" + "github.com/spf13/viper" +) + +func Run(flags []string) { + commands.Execute(flags) +} + +func Reset() { + commands.ClearSite() + viper.Reset() +} From 239bbe2d1a7198eba35a08105f3010c2dcb198b1 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 17:27:31 +0000 Subject: [PATCH 02/16] Update main.go --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index f3b14d976a3..484a0698906 100644 --- a/main.go +++ b/main.go @@ -21,5 +21,5 @@ import ( func main() { runtime.GOMAXPROCS(runtime.NumCPU()) - commands.Execute() + commands.Execute(nil) } From 3e715b809daad8a76dd616ce354cfeb3cf6e7ccd Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 17:27:53 +0000 Subject: [PATCH 03/16] Update benchmark.go --- commands/benchmark.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/benchmark.go b/commands/benchmark.go index d10289be4ec..6384c191228 100644 --- a/commands/benchmark.go +++ b/commands/benchmark.go @@ -57,7 +57,7 @@ func benchmark(cmd *cobra.Command, args []string) error { return err } for i := 0; i < benchmarkTimes; i++ { - MainSite = nil + mainSite = nil _ = buildSite() } pprof.WriteHeapProfile(f) @@ -76,7 +76,7 @@ func benchmark(cmd *cobra.Command, args []string) error { pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() for i := 0; i < benchmarkTimes; i++ { - MainSite = nil + mainSite = nil _ = buildSite() } } From 9d483a4f43f74331a1f0c7cf8178d50ec1aa64f5 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 17:28:08 +0000 Subject: [PATCH 04/16] Update hugo.go --- commands/hugo.go | 93 ++++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/commands/hugo.go b/commands/hugo.go index 83dae0d118d..9d69f1534fe 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -46,7 +46,7 @@ import ( "gopkg.in/fsnotify.v1" ) -var MainSite *hugolib.Site +var mainSite *hugolib.Site // userError is an error used to signal different error situations in command handling. type commandError struct { @@ -91,9 +91,9 @@ func isUserError(err error) bool { return userErrorRegexp.MatchString(err.Error()) } -// HugoCmd is Hugo's root command. -// Every other command attached to HugoCmd is a child command to it. -var HugoCmd = &cobra.Command{ +// hugoCmd is Hugo's root command. +// Every other command attached to hugoCmd is a child command to it. +var hugoCmd = &cobra.Command{ Use: "hugo", Short: "hugo builds your site", Long: `hugo is the main command, used to build your Hugo site. @@ -150,15 +150,26 @@ var ( source string ) -// Execute adds all child commands to the root command HugoCmd and sets flags appropriately. -func Execute() { - HugoCmd.SetGlobalNormalizationFunc(helpers.NormalizeHugoFlags) +// ClearSite sets mainSite to nil +func ClearSite() { + mainSite = nil +} + +// Execute adds all child commands to the root command hugoCmd and sets flags appropriately. +func Execute(flags []string) { + hugoCmd.SetGlobalNormalizationFunc(helpers.NormalizeHugoFlags) - HugoCmd.SilenceUsage = true + hugoCmd.SilenceUsage = true AddCommands() - if c, err := HugoCmd.ExecuteC(); err != nil { + if flags != nil { + if err := hugoCmd.ParseFlags(flags); err != nil { + os.Exit(-1) + } + } + + if c, err := hugoCmd.ExecuteC(); err != nil { if isUserError(err) { c.Println("") c.Println(c.UsageString()) @@ -168,20 +179,20 @@ func Execute() { } } -// AddCommands adds child commands to the root command HugoCmd. +// AddCommands adds child commands to the root command hugoCmd. func AddCommands() { - HugoCmd.AddCommand(serverCmd) - HugoCmd.AddCommand(versionCmd) - HugoCmd.AddCommand(configCmd) - HugoCmd.AddCommand(checkCmd) - HugoCmd.AddCommand(benchmarkCmd) - HugoCmd.AddCommand(convertCmd) - HugoCmd.AddCommand(newCmd) - HugoCmd.AddCommand(listCmd) - HugoCmd.AddCommand(undraftCmd) - HugoCmd.AddCommand(importCmd) - - HugoCmd.AddCommand(genCmd) + hugoCmd.AddCommand(serverCmd) + hugoCmd.AddCommand(versionCmd) + hugoCmd.AddCommand(configCmd) + hugoCmd.AddCommand(checkCmd) + hugoCmd.AddCommand(benchmarkCmd) + hugoCmd.AddCommand(convertCmd) + hugoCmd.AddCommand(newCmd) + hugoCmd.AddCommand(listCmd) + hugoCmd.AddCommand(undraftCmd) + hugoCmd.AddCommand(importCmd) + + hugoCmd.AddCommand(genCmd) genCmd.AddCommand(genautocompleteCmd) genCmd.AddCommand(gendocCmd) genCmd.AddCommand(genmanCmd) @@ -241,19 +252,19 @@ func initBenchmarkBuildingFlags(cmd *cobra.Command) { // init initializes flags. func init() { - HugoCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output") - HugoCmd.PersistentFlags().BoolVar(&logging, "log", false, "Enable Logging") - HugoCmd.PersistentFlags().StringVar(&logFile, "logFile", "", "Log File path (if set, logging enabled automatically)") - HugoCmd.PersistentFlags().BoolVar(&verboseLog, "verboseLog", false, "verbose logging") + hugoCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output") + hugoCmd.PersistentFlags().BoolVar(&logging, "log", false, "Enable Logging") + hugoCmd.PersistentFlags().StringVar(&logFile, "logFile", "", "Log File path (if set, logging enabled automatically)") + hugoCmd.PersistentFlags().BoolVar(&verboseLog, "verboseLog", false, "verbose logging") - initHugoBuilderFlags(HugoCmd) - initBenchmarkBuildingFlags(HugoCmd) + initHugoBuilderFlags(hugoCmd) + initBenchmarkBuildingFlags(hugoCmd) - HugoCmd.Flags().BoolVarP(&buildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed") - hugoCmdV = HugoCmd + hugoCmd.Flags().BoolVarP(&buildWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed") + hugoCmdV = hugoCmd // Set bash-completion - HugoCmd.PersistentFlags().SetAnnotation("logFile", cobra.BashCompFilenameExt, []string{}) + hugoCmd.PersistentFlags().SetAnnotation("logFile", cobra.BashCompFilenameExt, []string{}) } func LoadDefaultSettings() { @@ -318,9 +329,9 @@ func InitializeConfig(subCmdVs ...*cobra.Command) error { if err != nil { if _, ok := err.(viper.ConfigParseError); ok { return newSystemError(err) - } else { - return newSystemErrorF("Unable to locate Config file. Perhaps you need to create a new site.\n Run `hugo help new` for details. (%s)\n", err) } + + return newSystemErrorF("Unable to locate Config file. Perhaps you need to create a new site.\n Run `hugo help new` for details. (%s)\n", err) } viper.RegisterAlias("indexes", "taxonomies") @@ -467,8 +478,6 @@ func watchConfig() { viper.WatchConfig() viper.OnConfigChange(func(e fsnotify.Event) { fmt.Println("Config file changed:", e.Name) - // Force a full rebuild - MainSite = nil utils.CheckErr(buildSite(true)) if !viper.GetBool("DisableLiveReload") { // Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized @@ -652,17 +661,17 @@ func getDirList() []string { func buildSite(watching ...bool) (err error) { fmt.Println("Started building site") startTime := time.Now() - if MainSite == nil { - MainSite = new(hugolib.Site) + if mainSite == nil { + mainSite = new(hugolib.Site) } if len(watching) > 0 && watching[0] { - MainSite.RunMode.Watching = true + mainSite.RunMode.Watching = true } - err = MainSite.Build() + err = mainSite.Build() if err != nil { return err } - MainSite.Stats() + mainSite.Stats() jww.FEEDBACK.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds())) return nil @@ -670,11 +679,11 @@ func buildSite(watching ...bool) (err error) { func rebuildSite(events []fsnotify.Event) error { startTime := time.Now() - err := MainSite.ReBuild(events) + err := mainSite.ReBuild(events) if err != nil { return err } - MainSite.Stats() + mainSite.Stats() jww.FEEDBACK.Printf("in %v ms\n", int(1000*time.Since(startTime).Seconds())) return nil From 60febd7997aec224acabc1c603b831eced32c95e Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 17:28:37 +0000 Subject: [PATCH 05/16] Update api.go --- api/api.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/api/api.go b/api/api.go index 139cf7755d5..0e9eb3d2008 100644 --- a/api/api.go +++ b/api/api.go @@ -1,3 +1,16 @@ +// Copyright 2015 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package api import ( From ee0cd6c1f05f21ef9bcf1ee2d869bc7e855d5f6e Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 17:35:07 +0000 Subject: [PATCH 06/16] Update api.go --- api/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/api.go b/api/api.go index 0e9eb3d2008..0bbc2c9f983 100644 --- a/api/api.go +++ b/api/api.go @@ -1,4 +1,4 @@ -// Copyright 2015 The Hugo Authors. All rights reserved. +// Copyright 2016 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. From 0c37532b34d14abf46108691a134daa07e274b75 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 17:40:34 +0000 Subject: [PATCH 07/16] Update api.go --- api/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/api.go b/api/api.go index 0bbc2c9f983..32a14c96e98 100644 --- a/api/api.go +++ b/api/api.go @@ -18,7 +18,7 @@ import ( "github.com/spf13/viper" ) -func Run(flags []string) { +func Build(flags []string) { commands.Execute(flags) } From 64d7b5c939d28fda3b11ca7afc25a611ddfdfab7 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 18:24:11 +0000 Subject: [PATCH 08/16] Add a NewSite function --- api/api.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/api/api.go b/api/api.go index 32a14c96e98..5b394b6ce21 100644 --- a/api/api.go +++ b/api/api.go @@ -14,10 +14,28 @@ package api import ( + "github.com/spf13/cobra" "github.com/spf13/hugo/commands" "github.com/spf13/viper" ) +func NewSite(path string, force bool, format string) { + cmd := &cobra.Command{} + + if &force == nil { + force = false + } + + cmd.Flags().Bool("force", true, "") + + if &format == nil { + format = "toml" + } + + cmd.Flags().String("format", format, "") + commands.NewSite(cmd, []string{path}) +} + func Build(flags []string) { commands.Execute(flags) } From 9e23358bf6e3f9606680244004ac97adde29c2be Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 19:05:51 +0000 Subject: [PATCH 09/16] add error to api.Build() --- api/api.go | 4 ++-- commands/hugo.go | 6 ++++-- main.go | 5 ++++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/api/api.go b/api/api.go index 5b394b6ce21..1b641a28a9f 100644 --- a/api/api.go +++ b/api/api.go @@ -36,8 +36,8 @@ func NewSite(path string, force bool, format string) { commands.NewSite(cmd, []string{path}) } -func Build(flags []string) { - commands.Execute(flags) +func Build(flags []string) error { + return commands.Execute(flags) } func Reset() { diff --git a/commands/hugo.go b/commands/hugo.go index 9d69f1534fe..d2f08d7cd27 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -156,7 +156,7 @@ func ClearSite() { } // Execute adds all child commands to the root command hugoCmd and sets flags appropriately. -func Execute(flags []string) { +func Execute(flags []string) error { hugoCmd.SetGlobalNormalizationFunc(helpers.NormalizeHugoFlags) hugoCmd.SilenceUsage = true @@ -175,8 +175,10 @@ func Execute(flags []string) { c.Println(c.UsageString()) } - os.Exit(-1) + return err } + + return nil } // AddCommands adds child commands to the root command hugoCmd. diff --git a/main.go b/main.go index 484a0698906..1143e8b66c0 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ package main import ( + "os" "runtime" "github.com/spf13/hugo/commands" @@ -21,5 +22,7 @@ import ( func main() { runtime.GOMAXPROCS(runtime.NumCPU()) - commands.Execute(nil) + if err := commands.Execute(nil); err != nil { + os.Exit(-1) + } } From 8c17b7609aecc89beadbccb7c683aaaa21c4c774 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 19:16:56 +0000 Subject: [PATCH 10/16] change the behaviour of Execute function to adapt to the API and main.go --- api/api.go | 3 ++- commands/hugo.go | 13 +++++++------ main.go | 4 +++- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/api/api.go b/api/api.go index 1b641a28a9f..53b9778c39c 100644 --- a/api/api.go +++ b/api/api.go @@ -37,7 +37,8 @@ func NewSite(path string, force bool, format string) { } func Build(flags []string) error { - return commands.Execute(flags) + _, err := commands.Execute(flags) + return err } func Reset() { diff --git a/commands/hugo.go b/commands/hugo.go index d2f08d7cd27..03087c77635 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -156,7 +156,7 @@ func ClearSite() { } // Execute adds all child commands to the root command hugoCmd and sets flags appropriately. -func Execute(flags []string) error { +func Execute(flags []string) (string, error) { hugoCmd.SetGlobalNormalizationFunc(helpers.NormalizeHugoFlags) hugoCmd.SilenceUsage = true @@ -165,20 +165,21 @@ func Execute(flags []string) error { if flags != nil { if err := hugoCmd.ParseFlags(flags); err != nil { - os.Exit(-1) + return "", err } } if c, err := hugoCmd.ExecuteC(); err != nil { + message := "\n" + if isUserError(err) { - c.Println("") - c.Println(c.UsageString()) + message += c.UsageString() } - return err + return message, err } - return nil + return "", nil } // AddCommands adds child commands to the root command hugoCmd. diff --git a/main.go b/main.go index 1143e8b66c0..2a924242317 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ package main import ( + "fmt" "os" "runtime" @@ -22,7 +23,8 @@ import ( func main() { runtime.GOMAXPROCS(runtime.NumCPU()) - if err := commands.Execute(nil); err != nil { + if message, err := commands.Execute(nil); err != nil { + fmt.Println(message) os.Exit(-1) } } From 262cad23c8391e05da20dec06816545674593686 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 19:52:53 +0000 Subject: [PATCH 11/16] build updates and add set method --- api/api.go | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/api/api.go b/api/api.go index 53b9778c39c..63607e25340 100644 --- a/api/api.go +++ b/api/api.go @@ -19,6 +19,28 @@ import ( "github.com/spf13/viper" ) +// Build is the type that handles Building methods +type Build struct { + flags []string +} + +// Run regenerates the website +func (b *Build) Run() error { + _, err := commands.Execute(b.flags) + return err +} + +// Set adds a value to the flags array +func (b *Build) Set(key string, value interface{}) { + // If the key is something like '-b' or '--config' + if key[0] != '-' { + key = "--" + key + } + + b.flags = append([]string{key, value.(string)}, b.flags...) +} + +// NewSite generates a new site func NewSite(path string, force bool, format string) { cmd := &cobra.Command{} @@ -36,11 +58,8 @@ func NewSite(path string, force bool, format string) { commands.NewSite(cmd, []string{path}) } -func Build(flags []string) error { - _, err := commands.Execute(flags) - return err -} - +// Reset resets the current website and sets all settings to default. It can +// be useful if your application needs to run Hugo in different paths func Reset() { commands.ClearSite() viper.Reset() From d946b5ba2ba4f1d08c67378846680ff9f63b1b72 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 20:10:19 +0000 Subject: [PATCH 12/16] NewContent API --- api/api.go | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/api/api.go b/api/api.go index 63607e25340..2523abf47b4 100644 --- a/api/api.go +++ b/api/api.go @@ -32,12 +32,7 @@ func (b *Build) Run() error { // Set adds a value to the flags array func (b *Build) Set(key string, value interface{}) { - // If the key is something like '-b' or '--config' - if key[0] != '-' { - key = "--" + key - } - - b.flags = append([]string{key, value.(string)}, b.flags...) + b.flags = preppendFlag(b.flags, key, value.(string)) } // NewSite generates a new site @@ -58,9 +53,39 @@ func NewSite(path string, force bool, format string) { commands.NewSite(cmd, []string{path}) } +// NewContent is used to create new contents +type NewContent struct { + cmd *cobra.Command + path string +} + +// Set adds a value to the flags array +func (n *NewContent) Set(key string, value interface{}) { + if key == "path" { + n.path = key + return + } + + n.cmd.Flags().Set(key, value.(string)) +} + +// Make generates a new content +func (n *NewContent) Make(path string) { + commands.NewSite(n.cmd, []string{n.path}) +} + // Reset resets the current website and sets all settings to default. It can // be useful if your application needs to run Hugo in different paths func Reset() { commands.ClearSite() viper.Reset() } + +func preppendFlag(flags []string, key string, value string) []string { + // If the key doesn't begin with "-" + if key[0] != '-' { + key = "--" + key + } + + return append([]string{key, value}, flags...) +} From e37a9005fd7e99200b4b9a7603adeec2e17d4f0b Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 20:41:01 +0000 Subject: [PATCH 13/16] unifo. functions --- api/api.go | 85 ++++++++++++++++++++++++++++-------------------- commands/hugo.go | 2 +- commands/new.go | 24 +++++++------- 3 files changed, 63 insertions(+), 48 deletions(-) diff --git a/api/api.go b/api/api.go index 2523abf47b4..74c1802e44a 100644 --- a/api/api.go +++ b/api/api.go @@ -19,48 +19,52 @@ import ( "github.com/spf13/viper" ) +// Reset resets the current website and sets all settings to default. It can +// be useful if your application needs to run Hugo in different paths +func Reset() { + commands.ClearSite() + viper.Reset() +} + +func NewBuild() *build { + return new(build) +} + // Build is the type that handles Building methods -type Build struct { +type build struct { flags []string } // Run regenerates the website -func (b *Build) Run() error { +func (b *build) Run() error { _, err := commands.Execute(b.flags) return err } // Set adds a value to the flags array -func (b *Build) Set(key string, value interface{}) { - b.flags = preppendFlag(b.flags, key, value.(string)) -} - -// NewSite generates a new site -func NewSite(path string, force bool, format string) { - cmd := &cobra.Command{} - - if &force == nil { - force = false +func (b *build) Set(key string, value interface{}) { + // If the key doesn't begin with "-" + if key[0] != '-' { + key = "--" + key } - cmd.Flags().Bool("force", true, "") - - if &format == nil { - format = "toml" - } + b.flags = append([]string{key, value.(string)}, b.flags...) +} - cmd.Flags().String("format", format, "") - commands.NewSite(cmd, []string{path}) +func NewSite() *newSite { + n := new(newSite) + *n.cmd = *commands.NewSiteCmd + return n } -// NewContent is used to create new contents -type NewContent struct { +// NewSite creates new sites +type newSite struct { cmd *cobra.Command path string } // Set adds a value to the flags array -func (n *NewContent) Set(key string, value interface{}) { +func (n *newSite) Set(key string, value interface{}) { if key == "path" { n.path = key return @@ -69,23 +73,34 @@ func (n *NewContent) Set(key string, value interface{}) { n.cmd.Flags().Set(key, value.(string)) } -// Make generates a new content -func (n *NewContent) Make(path string) { - commands.NewSite(n.cmd, []string{n.path}) +// Make generates a new site +func (n *newSite) Make() error { + return n.cmd.RunE(n.cmd, []string{n.path}) } -// Reset resets the current website and sets all settings to default. It can -// be useful if your application needs to run Hugo in different paths -func Reset() { - commands.ClearSite() - viper.Reset() +func NewContent() *newContent { + n := new(newContent) + *n.cmd = *commands.NewCmd + return n } -func preppendFlag(flags []string, key string, value string) []string { - // If the key doesn't begin with "-" - if key[0] != '-' { - key = "--" + key +// NewContent is used to create new contents +type newContent struct { + cmd *cobra.Command + path string +} + +// Set adds a value to the flags array +func (n *newContent) Set(key string, value interface{}) { + if key == "path" { + n.path = key + return } - return append([]string{key, value}, flags...) + n.cmd.Flags().Set(key, value.(string)) +} + +// Make generates a new content +func (n *newContent) Make() error { + return n.cmd.RunE(n.cmd, []string{n.path}) } diff --git a/commands/hugo.go b/commands/hugo.go index 03087c77635..50fc12f351c 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -190,7 +190,7 @@ func AddCommands() { hugoCmd.AddCommand(checkCmd) hugoCmd.AddCommand(benchmarkCmd) hugoCmd.AddCommand(convertCmd) - hugoCmd.AddCommand(newCmd) + hugoCmd.AddCommand(NewCmd) hugoCmd.AddCommand(listCmd) hugoCmd.AddCommand(undraftCmd) hugoCmd.AddCommand(importCmd) diff --git a/commands/new.go b/commands/new.go index 2a1905ca8b6..281d33092c2 100644 --- a/commands/new.go +++ b/commands/new.go @@ -40,20 +40,20 @@ var ( ) func init() { - newSiteCmd.Flags().StringVarP(&configFormat, "format", "f", "toml", "config & frontmatter format") - newSiteCmd.Flags().Bool("force", false, "Init inside non-empty directory") - newCmd.Flags().StringVarP(&configFormat, "format", "f", "toml", "frontmatter format") - 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().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{}) - newCmd.Flags().StringVar(&contentEditor, "editor", "", "edit new content with this editor, if provided") + NewSiteCmd.Flags().StringVarP(&configFormat, "format", "f", "toml", "config & frontmatter format") + NewSiteCmd.Flags().Bool("force", false, "Init inside non-empty directory") + NewCmd.Flags().StringVarP(&configFormat, "format", "f", "toml", "frontmatter format") + 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().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{}) + NewCmd.Flags().StringVar(&contentEditor, "editor", "", "edit new content with this editor, if provided") - newCmd.AddCommand(newSiteCmd) - newCmd.AddCommand(newThemeCmd) + NewCmd.AddCommand(NewSiteCmd) + NewCmd.AddCommand(NewThemeCmd) } -var newCmd = &cobra.Command{ +var NewCmd = &cobra.Command{ Use: "new [path]", Short: "Create new content for your site", Long: `Create a new content file and automatically set the date and title. @@ -66,7 +66,7 @@ If archetypes are provided in your theme or site, they will be used.`, RunE: NewContent, } -var newSiteCmd = &cobra.Command{ +var NewSiteCmd = &cobra.Command{ Use: "site [path]", Short: "Create a new site (skeleton)", Long: `Create a new site in the provided directory. @@ -75,7 +75,7 @@ Use ` + "`hugo new [contentPath]`" + ` to create new content.`, RunE: NewSite, } -var newThemeCmd = &cobra.Command{ +var NewThemeCmd = &cobra.Command{ Use: "theme [name]", Short: "Create a new theme", Long: `Create a new theme (skeleton) called [name] in the current directory. From 92df60747d2135b88466fd729f44fa6122c8611d Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 20:55:23 +0000 Subject: [PATCH 14/16] fix some pointer related bugs --- api/api.go | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/api/api.go b/api/api.go index 74c1802e44a..7ede4c46850 100644 --- a/api/api.go +++ b/api/api.go @@ -51,56 +51,43 @@ func (b *build) Set(key string, value interface{}) { b.flags = append([]string{key, value.(string)}, b.flags...) } -func NewSite() *newSite { - n := new(newSite) - *n.cmd = *commands.NewSiteCmd - return n -} - // NewSite creates new sites -type newSite struct { - cmd *cobra.Command +type NewSite struct { path string } // Set adds a value to the flags array -func (n *newSite) Set(key string, value interface{}) { +func (n *NewSite) Set(key, value string) { if key == "path" { n.path = key return } - n.cmd.Flags().Set(key, value.(string)) + commands.NewSiteCmd.Flags().Set(key, value) } // Make generates a new site -func (n *newSite) Make() error { - return n.cmd.RunE(n.cmd, []string{n.path}) -} - -func NewContent() *newContent { - n := new(newContent) - *n.cmd = *commands.NewCmd - return n +func (n *NewSite) Make() error { + return commands.NewSiteCmd.RunE(commands.NewSiteCmd, []string{n.path}) } // NewContent is used to create new contents -type newContent struct { +type NewContent struct { cmd *cobra.Command path string } // Set adds a value to the flags array -func (n *newContent) Set(key string, value interface{}) { +func (n *NewContent) Set(key, value string) { if key == "path" { n.path = key return } - n.cmd.Flags().Set(key, value.(string)) + commands.NewCmd.Flags().Set(key, value) } // Make generates a new content -func (n *newContent) Make() error { - return n.cmd.RunE(n.cmd, []string{n.path}) +func (n *NewContent) Make() error { + return commands.NewCmd.RunE(commands.NewCmd, []string{n.path}) } From 5e1a2a8934c13a06fca59f88c72f5d796c331986 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 20:57:04 +0000 Subject: [PATCH 15/16] fix key/value misplaced --- api/api.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/api.go b/api/api.go index 7ede4c46850..c1036afcea9 100644 --- a/api/api.go +++ b/api/api.go @@ -59,7 +59,7 @@ type NewSite struct { // Set adds a value to the flags array func (n *NewSite) Set(key, value string) { if key == "path" { - n.path = key + n.path = value return } @@ -80,7 +80,7 @@ type NewContent struct { // Set adds a value to the flags array func (n *NewContent) Set(key, value string) { if key == "path" { - n.path = key + n.path = value return } From 0c42a21a638f0e8764b5a7a9fbd4f5fa6a8d220d Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 14 Feb 2016 20:57:56 +0000 Subject: [PATCH 16/16] export Build --- api/api.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/api/api.go b/api/api.go index c1036afcea9..a21086050a2 100644 --- a/api/api.go +++ b/api/api.go @@ -26,23 +26,19 @@ func Reset() { viper.Reset() } -func NewBuild() *build { - return new(build) -} - // Build is the type that handles Building methods -type build struct { +type Build struct { flags []string } // Run regenerates the website -func (b *build) Run() error { +func (b *Build) Run() error { _, err := commands.Execute(b.flags) return err } // Set adds a value to the flags array -func (b *build) Set(key string, value interface{}) { +func (b *Build) Set(key string, value interface{}) { // If the key doesn't begin with "-" if key[0] != '-' { key = "--" + key