2019-01-02 06:33:26 -05:00
// Copyright 2019 The Hugo Authors. All rights reserved.
2018-04-11 01:54:08 -04:00
//
// 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 (
2019-11-21 07:07:52 -05:00
"fmt"
2018-11-15 03:28:02 -05:00
"os"
2019-11-21 07:07:52 -05:00
"time"
2018-11-15 03:28:02 -05:00
"github.com/gohugoio/hugo/hugolib/paths"
"github.com/gohugoio/hugo/common/hugo"
2018-10-03 08:58:09 -04:00
"github.com/gohugoio/hugo/common/loggers"
2018-04-11 01:54:08 -04:00
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/helpers"
"github.com/spf13/cobra"
)
2018-04-13 02:42:29 -04:00
type commandsBuilder struct {
hugoBuilderCommon
commands [ ] cmder
}
func newCommandsBuilder ( ) * commandsBuilder {
return & commandsBuilder { }
}
func ( b * commandsBuilder ) addCommands ( commands ... cmder ) * commandsBuilder {
b . commands = append ( b . commands , commands ... )
return b
2018-04-11 01:54:08 -04:00
}
2018-04-13 02:42:29 -04:00
func ( b * commandsBuilder ) addAll ( ) * commandsBuilder {
b . addCommands (
b . newServerCmd ( ) ,
2018-04-11 01:54:08 -04:00
newVersionCmd ( ) ,
newEnvCmd ( ) ,
2020-01-31 03:09:11 -05:00
b . newConfigCmd ( ) ,
b . newDeployCmd ( ) ,
b . newConvertCmd ( ) ,
2018-11-17 22:20:43 -05:00
b . newNewCmd ( ) ,
2020-01-31 03:09:11 -05:00
b . newListCmd ( ) ,
2018-04-11 01:54:08 -04:00
newImportCmd ( ) ,
newGenCmd ( ) ,
2018-04-16 02:23:32 -04:00
createReleaser ( ) ,
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
b . newModCmd ( ) ,
2018-04-11 01:54:08 -04:00
)
2018-04-13 02:42:29 -04:00
return b
}
func ( b * commandsBuilder ) build ( ) * hugoCmd {
h := b . newHugoCmd ( )
addCommands ( h . getCommand ( ) , b . commands ... )
return h
2018-04-11 01:54:08 -04:00
}
func addCommands ( root * cobra . Command , commands ... cmder ) {
for _ , command := range commands {
2018-04-16 02:23:32 -04:00
cmd := command . getCommand ( )
if cmd == nil {
continue
}
root . AddCommand ( cmd )
2018-04-11 01:54:08 -04:00
}
}
type baseCmd struct {
cmd * cobra . Command
}
2018-04-13 02:42:29 -04:00
var _ commandsBuilderGetter = ( * baseBuilderCmd ) ( nil )
// Used in tests.
type commandsBuilderGetter interface {
2018-11-06 02:45:19 -05:00
getCommandsBuilder ( ) * commandsBuilder
2018-04-13 02:42:29 -04:00
}
2020-12-02 07:23:25 -05:00
2018-04-11 01:54:08 -04:00
type baseBuilderCmd struct {
* baseCmd
2018-04-13 02:42:29 -04:00
* commandsBuilder
}
2018-11-06 02:45:19 -05:00
func ( b * baseBuilderCmd ) getCommandsBuilder ( ) * commandsBuilder {
2018-04-13 02:42:29 -04:00
return b . commandsBuilder
2018-04-11 01:54:08 -04:00
}
func ( c * baseCmd ) getCommand ( ) * cobra . Command {
return c . cmd
}
func newBaseCmd ( cmd * cobra . Command ) * baseCmd {
return & baseCmd { cmd : cmd }
}
2018-04-13 02:42:29 -04:00
func ( b * commandsBuilder ) newBuilderCmd ( cmd * cobra . Command ) * baseBuilderCmd {
bcmd := & baseBuilderCmd { commandsBuilder : b , baseCmd : & baseCmd { cmd : cmd } }
2018-04-11 01:54:08 -04:00
bcmd . hugoBuilderCommon . handleFlags ( cmd )
return bcmd
}
2020-01-31 03:09:11 -05:00
func ( b * commandsBuilder ) newBuilderBasicCmd ( cmd * cobra . Command ) * baseBuilderCmd {
bcmd := & baseBuilderCmd { commandsBuilder : b , baseCmd : & baseCmd { cmd : cmd } }
bcmd . hugoBuilderCommon . handleCommonBuilderFlags ( cmd )
return bcmd
}
2018-04-11 01:54:08 -04:00
func ( c * baseCmd ) flagsToConfig ( cfg config . Provider ) {
initializeFlags ( c . cmd , cfg )
}
type hugoCmd struct {
* baseBuilderCmd
2018-04-11 14:17:28 -04:00
// Need to get the sites once built.
c * commandeer
2018-04-11 01:54:08 -04:00
}
2018-04-16 02:23:32 -04:00
var _ cmder = ( * nilCommand ) ( nil )
2022-01-04 07:07:10 -05:00
type nilCommand struct { }
2018-04-16 02:23:32 -04:00
func ( c * nilCommand ) getCommand ( ) * cobra . Command {
return nil
}
func ( c * nilCommand ) flagsToConfig ( cfg config . Provider ) {
}
2018-04-13 02:42:29 -04:00
func ( b * commandsBuilder ) newHugoCmd ( ) * hugoCmd {
2018-04-11 01:54:08 -04:00
cc := & hugoCmd { }
2018-04-13 02:42:29 -04:00
cc . baseBuilderCmd = b . newBuilderCmd ( & cobra . Command {
2018-04-11 01:54:08 -04:00
Use : "hugo" ,
Short : "hugo builds your site" ,
Long : ` hugo is the main command , used to build your Hugo site .
Hugo is a Fast and Flexible Static Site Generator
built with love by spf13 and friends in Go .
Complete documentation is available at http : //gohugo.io/.`,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2019-11-21 07:07:52 -05:00
defer cc . timeTrack ( time . Now ( ) , "Total" )
2018-04-11 01:54:08 -04:00
cfgInit := func ( c * commandeer ) error {
if cc . buildWatch {
c . Set ( "disableLiveReload" , true )
}
return nil
}
2021-07-05 04:38:54 -04:00
// prevent cobra printing error so it can be handled here (before the timeTrack prints)
cmd . SilenceErrors = true
2021-08-31 11:02:51 -04:00
c , err := initializeConfig ( true , true , cc . buildWatch , & cc . hugoBuilderCommon , cc , cfgInit )
2018-04-11 01:54:08 -04:00
if err != nil {
2021-07-05 04:38:54 -04:00
cmd . PrintErrln ( "Error:" , err . Error ( ) )
2018-04-11 01:54:08 -04:00
return err
}
2018-04-11 14:17:28 -04:00
cc . c = c
2018-04-11 01:54:08 -04:00
2021-07-05 04:38:54 -04:00
err = c . build ( )
if err != nil {
cmd . PrintErrln ( "Error:" , err . Error ( ) )
}
return err
2018-04-11 01:54:08 -04:00
} ,
} )
cc . cmd . PersistentFlags ( ) . StringVar ( & cc . cfgFile , "config" , "" , "config file (default is path/config.yaml|json|toml)" )
2018-11-15 03:28:02 -05:00
cc . cmd . PersistentFlags ( ) . StringVar ( & cc . cfgDir , "configDir" , "config" , "config dir" )
2018-04-11 01:54:08 -04:00
cc . cmd . PersistentFlags ( ) . BoolVar ( & cc . quiet , "quiet" , false , "build in quiet mode" )
// Set bash-completion
2019-01-31 07:06:18 -05:00
_ = cc . cmd . PersistentFlags ( ) . SetAnnotation ( "config" , cobra . BashCompFilenameExt , config . ValidConfigFileExtensions )
2018-04-11 01:54:08 -04:00
cc . cmd . PersistentFlags ( ) . BoolVarP ( & cc . verbose , "verbose" , "v" , false , "verbose output" )
cc . cmd . PersistentFlags ( ) . BoolVarP ( & cc . debug , "debug" , "" , false , "debug output" )
cc . cmd . PersistentFlags ( ) . BoolVar ( & cc . logging , "log" , false , "enable Logging" )
cc . cmd . PersistentFlags ( ) . StringVar ( & cc . logFile , "logFile" , "" , "log File path (if set, logging enabled automatically)" )
cc . cmd . PersistentFlags ( ) . BoolVar ( & cc . verboseLog , "verboseLog" , false , "verbose logging" )
cc . cmd . Flags ( ) . BoolVarP ( & cc . buildWatch , "watch" , "w" , false , "watch filesystem for changes and recreate as needed" )
2018-04-11 03:57:43 -04:00
cc . cmd . Flags ( ) . Bool ( "renderToMemory" , false , "render to memory (only useful for benchmark testing)" )
2018-04-11 01:54:08 -04:00
// Set bash-completion
_ = cc . cmd . PersistentFlags ( ) . SetAnnotation ( "logFile" , cobra . BashCompFilenameExt , [ ] string { } )
cc . cmd . SetGlobalNormalizationFunc ( helpers . NormalizeHugoFlags )
cc . cmd . SilenceUsage = true
return cc
}
type hugoBuilderCommon struct {
2018-11-15 03:28:02 -05:00
source string
baseURL string
environment string
2018-04-11 01:54:08 -04:00
buildWatch bool
2021-07-05 04:13:41 -04:00
poll string
2018-04-11 01:54:08 -04:00
gc bool
2019-01-02 06:33:26 -05:00
// Profile flags (for debugging of performance problems)
cpuprofile string
memprofile string
mutexprofile string
traceprofile string
2020-06-25 05:44:27 -04:00
printm bool
2019-01-02 06:33:26 -05:00
2018-04-11 01:54:08 -04:00
// TODO(bep) var vs string
logging bool
verbose bool
verboseLog bool
debug bool
quiet bool
cfgFile string
2018-11-15 03:28:02 -05:00
cfgDir string
2018-04-11 01:54:08 -04:00
logFile string
}
2019-11-21 07:07:52 -05:00
func ( cc * hugoBuilderCommon ) timeTrack ( start time . Time , name string ) {
if cc . quiet {
return
}
elapsed := time . Since ( start )
fmt . Printf ( "%s in %v ms\n" , name , int ( 1000 * elapsed . Seconds ( ) ) )
}
2018-11-15 03:28:02 -05:00
func ( cc * hugoBuilderCommon ) getConfigDir ( baseDir string ) string {
if cc . cfgDir != "" {
return paths . AbsPathify ( baseDir , cc . cfgDir )
}
if v , found := os . LookupEnv ( "HUGO_CONFIGDIR" ) ; found {
return paths . AbsPathify ( baseDir , v )
}
return paths . AbsPathify ( baseDir , "config" )
}
func ( cc * hugoBuilderCommon ) getEnvironment ( isServer bool ) string {
if cc . environment != "" {
return cc . environment
}
if v , found := os . LookupEnv ( "HUGO_ENVIRONMENT" ) ; found {
return v
}
2019-11-25 13:08:35 -05:00
// Used by Netlify and Forestry
if v , found := os . LookupEnv ( "HUGO_ENV" ) ; found {
return v
}
2018-11-15 03:28:02 -05:00
if isServer {
return hugo . EnvironmentDevelopment
}
return hugo . EnvironmentProduction
}
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
func ( cc * hugoBuilderCommon ) handleCommonBuilderFlags ( cmd * cobra . Command ) {
cmd . PersistentFlags ( ) . StringVarP ( & cc . source , "source" , "s" , "" , "filesystem path to read files relative from" )
cmd . PersistentFlags ( ) . SetAnnotation ( "source" , cobra . BashCompSubdirsInDir , [ ] string { } )
cmd . PersistentFlags ( ) . StringVarP ( & cc . environment , "environment" , "e" , "" , "build environment" )
cmd . PersistentFlags ( ) . StringP ( "themesDir" , "" , "" , "filesystem path to themes directory" )
2020-09-09 10:51:13 -04:00
cmd . PersistentFlags ( ) . StringP ( "ignoreVendorPaths" , "" , "" , "ignores any _vendor for module paths matching the given Glob pattern" )
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
}
2018-04-11 01:54:08 -04:00
func ( cc * hugoBuilderCommon ) handleFlags ( cmd * cobra . Command ) {
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
cc . handleCommonBuilderFlags ( cmd )
2018-04-11 01:54:08 -04:00
cmd . Flags ( ) . Bool ( "cleanDestinationDir" , false , "remove files from destination not found in static directories" )
cmd . Flags ( ) . BoolP ( "buildDrafts" , "D" , false , "include content marked as draft" )
cmd . Flags ( ) . BoolP ( "buildFuture" , "F" , false , "include content with publishdate in the future" )
cmd . Flags ( ) . BoolP ( "buildExpired" , "E" , false , "include expired content" )
cmd . Flags ( ) . StringP ( "contentDir" , "c" , "" , "filesystem path to content directory" )
cmd . Flags ( ) . StringP ( "layoutDir" , "l" , "" , "filesystem path to layout directory" )
cmd . Flags ( ) . StringP ( "cacheDir" , "" , "" , "filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/" )
cmd . Flags ( ) . BoolP ( "ignoreCache" , "" , false , "ignores the cache directory" )
cmd . Flags ( ) . StringP ( "destination" , "d" , "" , "filesystem path to write files to" )
2019-01-13 04:44:38 -05:00
cmd . Flags ( ) . StringSliceP ( "theme" , "t" , [ ] string { } , "themes to use (located in /themes/THEMENAME/)" )
2018-04-11 01:54:08 -04:00
cmd . Flags ( ) . StringVarP ( & cc . baseURL , "baseURL" , "b" , "" , "hostname (and path) to the root, e.g. http://spf13.com/" )
cmd . Flags ( ) . Bool ( "enableGitInfo" , false , "add Git revision, date and author info to the pages" )
cmd . Flags ( ) . BoolVar ( & cc . gc , "gc" , false , "enable to run some cleanup tasks (remove unused cache files) after the build" )
2021-07-05 04:13:41 -04:00
cmd . Flags ( ) . StringVar ( & cc . poll , "poll" , "" , "set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes" )
2022-01-06 04:22:19 -05:00
cmd . Flags ( ) . BoolVar ( & loggers . PanicOnWarning , "panicOnWarning" , false , "panic on first WARNING log" )
2018-04-11 01:54:08 -04:00
cmd . Flags ( ) . Bool ( "templateMetrics" , false , "display metrics about template executions" )
cmd . Flags ( ) . Bool ( "templateMetricsHints" , false , "calculate some improvement hints when combined with --templateMetrics" )
cmd . Flags ( ) . BoolP ( "forceSyncStatic" , "" , false , "copy all files when static is changed." )
cmd . Flags ( ) . BoolP ( "noTimes" , "" , false , "don't sync modification time of files" )
cmd . Flags ( ) . BoolP ( "noChmod" , "" , false , "don't sync permission mode of files" )
cmd . Flags ( ) . BoolP ( "i18n-warnings" , "" , false , "print missing translations" )
2022-02-15 06:57:49 -05:00
cmd . Flags ( ) . BoolP ( "printPathWarnings" , "" , false , "print warnings on duplicate target paths etc." )
2019-01-02 06:33:26 -05:00
cmd . Flags ( ) . StringVarP ( & cc . cpuprofile , "profile-cpu" , "" , "" , "write cpu profile to `file`" )
cmd . Flags ( ) . StringVarP ( & cc . memprofile , "profile-mem" , "" , "" , "write memory profile to `file`" )
2022-02-15 06:57:49 -05:00
cmd . Flags ( ) . BoolVarP ( & cc . printm , "printMemoryUsage" , "" , false , "print memory usage to screen at intervals" )
2019-01-02 06:33:26 -05:00
cmd . Flags ( ) . StringVarP ( & cc . mutexprofile , "profile-mutex" , "" , "" , "write Mutex profile to `file`" )
cmd . Flags ( ) . StringVarP ( & cc . traceprofile , "trace" , "" , "" , "write trace to `file` (not useful in general)" )
// Hide these for now.
cmd . Flags ( ) . MarkHidden ( "profile-cpu" )
cmd . Flags ( ) . MarkHidden ( "profile-mem" )
cmd . Flags ( ) . MarkHidden ( "profile-mutex" )
2018-04-11 01:54:08 -04:00
cmd . Flags ( ) . StringSlice ( "disableKinds" , [ ] string { } , "disable different kind of pages (home, RSS etc.)" )
2018-11-04 12:39:14 -05:00
cmd . Flags ( ) . Bool ( "minify" , false , "minify any supported output format (HTML, XML etc.)" )
2018-04-11 01:54:08 -04:00
// Set bash-completion.
// Each flag must first be defined before using the SetAnnotation() call.
_ = cmd . Flags ( ) . SetAnnotation ( "source" , cobra . BashCompSubdirsInDir , [ ] string { } )
_ = cmd . Flags ( ) . SetAnnotation ( "cacheDir" , cobra . BashCompSubdirsInDir , [ ] string { } )
_ = cmd . Flags ( ) . SetAnnotation ( "destination" , cobra . BashCompSubdirsInDir , [ ] string { } )
_ = cmd . Flags ( ) . SetAnnotation ( "theme" , cobra . BashCompSubdirsInDir , [ ] string { "themes" } )
}
2018-07-21 18:34:17 -04:00
2020-10-21 05:17:48 -04:00
func checkErr ( logger loggers . Logger , err error , s ... string ) {
2018-07-21 18:34:17 -04:00
if err == nil {
return
}
for _ , message := range s {
2020-10-21 05:17:48 -04:00
logger . Errorln ( message )
2018-07-21 18:34:17 -04:00
}
2020-10-21 05:17:48 -04:00
logger . Errorln ( err )
2018-07-21 18:34:17 -04:00
}