Debian Codegraph @

commands/helpers.go

raw
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 }
83
84 // Flags that we for some reason don't want to expose in the project config.
85 internalKeySet := map[string]bool{
86 "quiet": true,
87 "verbose": true,
88 "watch": true,
89 "liveReloadPort": true,
90 "renderToMemory": true,
91 "clock": true,
92 }
93
94 cmd := cd.CobraCommand
95 flags := cmd.Flags()
96
97 flags.VisitAll(func(f *pflag.Flag) {
98 if f.Changed {
99 targetKey := f.Name
100 if internalKeySet[targetKey] {
101 targetKey = "internal." + targetKey
102 } else if mapped, ok := keyMap[targetKey]; ok {
103 targetKey = mapped
104 }
105 setValueFromFlag(flags, f.Name, cfg, targetKey, false)
106 if additionalConfigBase != "" {
107 setValueFromFlag(flags, f.Name, cfg, additionalConfigBase+"."+targetKey, true)
108 }
109 }
110 })
111
112 return cfg
113 }
114
115 func mkdir(x ...string) {
116 p := filepath.Join(x...)
117 err := os.MkdirAll(p, 0o777) // before umask
118 if err != nil {
119 log.Fatal(err)
120 }
121 }