Skip to content

Commit

Permalink
More server work
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 3, 2023
1 parent b90abf9 commit 8a3123b
Show file tree
Hide file tree
Showing 21 changed files with 1,481 additions and 65 deletions.
4 changes: 2 additions & 2 deletions commands/new_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (b *commandsBuilder) newNewSiteCmd() *newSiteCmd {
Short: "Create a new site (skeleton)",
Long: `Create a new site in the provided directory.
The new site will have the correct structure, but no content or theme yet.
Use ` + "`hugo new [contentPath]`" + ` to create new content.`,
Use ` + "`hugo new content [contentPath]`" + ` to create new content.`,
RunE: cc.newSite,
}

Expand Down Expand Up @@ -142,7 +142,7 @@ func createConfig(fs *hugofs.Fs, inpath string, kind string) (err error) {
return err
}

return helpers.WriteToDisk(filepath.Join(inpath, "config."+kind), &buf, fs.Source)
return helpers.WriteToDisk(filepath.Join(inpath, "hugo."+kind), &buf, fs.Source)
}

func nextStepsText() string {
Expand Down
52 changes: 52 additions & 0 deletions commandsnew/command_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package commandsnew

import (
"context"
"fmt"

"github.com/bep/simplecobra"
ck "github.com/bep/simplecobra"
"github.com/spf13/cobra"
)

func newTemplateCommand() *templateCommand {
return &templateCommand{
commands: []ck.Commander{},
}

}

type templateCommand struct {
rootCmd *rootCommand

commands []ck.Commander
}

func (c *templateCommand) Commands() []simplecobra.Commander {
return c.commands
}

func (c *templateCommand) Init(cd *simplecobra.Commandeer) error {
c.rootCmd = cd.Root.Command.(*rootCommand)
return nil
}

func (c *templateCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
conf, err := c.rootCmd.CommonConfig(flagsToCfg(cd, nil))
if err != nil {
return err
}
fmt.Println("templateCommand.Run", conf)

return nil
}

func (c *templateCommand) Name() string {
return "template"
}

func (c *templateCommand) WithCobraCommand(cmd *cobra.Command) error {
cmd.Short = "Print the site configuration"
cmd.Long = `Print the site configuration, both default and custom settings.`
return nil
}
22 changes: 11 additions & 11 deletions commandsnew/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@ import (
"github.com/spf13/cobra"
)

// newConfigCommands creates a new config command and its subcommands.
func newConfigCommands() *configCommands {
return &configCommands{
// newConfigCommand creates a new config command and its subcommands.
func newConfigCommand() *configCommand {
return &configCommand{
commands: []ck.Commander{
&configMountsCommand{},
},
}

}

type configCommands struct {
type configCommand struct {
rootCmd *rootCommand

commands []ck.Commander
}

func (c *configCommands) Commands() []simplecobra.Commander {
func (c *configCommand) Commands() []simplecobra.Commander {
return c.commands
}

func (c *configCommands) Init(cd *simplecobra.Commandeer) error {
func (c *configCommand) Init(cd *simplecobra.Commandeer) error {
c.rootCmd = cd.Root.Command.(*rootCommand)
return nil
}

func (c *configCommands) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
func (c *configCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
conf, err := c.rootCmd.CommonConfig(flagsToCfg(cd, nil))
if err != nil {
return err
Expand All @@ -57,26 +57,26 @@ func (c *configCommands) Run(ctx context.Context, cd *simplecobra.Commandeer, ar
return nil
}

func (c *configCommands) Name() string {
func (c *configCommand) Name() string {
return "config"
}

func (c *configCommands) WithCobraCommand(cmd *cobra.Command) error {
func (c *configCommand) WithCobraCommand(cmd *cobra.Command) error {
cmd.Short = "Print the site configuration"
cmd.Long = `Print the site configuration, both default and custom settings.`
return nil
}

type configMountsCommand struct {
configCmd *configCommands
configCmd *configCommand
}

func (c *configMountsCommand) Commands() []simplecobra.Commander {
return nil
}

func (c *configMountsCommand) Init(cd *simplecobra.Commandeer) error {
c.configCmd = cd.Parent.Command.(*configCommands)
c.configCmd = cd.Parent.Command.(*configCommand)
return nil
}

Expand Down
37 changes: 30 additions & 7 deletions commandsnew/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/config/allconfig"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/hugolib"
"github.com/gohugoio/hugo/identity"
Expand Down Expand Up @@ -71,7 +72,8 @@ func newExec() (*simplecobra.Exec, error) {
},
},
newServerCommand(),
newConfigCommands(),
newConfigCommand(),
newNewCommand(),
newListCommands(),
newModCommands(),
},
Expand All @@ -86,10 +88,20 @@ func Execute(args []string) error {
if err != nil {
return err
}
args = mapLegacyArgs(args)
_, err = x.Execute(context.Background(), args)
return err
}

func mapLegacyArgs(args []string) []string {
if args[0] == "new" && !helpers.StringSliceContainsAny(args, "site", "theme") {
// Insert "content" as the second argument
args = append(args[:1], append([]string{"content"}, args[1:]...)...)
}

return args
}

// This is the root command.
type rootCommand struct {
Printf func(format string, v ...interface{})
Expand All @@ -109,10 +121,11 @@ type rootCommand struct {
baseURL string
environment string

buildWatch bool
panicOnWarning bool
poll string
clock string
buildWatch bool
forceSyncStatic bool
panicOnWarning bool
poll string
clock string

gc bool

Expand Down Expand Up @@ -158,6 +171,7 @@ func flagsToCfg(cd *simplecobra.Commandeer, cfg config.Provider) config.Provider
"destination": "publishDir",
"printI18nWarnings": "logI18nWarnings",
"printPathWarnings": "logPathWarnings",
"editor": "newContentEditor",
}

cmd := cd.CobraCommand
Expand Down Expand Up @@ -396,7 +410,7 @@ Complete documentation is available at https://gohugo.io/.`
cmd.Flags().BoolVar(&c.panicOnWarning, "panicOnWarning", false, "panic on first WARNING log")
cmd.Flags().Bool("templateMetrics", false, "display metrics about template executions")
cmd.Flags().Bool("templateMetricsHints", false, "calculate some improvement hints when combined with --templateMetrics")
cmd.Flags().BoolP("forceSyncStatic", "", false, "copy all files when static is changed.")
cmd.Flags().BoolVar(&c.forceSyncStatic, "", false, "copy all files when static is changed.")
cmd.Flags().BoolP("noTimes", "", false, "don't sync modification time of files")
cmd.Flags().BoolP("noChmod", "", false, "don't sync permission mode of files")
cmd.Flags().BoolP("noBuildLock", "", false, "don't create .hugo_build.lock file")
Expand Down Expand Up @@ -429,16 +443,22 @@ Complete documentation is available at https://gohugo.io/.`
}

type simpleCommand struct {
use string
name string
short string
long string
run func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *rootCommand, args []string) error
withc func(cmd *cobra.Command)

commands []simplecobra.Commander

rootCmd *rootCommand
}

func (c *simpleCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
if c.run == nil {
return nil
}
return c.run(ctx, cd, c.rootCmd, args)
}

Expand All @@ -449,6 +469,9 @@ func (c *simpleCommand) Name() string {
func (c *simpleCommand) WithCobraCommand(cmd *cobra.Command) error {
cmd.Short = c.short
cmd.Long = c.long
if c.use != "" {
cmd.Use = c.use
}
if c.withc != nil {
c.withc(cmd)
}
Expand All @@ -461,5 +484,5 @@ func (c *simpleCommand) Init(cd *simplecobra.Commandeer) error {
}

func (c *simpleCommand) Commands() []simplecobra.Commander {
return nil
return c.commands
}
Loading

0 comments on commit 8a3123b

Please sign in to comment.