2018-04-09 13:36:10 -04:00
|
|
|
// Copyright 2018 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 commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2018-06-11 03:38:50 -04:00
|
|
|
"path/filepath"
|
2018-04-09 13:36:10 -04:00
|
|
|
"strings"
|
|
|
|
|
2018-10-20 11:38:49 -04:00
|
|
|
"github.com/gohugoio/hugo/parser/metadecoders"
|
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
_errors "github.com/pkg/errors"
|
|
|
|
|
2018-06-11 03:38:50 -04:00
|
|
|
"github.com/gohugoio/hugo/create"
|
2018-04-09 13:36:10 -04:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
2018-06-11 03:38:50 -04:00
|
|
|
"github.com/gohugoio/hugo/parser"
|
2018-04-09 13:36:10 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2018-06-11 03:38:50 -04:00
|
|
|
"github.com/spf13/viper"
|
2018-04-09 13:36:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ cmder = (*newSiteCmd)(nil)
|
|
|
|
|
|
|
|
type newSiteCmd struct {
|
|
|
|
configFormat string
|
|
|
|
|
2020-01-31 03:09:11 -05:00
|
|
|
*baseBuilderCmd
|
2018-04-09 13:36:10 -04:00
|
|
|
}
|
|
|
|
|
2020-01-31 03:09:11 -05:00
|
|
|
func (b *commandsBuilder) newNewSiteCmd() *newSiteCmd {
|
|
|
|
cc := &newSiteCmd{}
|
2018-04-09 13:36:10 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
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.`,
|
2020-01-31 03:09:11 -05:00
|
|
|
RunE: cc.newSite,
|
2018-04-09 13:36:10 -04:00
|
|
|
}
|
|
|
|
|
2020-01-31 03:09:11 -05:00
|
|
|
cmd.Flags().StringVarP(&cc.configFormat, "format", "f", "toml", "config & frontmatter format")
|
2018-04-09 13:36:10 -04:00
|
|
|
cmd.Flags().Bool("force", false, "init inside non-empty directory")
|
|
|
|
|
2020-01-31 03:09:11 -05:00
|
|
|
cc.baseBuilderCmd = b.newBuilderBasicCmd(cmd)
|
2018-04-09 13:36:10 -04:00
|
|
|
|
2020-01-31 03:09:11 -05:00
|
|
|
return cc
|
2018-04-09 13:36:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *newSiteCmd) doNewSite(fs *hugofs.Fs, basepath string, force bool) error {
|
|
|
|
archeTypePath := filepath.Join(basepath, "archetypes")
|
|
|
|
dirs := []string{
|
|
|
|
filepath.Join(basepath, "layouts"),
|
|
|
|
filepath.Join(basepath, "content"),
|
|
|
|
archeTypePath,
|
|
|
|
filepath.Join(basepath, "static"),
|
|
|
|
filepath.Join(basepath, "data"),
|
|
|
|
filepath.Join(basepath, "themes"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists, _ := helpers.Exists(basepath, fs.Source); exists {
|
|
|
|
if isDir, _ := helpers.IsDir(basepath, fs.Source); !isDir {
|
|
|
|
return errors.New(basepath + " already exists but not a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
isEmpty, _ := helpers.IsEmpty(basepath, fs.Source)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case !isEmpty && !force:
|
2019-11-11 15:38:10 -05:00
|
|
|
return errors.New(basepath + " already exists and is not empty. See --force.")
|
2018-04-09 13:36:10 -04:00
|
|
|
|
|
|
|
case !isEmpty && force:
|
|
|
|
all := append(dirs, filepath.Join(basepath, "config."+n.configFormat))
|
|
|
|
for _, path := range all {
|
|
|
|
if exists, _ := helpers.Exists(path, fs.Source); exists {
|
|
|
|
return errors.New(path + " already exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dir := range dirs {
|
|
|
|
if err := fs.Source.MkdirAll(dir, 0777); err != nil {
|
2018-10-03 08:58:09 -04:00
|
|
|
return _errors.Wrap(err, "Failed to create dir")
|
2018-04-09 13:36:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createConfig(fs, basepath, n.configFormat)
|
|
|
|
|
2018-06-11 03:38:50 -04:00
|
|
|
// Create a default archetype file.
|
2018-04-09 13:36:10 -04:00
|
|
|
helpers.SafeWriteToDisk(filepath.Join(archeTypePath, "default.md"),
|
|
|
|
strings.NewReader(create.ArchetypeTemplateTemplate), fs.Source)
|
|
|
|
|
|
|
|
jww.FEEDBACK.Printf("Congratulations! Your new Hugo site is created in %s.\n\n", basepath)
|
|
|
|
jww.FEEDBACK.Println(nextStepsText())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSite creates a new Hugo site and initializes a structured Hugo directory.
|
|
|
|
func (n *newSiteCmd) newSite(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) < 1 {
|
|
|
|
return newUserError("path needs to be provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
createpath, err := filepath.Abs(filepath.Clean(args[0]))
|
|
|
|
if err != nil {
|
|
|
|
return newUserError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
forceNew, _ := cmd.Flags().GetBool("force")
|
|
|
|
|
|
|
|
return n.doNewSite(hugofs.NewDefault(viper.New()), createpath, forceNew)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createConfig(fs *hugofs.Fs, inpath string, kind string) (err error) {
|
|
|
|
in := map[string]string{
|
|
|
|
"baseURL": "http://example.org/",
|
|
|
|
"title": "My New Hugo Site",
|
|
|
|
"languageCode": "en-us",
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2018-10-20 11:38:49 -04:00
|
|
|
err = parser.InterfaceToConfig(in, metadecoders.FormatFromString(kind), &buf)
|
2018-04-09 13:36:10 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return helpers.WriteToDisk(filepath.Join(inpath, "config."+kind), &buf, fs.Source)
|
|
|
|
}
|
2018-06-11 03:38:50 -04:00
|
|
|
|
|
|
|
func nextStepsText() string {
|
|
|
|
var nextStepsText bytes.Buffer
|
|
|
|
|
|
|
|
nextStepsText.WriteString(`Just a few more steps and you're ready to go:
|
|
|
|
|
|
|
|
1. Download a theme into the same-named folder.
|
2019-06-19 17:03:30 -04:00
|
|
|
Choose a theme from https://themes.gohugo.io/ or
|
2018-06-11 03:38:50 -04:00
|
|
|
create your own with the "hugo new theme <THEMENAME>" command.
|
|
|
|
2. Perhaps you want to add some content. You can add single files
|
|
|
|
with "hugo new `)
|
|
|
|
|
|
|
|
nextStepsText.WriteString(filepath.Join("<SECTIONNAME>", "<FILENAME>.<FORMAT>"))
|
|
|
|
|
|
|
|
nextStepsText.WriteString(`".
|
|
|
|
3. Start the built-in live server via "hugo server".
|
|
|
|
|
|
|
|
Visit https://gohugo.io/ for quickstart guide and full documentation.`)
|
|
|
|
|
|
|
|
return nextStepsText.String()
|
|
|
|
}
|