github.com/gohugoio/hugo@v0.88.1/commands/new_site.go (about)

     1  // Copyright 2018 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package commands
    15  
    16  import (
    17  	"bytes"
    18  	"errors"
    19  	"path/filepath"
    20  	"strings"
    21  
    22  	"github.com/gohugoio/hugo/config"
    23  	"github.com/gohugoio/hugo/parser/metadecoders"
    24  
    25  	_errors "github.com/pkg/errors"
    26  
    27  	"github.com/gohugoio/hugo/create"
    28  	"github.com/gohugoio/hugo/helpers"
    29  	"github.com/gohugoio/hugo/hugofs"
    30  	"github.com/gohugoio/hugo/parser"
    31  	"github.com/spf13/cobra"
    32  	jww "github.com/spf13/jwalterweatherman"
    33  )
    34  
    35  var _ cmder = (*newSiteCmd)(nil)
    36  
    37  type newSiteCmd struct {
    38  	configFormat string
    39  
    40  	*baseBuilderCmd
    41  }
    42  
    43  func (b *commandsBuilder) newNewSiteCmd() *newSiteCmd {
    44  	cc := &newSiteCmd{}
    45  
    46  	cmd := &cobra.Command{
    47  		Use:   "site [path]",
    48  		Short: "Create a new site (skeleton)",
    49  		Long: `Create a new site in the provided directory.
    50  The new site will have the correct structure, but no content or theme yet.
    51  Use ` + "`hugo new [contentPath]`" + ` to create new content.`,
    52  		RunE: cc.newSite,
    53  	}
    54  
    55  	cmd.Flags().StringVarP(&cc.configFormat, "format", "f", "toml", "config & frontmatter format")
    56  	cmd.Flags().Bool("force", false, "init inside non-empty directory")
    57  
    58  	cc.baseBuilderCmd = b.newBuilderBasicCmd(cmd)
    59  
    60  	return cc
    61  }
    62  
    63  func (n *newSiteCmd) doNewSite(fs *hugofs.Fs, basepath string, force bool) error {
    64  	archeTypePath := filepath.Join(basepath, "archetypes")
    65  	dirs := []string{
    66  		filepath.Join(basepath, "layouts"),
    67  		filepath.Join(basepath, "content"),
    68  		archeTypePath,
    69  		filepath.Join(basepath, "static"),
    70  		filepath.Join(basepath, "data"),
    71  		filepath.Join(basepath, "themes"),
    72  	}
    73  
    74  	if exists, _ := helpers.Exists(basepath, fs.Source); exists {
    75  		if isDir, _ := helpers.IsDir(basepath, fs.Source); !isDir {
    76  			return errors.New(basepath + " already exists but not a directory")
    77  		}
    78  
    79  		isEmpty, _ := helpers.IsEmpty(basepath, fs.Source)
    80  
    81  		switch {
    82  		case !isEmpty && !force:
    83  			return errors.New(basepath + " already exists and is not empty. See --force.")
    84  
    85  		case !isEmpty && force:
    86  			all := append(dirs, filepath.Join(basepath, "config."+n.configFormat))
    87  			for _, path := range all {
    88  				if exists, _ := helpers.Exists(path, fs.Source); exists {
    89  					return errors.New(path + " already exists")
    90  				}
    91  			}
    92  		}
    93  	}
    94  
    95  	for _, dir := range dirs {
    96  		if err := fs.Source.MkdirAll(dir, 0777); err != nil {
    97  			return _errors.Wrap(err, "Failed to create dir")
    98  		}
    99  	}
   100  
   101  	createConfig(fs, basepath, n.configFormat)
   102  
   103  	// Create a default archetype file.
   104  	helpers.SafeWriteToDisk(filepath.Join(archeTypePath, "default.md"),
   105  		strings.NewReader(create.ArchetypeTemplateTemplate), fs.Source)
   106  
   107  	jww.FEEDBACK.Printf("Congratulations! Your new Hugo site is created in %s.\n\n", basepath)
   108  	jww.FEEDBACK.Println(nextStepsText())
   109  
   110  	return nil
   111  }
   112  
   113  // newSite creates a new Hugo site and initializes a structured Hugo directory.
   114  func (n *newSiteCmd) newSite(cmd *cobra.Command, args []string) error {
   115  	if len(args) < 1 {
   116  		return newUserError("path needs to be provided")
   117  	}
   118  
   119  	createpath, err := filepath.Abs(filepath.Clean(args[0]))
   120  	if err != nil {
   121  		return newUserError(err)
   122  	}
   123  
   124  	forceNew, _ := cmd.Flags().GetBool("force")
   125  
   126  	return n.doNewSite(hugofs.NewDefault(config.New()), createpath, forceNew)
   127  }
   128  
   129  func createConfig(fs *hugofs.Fs, inpath string, kind string) (err error) {
   130  	in := map[string]string{
   131  		"baseURL":      "http://example.org/",
   132  		"title":        "My New Hugo Site",
   133  		"languageCode": "en-us",
   134  	}
   135  
   136  	var buf bytes.Buffer
   137  	err = parser.InterfaceToConfig(in, metadecoders.FormatFromString(kind), &buf)
   138  	if err != nil {
   139  		return err
   140  	}
   141  
   142  	return helpers.WriteToDisk(filepath.Join(inpath, "config."+kind), &buf, fs.Source)
   143  }
   144  
   145  func nextStepsText() string {
   146  	var nextStepsText bytes.Buffer
   147  
   148  	nextStepsText.WriteString(`Just a few more steps and you're ready to go:
   149  
   150  1. Download a theme into the same-named folder.
   151     Choose a theme from https://themes.gohugo.io/ or
   152     create your own with the "hugo new theme <THEMENAME>" command.
   153  2. Perhaps you want to add some content. You can add single files
   154     with "hugo new `)
   155  
   156  	nextStepsText.WriteString(filepath.Join("<SECTIONNAME>", "<FILENAME>.<FORMAT>"))
   157  
   158  	nextStepsText.WriteString(`".
   159  3. Start the built-in live server via "hugo server".
   160  
   161  Visit https://gohugo.io/ for quickstart guide and full documentation.`)
   162  
   163  	return nextStepsText.String()
   164  }