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