Trigger an editor after hugo new.

- Trigger permanently with NewContentEditor in config.{toml,yaml,json}.
- Trigger on an individual basis with --editor.
This commit is contained in:
Austin Ziegler 2014-10-14 22:48:55 -04:00 committed by spf13
parent 2c8e9a7931
commit ec4b6c03a8
2 changed files with 24 additions and 1 deletions

View file

@ -56,7 +56,7 @@ var hugoCmdV *cobra.Command
//Flags that are to be added to commands. //Flags that are to be added to commands.
var BuildWatch, Draft, Future, UglyUrls, Verbose, Logging, VerboseLog, DisableRSS, DisableSitemap, PluralizeListTitles, NoTimes bool var BuildWatch, Draft, Future, UglyUrls, Verbose, Logging, VerboseLog, DisableRSS, DisableSitemap, PluralizeListTitles, NoTimes bool
var Source, Destination, Theme, BaseUrl, CfgFile, LogFile string var Source, Destination, Theme, BaseUrl, CfgFile, LogFile, Editor 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() {
@ -88,6 +88,7 @@ func init() {
HugoCmd.PersistentFlags().BoolVar(&UglyUrls, "uglyUrls", false, "if true, use /filename.html instead of /filename/") HugoCmd.PersistentFlags().BoolVar(&UglyUrls, "uglyUrls", false, "if true, use /filename.html instead of /filename/")
HugoCmd.PersistentFlags().StringVarP(&BaseUrl, "baseUrl", "b", "", "hostname (and path) to the root eg. http://spf13.com/") HugoCmd.PersistentFlags().StringVarP(&BaseUrl, "baseUrl", "b", "", "hostname (and path) to the root eg. http://spf13.com/")
HugoCmd.PersistentFlags().StringVar(&CfgFile, "config", "", "config file (default is path/config.yaml|json|toml)") HugoCmd.PersistentFlags().StringVar(&CfgFile, "config", "", "config file (default is path/config.yaml|json|toml)")
HugoCmd.PersistentFlags().StringVar(&Editor, "editor", "", "edit new content with this editor, if provided")
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")
@ -134,6 +135,7 @@ func InitializeConfig() {
viper.SetDefault("PluralizeListTitles", true) viper.SetDefault("PluralizeListTitles", true)
viper.SetDefault("FootnoteAnchorPrefix", "") viper.SetDefault("FootnoteAnchorPrefix", "")
viper.SetDefault("FootnoteReturnLinkContents", "") viper.SetDefault("FootnoteReturnLinkContents", "")
viper.SetDefault("NewContentEditor", "")
if hugoCmdV.PersistentFlags().Lookup("buildDrafts").Changed { if hugoCmdV.PersistentFlags().Lookup("buildDrafts").Changed {
viper.Set("BuildDrafts", Draft) viper.Set("BuildDrafts", Draft)
@ -163,6 +165,10 @@ func InitializeConfig() {
viper.Set("PluralizeListTitles", PluralizeListTitles) viper.Set("PluralizeListTitles", PluralizeListTitles)
} }
if hugoCmdV.PersistentFlags().Lookup("editor").Changed {
viper.Set("NewContentEditor", Editor)
}
if hugoCmdV.PersistentFlags().Lookup("logFile").Changed { if hugoCmdV.PersistentFlags().Lookup("logFile").Changed {
viper.Set("LogFile", LogFile) viper.Set("LogFile", LogFile)
} }

View file

@ -17,6 +17,8 @@ import (
"bytes" "bytes"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
@ -104,6 +106,21 @@ func NewContent(kind, name string) (err error) {
} }
jww.FEEDBACK.Println(helpers.AbsPathify(filepath.Join(viper.GetString("contentDir"), name)), "created") jww.FEEDBACK.Println(helpers.AbsPathify(filepath.Join(viper.GetString("contentDir"), name)), "created")
editor := viper.GetString("NewContentEditor")
if editor != "" {
jww.FEEDBACK.Printf("Editing %s in %s.\n", name, editor)
cmd := exec.Command(editor, path.Join(viper.GetString("contentDir"), name))
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err = cmd.Run(); err != nil {
return
}
}
return nil return nil
} }