2023-01-04 12:24:36 -05:00
|
|
|
// Copyright 2023 The Hugo Authors. All rights reserved.
|
2015-02-08 10:11:04 -05:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-05-02 01:06:01 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-05-02 01:06:01 -04:00
|
|
|
//
|
|
|
|
// 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 commands
|
|
|
|
|
|
|
|
import (
|
2014-05-08 18:30:11 -04:00
|
|
|
"bytes"
|
2023-01-04 12:24:36 -05:00
|
|
|
"context"
|
2014-05-08 18:30:11 -04:00
|
|
|
"path/filepath"
|
2014-05-02 01:06:01 -04:00
|
|
|
"strings"
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/bep/simplecobra"
|
|
|
|
"github.com/gohugoio/hugo/config"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/create"
|
2023-08-16 18:07:01 -04:00
|
|
|
"github.com/gohugoio/hugo/create/skeletons"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/cobra"
|
2014-05-02 01:06:01 -04:00
|
|
|
)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func newNewCommand() *newCommand {
|
|
|
|
var (
|
2023-05-22 13:00:07 -04:00
|
|
|
force bool
|
|
|
|
contentType string
|
2023-05-27 09:04:36 -04:00
|
|
|
format string
|
2023-01-04 12:24:36 -05:00
|
|
|
)
|
2018-04-09 13:36:10 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
var c *newCommand
|
|
|
|
c = &newCommand{
|
|
|
|
commands: []simplecobra.Commander{
|
|
|
|
&simpleCommand{
|
|
|
|
name: "content",
|
|
|
|
use: "content [path]",
|
|
|
|
short: "Create new content for your site",
|
|
|
|
long: `Create a new content file and automatically set the date and title.
|
2023-06-24 11:45:53 -04:00
|
|
|
It will guess which kind of file to create based on the path provided.
|
|
|
|
|
|
|
|
You can also specify the kind with ` + "`-k KIND`" + `.
|
|
|
|
|
|
|
|
If archetypes are provided in your theme or site, they will be used.
|
|
|
|
|
|
|
|
Ensure you run this within the root directory of your site.`,
|
2023-01-04 12:24:36 -05:00
|
|
|
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
|
|
|
|
if len(args) < 1 {
|
2023-06-27 10:33:28 -04:00
|
|
|
return newUserError("path needs to be provided")
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
|
|
|
h, err := r.Hugo(flagsToCfg(cd, nil))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return create.NewContent(h, contentType, args[0], force)
|
|
|
|
},
|
2023-05-28 04:44:40 -04:00
|
|
|
withc: func(cmd *cobra.Command, r *rootCommand) {
|
2023-01-04 12:24:36 -05:00
|
|
|
cmd.Flags().StringVarP(&contentType, "kind", "k", "", "content type to create")
|
|
|
|
cmd.Flags().String("editor", "", "edit new content with this editor, if provided")
|
|
|
|
cmd.Flags().BoolVarP(&force, "force", "f", false, "overwrite file if it already exists")
|
2023-05-27 09:04:36 -04:00
|
|
|
cmd.Flags().StringVar(&format, "format", "toml", "preferred file format (toml, yaml or json)")
|
2023-05-28 04:44:40 -04:00
|
|
|
applyLocalFlagsBuildConfig(cmd, r)
|
2023-05-27 09:04:36 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&simpleCommand{
|
|
|
|
name: "site",
|
|
|
|
use: "site [path]",
|
|
|
|
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.`,
|
|
|
|
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
|
|
|
|
if len(args) < 1 {
|
2023-06-27 10:33:28 -04:00
|
|
|
return newUserError("path needs to be provided")
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
|
|
|
createpath, err := filepath.Abs(filepath.Clean(args[0]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-05 12:41:40 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
cfg := config.New()
|
|
|
|
cfg.Set("workingDir", createpath)
|
|
|
|
cfg.Set("publishDir", "public")
|
2014-05-02 01:06:01 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
conf, err := r.ConfigFromProvider(r.configVersionID.Load(), flagsToCfg(cd, cfg))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sourceFs := conf.fs.Source
|
2015-08-04 05:15:12 -04:00
|
|
|
|
2023-08-16 18:07:01 -04:00
|
|
|
err = skeletons.CreateSite(createpath, sourceFs, force, format)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
|
|
|
|
2023-08-16 18:07:01 -04:00
|
|
|
r.Printf("Congratulations! Your new Hugo site was created in %s.\n\n", createpath)
|
|
|
|
r.Println(c.newSiteNextStepsText(createpath, format))
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2023-05-28 04:44:40 -04:00
|
|
|
withc: func(cmd *cobra.Command, r *rootCommand) {
|
2023-05-27 09:08:11 -04:00
|
|
|
cmd.Flags().BoolVarP(&force, "force", "f", false, "init inside non-empty directory")
|
2023-06-24 11:45:53 -04:00
|
|
|
cmd.Flags().StringVar(&format, "format", "toml", "preferred file format (toml, yaml or json)")
|
2023-01-04 12:24:36 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&simpleCommand{
|
|
|
|
name: "theme",
|
2023-06-28 03:30:27 -04:00
|
|
|
use: "theme [name]",
|
|
|
|
short: "Create a new theme (skeleton)",
|
|
|
|
long: `Create a new theme (skeleton) called [name] in ./themes.
|
|
|
|
New theme is a skeleton. Please add content to the touched files. Add your
|
|
|
|
name to the copyright line in the license and adjust the theme.toml file
|
|
|
|
according to your needs.`,
|
2023-01-04 12:24:36 -05:00
|
|
|
run: func(ctx context.Context, cd *simplecobra.Commandeer, r *rootCommand, args []string) error {
|
2023-06-27 10:33:28 -04:00
|
|
|
if len(args) < 1 {
|
|
|
|
return newUserError("theme name needs to be provided")
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
h, err := r.Hugo(flagsToCfg(cd, nil))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ps := h.PathSpec
|
|
|
|
sourceFs := ps.Fs.Source
|
|
|
|
themesDir := h.Configs.LoadingInfo.BaseConfig.ThemesDir
|
|
|
|
createpath := ps.AbsPathify(filepath.Join(themesDir, args[0]))
|
2023-08-16 18:07:01 -04:00
|
|
|
r.Println("Creating new theme in", createpath)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
2023-08-16 18:07:01 -04:00
|
|
|
err = skeletons.CreateTheme(createpath, sourceFs)
|
2023-01-04 12:24:36 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-11-17 22:20:43 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
return c
|
2015-08-04 05:15:12 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
2014-05-02 01:06:01 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
type newCommand struct {
|
|
|
|
rootCmd *rootCommand
|
2018-04-09 13:36:10 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
commands []simplecobra.Commander
|
|
|
|
}
|
2014-05-08 18:30:11 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *newCommand) Commands() []simplecobra.Commander {
|
|
|
|
return c.commands
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *newCommand) Name() string {
|
|
|
|
return "new"
|
|
|
|
}
|
2014-05-02 01:06:01 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *newCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2023-05-17 12:45:23 -04:00
|
|
|
func (c *newCommand) Init(cd *simplecobra.Commandeer) error {
|
|
|
|
cmd := cd.CobraCommand
|
2023-01-04 12:24:36 -05:00
|
|
|
cmd.Short = "Create new content for your site"
|
|
|
|
cmd.Long = `Create a new content file and automatically set the date and title.
|
|
|
|
It will guess which kind of file to create based on the path provided.
|
|
|
|
|
|
|
|
You can also specify the kind with ` + "`-k KIND`" + `.
|
2014-05-02 01:06:01 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
If archetypes are provided in your theme or site, they will be used.
|
|
|
|
|
|
|
|
Ensure you run this within the root directory of your site.`
|
2023-06-26 14:20:24 -04:00
|
|
|
|
|
|
|
cmd.RunE = nil
|
2023-01-04 12:24:36 -05:00
|
|
|
return nil
|
2014-05-02 01:06:01 -04:00
|
|
|
}
|
|
|
|
|
2023-05-17 12:45:23 -04:00
|
|
|
func (c *newCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
|
2023-01-04 12:24:36 -05:00
|
|
|
c.rootCmd = cd.Root.Command.(*rootCommand)
|
|
|
|
return nil
|
|
|
|
}
|
2014-05-08 18:30:11 -04:00
|
|
|
|
2023-08-16 18:07:01 -04:00
|
|
|
func (c *newCommand) newSiteNextStepsText(path string, format string) string {
|
|
|
|
format = strings.ToLower(format)
|
2023-01-04 12:24:36 -05:00
|
|
|
var nextStepsText bytes.Buffer
|
2018-09-19 01:48:17 -04:00
|
|
|
|
2023-08-16 18:07:01 -04:00
|
|
|
nextStepsText.WriteString(`Just a few more steps...
|
2023-01-04 12:24:36 -05:00
|
|
|
|
2023-08-16 18:07:01 -04:00
|
|
|
1. Change the current directory to ` + path + `.
|
|
|
|
2. Create or install a theme:
|
|
|
|
- Create a new theme with the command "hugo new theme <THEMENAME>"
|
|
|
|
- Install a theme from https://themes.gohugo.io/
|
|
|
|
3. Edit hugo.` + format + `, setting the "theme" property to the theme name.
|
|
|
|
4. Create new content with the command "hugo new content `)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
nextStepsText.WriteString(filepath.Join("<SECTIONNAME>", "<FILENAME>.<FORMAT>"))
|
|
|
|
|
|
|
|
nextStepsText.WriteString(`".
|
2023-08-16 18:07:01 -04:00
|
|
|
5. Start the embedded web server with the command "hugo server --buildDrafts".
|
2023-01-04 12:24:36 -05:00
|
|
|
|
2023-08-16 18:07:01 -04:00
|
|
|
See documentation at https://gohugo.io/.`)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
return nextStepsText.String()
|
|
|
|
}
|