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