2015-12-07 13:57:01 -05:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
2015-02-01 16:05:47 -05:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2015-02-01 16:05:47 -05:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-02-01 16:05:47 -05:00
|
|
|
//
|
|
|
|
// 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.Print the version number of Hug
|
|
|
|
|
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2019-07-31 04:31:26 -04:00
|
|
|
"encoding/json"
|
2020-01-31 03:09:11 -05:00
|
|
|
"fmt"
|
2019-07-31 04:31:26 -04:00
|
|
|
"os"
|
2015-02-17 05:19:30 -05:00
|
|
|
"reflect"
|
2019-07-31 03:59:32 -04:00
|
|
|
"regexp"
|
2015-02-01 16:05:47 -05:00
|
|
|
"sort"
|
2019-07-31 03:59:32 -04:00
|
|
|
"strings"
|
2021-06-27 12:00:20 -04:00
|
|
|
"time"
|
2016-07-07 12:06:48 -04:00
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
|
|
|
|
2019-07-31 04:31:26 -04:00
|
|
|
"github.com/gohugoio/hugo/parser"
|
|
|
|
"github.com/gohugoio/hugo/parser/metadecoders"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/modules"
|
|
|
|
|
2016-07-07 12:06:48 -04:00
|
|
|
"github.com/spf13/cobra"
|
2015-02-01 16:05:47 -05:00
|
|
|
)
|
|
|
|
|
2018-04-09 14:05:09 -04:00
|
|
|
var _ cmder = (*configCmd)(nil)
|
|
|
|
|
|
|
|
type configCmd struct {
|
2020-01-31 03:09:11 -05:00
|
|
|
*baseBuilderCmd
|
2015-12-02 14:00:47 -05:00
|
|
|
}
|
2015-12-02 05:42:53 -05:00
|
|
|
|
2020-01-31 03:09:11 -05:00
|
|
|
func (b *commandsBuilder) newConfigCmd() *configCmd {
|
2018-04-09 14:05:09 -04:00
|
|
|
cc := &configCmd{}
|
2020-01-31 03:09:11 -05:00
|
|
|
cmd := &cobra.Command{
|
2018-04-09 14:05:09 -04:00
|
|
|
Use: "config",
|
|
|
|
Short: "Print the site configuration",
|
|
|
|
Long: `Print the site configuration, both default and custom settings.`,
|
|
|
|
RunE: cc.printConfig,
|
2020-01-31 03:09:11 -05:00
|
|
|
}
|
2019-07-31 04:31:26 -04:00
|
|
|
|
|
|
|
printMountsCmd := &cobra.Command{
|
|
|
|
Use: "mounts",
|
|
|
|
Short: "Print the configured file mounts",
|
|
|
|
RunE: cc.printMounts,
|
|
|
|
}
|
|
|
|
|
2020-01-31 03:09:11 -05:00
|
|
|
cmd.AddCommand(printMountsCmd)
|
|
|
|
|
|
|
|
cc.baseBuilderCmd = b.newBuilderBasicCmd(cmd)
|
2018-04-11 04:09:45 -04:00
|
|
|
|
2018-04-09 14:05:09 -04:00
|
|
|
return cc
|
2015-12-02 14:00:47 -05:00
|
|
|
}
|
2015-02-17 05:19:30 -05:00
|
|
|
|
2019-07-31 04:31:26 -04:00
|
|
|
func (c *configCmd) printMounts(cmd *cobra.Command, args []string) error {
|
2021-08-31 11:02:51 -04:00
|
|
|
cfg, err := initializeConfig(true, false, false, &c.hugoBuilderCommon, c, nil)
|
2019-07-31 04:31:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2019-07-31 04:31:26 -04:00
|
|
|
allModules := cfg.Cfg.Get("allmodules").(modules.Modules)
|
|
|
|
|
|
|
|
for _, m := range allModules {
|
2021-06-27 09:58:24 -04:00
|
|
|
if err := parser.InterfaceToConfig(&modMounts{m: m, verbose: c.verbose}, metadecoders.JSON, os.Stdout); err != nil {
|
2019-07-31 04:31:26 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *configCmd) printConfig(cmd *cobra.Command, args []string) error {
|
2021-08-31 11:02:51 -04:00
|
|
|
cfg, err := initializeConfig(true, false, false, &c.hugoBuilderCommon, c, nil)
|
2017-02-04 22:20:06 -05:00
|
|
|
if err != nil {
|
2015-12-02 14:00:47 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-02-17 05:19:30 -05:00
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
allSettings := cfg.Cfg.Get("").(maps.Params)
|
2015-12-02 14:00:47 -05:00
|
|
|
|
2019-07-31 03:59:32 -04:00
|
|
|
// We need to clean up this, but we store objects in the config that
|
|
|
|
// isn't really interesting to the end user, so filter these.
|
|
|
|
ignoreKeysRe := regexp.MustCompile("client|sorted|filecacheconfigs|allmodules|multilingual")
|
|
|
|
|
|
|
|
separator := ": "
|
|
|
|
|
|
|
|
if len(cfg.configFiles) > 0 && strings.HasSuffix(cfg.configFiles[0], ".toml") {
|
2015-12-02 14:00:47 -05:00
|
|
|
separator = " = "
|
|
|
|
}
|
|
|
|
|
|
|
|
var keys []string
|
|
|
|
for k := range allSettings {
|
2019-07-31 03:59:32 -04:00
|
|
|
if ignoreKeysRe.MatchString(k) {
|
|
|
|
continue
|
|
|
|
}
|
2015-12-02 14:00:47 -05:00
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, k := range keys {
|
|
|
|
kv := reflect.ValueOf(allSettings[k])
|
|
|
|
if kv.Kind() == reflect.String {
|
2020-01-31 03:09:11 -05:00
|
|
|
fmt.Printf("%s%s\"%+v\"\n", k, separator, allSettings[k])
|
2015-12-02 14:00:47 -05:00
|
|
|
} else {
|
2020-01-31 03:09:11 -05:00
|
|
|
fmt.Printf("%s%s%+v\n", k, separator, allSettings[k])
|
2015-02-01 16:05:47 -05:00
|
|
|
}
|
2015-12-02 14:00:47 -05:00
|
|
|
}
|
2015-12-02 05:42:53 -05:00
|
|
|
|
2015-12-02 14:00:47 -05:00
|
|
|
return nil
|
2015-02-01 16:05:47 -05:00
|
|
|
}
|
2019-07-31 04:31:26 -04:00
|
|
|
|
|
|
|
type modMounts struct {
|
2021-06-27 09:58:24 -04:00
|
|
|
verbose bool
|
|
|
|
m modules.Module
|
2019-07-31 04:31:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type modMount struct {
|
|
|
|
Source string `json:"source"`
|
|
|
|
Target string `json:"target"`
|
|
|
|
Lang string `json:"lang,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *modMounts) MarshalJSON() ([]byte, error) {
|
|
|
|
var mounts []modMount
|
|
|
|
|
|
|
|
for _, mount := range m.m.Mounts() {
|
|
|
|
mounts = append(mounts, modMount{
|
|
|
|
Source: mount.Source,
|
|
|
|
Target: mount.Target,
|
|
|
|
Lang: mount.Lang,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-27 09:58:24 -04:00
|
|
|
var ownerPath string
|
|
|
|
if m.m.Owner() != nil {
|
|
|
|
ownerPath = m.m.Owner().Path()
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.verbose {
|
|
|
|
config := m.m.Config()
|
|
|
|
return json.Marshal(&struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
Path string `json:"path"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Time time.Time `json:"time"`
|
|
|
|
Owner string `json:"owner"`
|
|
|
|
Dir string `json:"dir"`
|
|
|
|
Meta map[string]any `json:"meta"`
|
|
|
|
HugoVersion modules.HugoVersion `json:"hugoVersion"`
|
2021-06-27 09:58:24 -04:00
|
|
|
|
|
|
|
Mounts []modMount `json:"mounts"`
|
|
|
|
}{
|
|
|
|
Path: m.m.Path(),
|
|
|
|
Version: m.m.Version(),
|
2021-06-27 12:00:20 -04:00
|
|
|
Time: m.m.Time(),
|
2021-06-27 09:58:24 -04:00
|
|
|
Owner: ownerPath,
|
|
|
|
Dir: m.m.Dir(),
|
|
|
|
Meta: config.Params,
|
|
|
|
HugoVersion: config.HugoVersion,
|
|
|
|
Mounts: mounts,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-31 04:31:26 -04:00
|
|
|
return json.Marshal(&struct {
|
2021-06-27 09:58:24 -04:00
|
|
|
Path string `json:"path"`
|
|
|
|
Version string `json:"version"`
|
2021-06-27 12:06:52 -04:00
|
|
|
Time time.Time `json:"time"`
|
2021-06-27 09:58:24 -04:00
|
|
|
Owner string `json:"owner"`
|
|
|
|
Dir string `json:"dir"`
|
|
|
|
Mounts []modMount `json:"mounts"`
|
2019-07-31 04:31:26 -04:00
|
|
|
}{
|
2021-06-27 09:58:24 -04:00
|
|
|
Path: m.m.Path(),
|
|
|
|
Version: m.m.Version(),
|
2021-06-27 12:00:20 -04:00
|
|
|
Time: m.m.Time(),
|
2021-06-27 09:58:24 -04:00
|
|
|
Owner: ownerPath,
|
|
|
|
Dir: m.m.Dir(),
|
|
|
|
Mounts: mounts,
|
2019-07-31 04:31:26 -04:00
|
|
|
})
|
2021-06-27 09:58:24 -04:00
|
|
|
|
2019-07-31 04:31:26 -04:00
|
|
|
}
|