| 1 |
// Copyright 2024 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 |
"errors" |
| 18 |
"fmt" |
| 19 |
"log" |
| 20 |
"os" |
| 21 |
"path/filepath" |
| 22 |
"strings" |
| 23 |
|
| 24 |
"github.com/bep/simplecobra" |
| 25 |
"github.com/gohugoio/hugo/config" |
| 26 |
"github.com/spf13/pflag" |
| 27 |
) |
| 28 |
|
| 29 |
const ( |
| 30 |
ansiEsc = "\u001B" |
| 31 |
clearLine = "\r\033[K" |
| 32 |
hideCursor = ansiEsc + "[?25l" |
| 33 |
showCursor = ansiEsc + "[?25h" |
| 34 |
) |
| 35 |
|
| 36 |
func newUserError(a ...any) *simplecobra.CommandError { |
| 37 |
return &simplecobra.CommandError{Err: errors.New(fmt.Sprint(a...))} |
| 38 |
} |
| 39 |
|
| 40 |
func setValueFromFlag(flags *pflag.FlagSet, key string, cfg config.Provider, targetKey string, force bool) { |
| 41 |
key = strings.TrimSpace(key) |
| 42 |
if (force && flags.Lookup(key) != nil) || flags.Changed(key) { |
| 43 |
f := flags.Lookup(key) |
| 44 |
configKey := key |
| 45 |
if targetKey != "" { |
| 46 |
configKey = targetKey |
| 47 |
} |
| 48 |
// Gotta love this API. |
| 49 |
switch f.Value.Type() { |
| 50 |
case "bool": |
| 51 |
bv, _ := flags.GetBool(key) |
| 52 |
cfg.Set(configKey, bv) |
| 53 |
case "string": |
| 54 |
cfg.Set(configKey, f.Value.String()) |
| 55 |
case "stringSlice": |
| 56 |
bv, _ := flags.GetStringSlice(key) |
| 57 |
cfg.Set(configKey, bv) |
| 58 |
case "int": |
| 59 |
iv, _ := flags.GetInt(key) |
| 60 |
cfg.Set(configKey, iv) |
| 61 |
default: |
| 62 |
panic(fmt.Sprintf("update switch with %s", f.Value.Type())) |
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func flagsToCfg(cd *simplecobra.Commandeer, cfg config.Provider) config.Provider { |
| 69 |
return flagsToCfgWithAdditionalConfigBase(cd, cfg, "") |
| 70 |
} |
| 71 |
|
| 72 |
func flagsToCfgWithAdditionalConfigBase(cd *simplecobra.Commandeer, cfg config.Provider, additionalConfigBase string) config.Provider { |
| 73 |
if cfg == nil { |
| 74 |
cfg = config.New() |
| 75 |
} |
| 76 |
|
| 77 |
// Flags with a different name in the config. |
| 78 |
keyMap := map[string]string{ |
| 79 |
"minify": "minify.minifyOutput", |
| 80 |
"destination": "publishDir", |
| 81 |
"editor": "newContentEditor", |
| 82 |
"cleanDestinationDir": "build.cleanDestinationDir.enable", |
| 83 |
} |
| 84 |
|
| 85 |
// Flags that we for some reason don't want to expose in the project config. |
| 86 |
internalKeySet := map[string]bool{ |
| 87 |
"quiet": true, |
| 88 |
"verbose": true, |
| 89 |
"watch": true, |
| 90 |
"liveReloadPort": true, |
| 91 |
"renderToMemory": true, |
| 92 |
"clock": true, |
| 93 |
} |
| 94 |
|
| 95 |
cmd := cd.CobraCommand |
| 96 |
flags := cmd.Flags() |
| 97 |
|
| 98 |
flags.VisitAll(func(f *pflag.Flag) { |
| 99 |
if f.Changed { |
| 100 |
targetKey := f.Name |
| 101 |
if internalKeySet[targetKey] { |
| 102 |
targetKey = "internal." + targetKey |
| 103 |
} else if mapped, ok := keyMap[targetKey]; ok { |
| 104 |
targetKey = mapped |
| 105 |
} |
| 106 |
setValueFromFlag(flags, f.Name, cfg, targetKey, false) |
| 107 |
if additionalConfigBase != "" { |
| 108 |
setValueFromFlag(flags, f.Name, cfg, additionalConfigBase+"."+targetKey, true) |
| 109 |
} |
| 110 |
} |
| 111 |
}) |
| 112 |
|
| 113 |
return cfg |
| 114 |
} |
| 115 |
|
| 116 |
func mkdir(x ...string) { |
| 117 |
p := filepath.Join(x...) |
| 118 |
err := os.MkdirAll(p, 0o777) // before umask |
| 119 |
if err != nil { |
| 120 |
log.Fatal(err) |
| 121 |
} |
| 122 |
} |