2023-01-04 12:24:36 -05:00
|
|
|
// Copyright 2023 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
|
2023-01-04 12:24:36 -05:00
|
|
|
// limitations under the License.
|
2015-02-01 16:05:47 -05:00
|
|
|
|
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2023-01-04 12:24:36 -05:00
|
|
|
"context"
|
2019-07-31 04:31:26 -04:00
|
|
|
"encoding/json"
|
|
|
|
"os"
|
2021-06-27 12:00:20 -04:00
|
|
|
"time"
|
2016-07-07 12:06:48 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/bep/simplecobra"
|
|
|
|
"github.com/gohugoio/hugo/modules"
|
2019-07-31 04:31:26 -04:00
|
|
|
"github.com/gohugoio/hugo/parser"
|
|
|
|
"github.com/gohugoio/hugo/parser/metadecoders"
|
2015-02-01 16:05:47 -05:00
|
|
|
)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// newConfigCommand creates a new config command and its subcommands.
|
|
|
|
func newConfigCommand() *configCommand {
|
|
|
|
return &configCommand{
|
|
|
|
commands: []simplecobra.Commander{
|
|
|
|
&configMountsCommand{},
|
|
|
|
},
|
|
|
|
}
|
2018-04-09 14:05:09 -04:00
|
|
|
|
2015-12-02 14:00:47 -05:00
|
|
|
}
|
2015-12-02 05:42:53 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
type configCommand struct {
|
|
|
|
r *rootCommand
|
2019-07-31 04:31:26 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
commands []simplecobra.Commander
|
|
|
|
}
|
2020-01-31 03:09:11 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *configCommand) Commands() []simplecobra.Commander {
|
|
|
|
return c.commands
|
|
|
|
}
|
2018-04-11 04:09:45 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *configCommand) Name() string {
|
|
|
|
return "config"
|
2015-12-02 14:00:47 -05:00
|
|
|
}
|
2015-02-17 05:19:30 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *configCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
|
|
|
|
conf, err := c.r.ConfigFromProvider(c.r.configVersionID.Load(), flagsToCfg(cd, nil))
|
2019-07-31 04:31:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
config := conf.configs.Base
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// Print it as JSON.
|
|
|
|
dec := json.NewEncoder(os.Stdout)
|
|
|
|
dec.SetIndent("", " ")
|
|
|
|
dec.SetEscapeHTML(false)
|
2019-07-31 04:31:26 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
if err := dec.Encode(parser.ReplacingJSONMarshaller{Value: config, KeysToLower: true, OmitEmpty: true}); err != nil {
|
|
|
|
return err
|
2019-07-31 04:31:26 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-17 12:45:23 -04:00
|
|
|
func (c *configCommand) Init(cd *simplecobra.Commandeer) error {
|
|
|
|
cmd := cd.CobraCommand
|
2023-01-04 12:24:36 -05:00
|
|
|
cmd.Short = "Print the site configuration"
|
|
|
|
cmd.Long = `Print the site configuration, both default and custom settings.`
|
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
|
|
|
|
2023-05-17 12:45:23 -04:00
|
|
|
func (c *configCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
|
2023-01-04 12:24:36 -05:00
|
|
|
c.r = cd.Root.Command.(*rootCommand)
|
|
|
|
return nil
|
2019-07-31 04:31:26 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
type configModMount struct {
|
2019-07-31 04:31:26 -04:00
|
|
|
Source string `json:"source"`
|
|
|
|
Target string `json:"target"`
|
|
|
|
Lang string `json:"lang,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
type configModMounts struct {
|
|
|
|
verbose bool
|
|
|
|
m modules.Module
|
|
|
|
}
|
|
|
|
|
2022-12-30 03:20:58 -05:00
|
|
|
// MarshalJSON is for internal use only.
|
2023-01-04 12:24:36 -05:00
|
|
|
func (m *configModMounts) MarshalJSON() ([]byte, error) {
|
|
|
|
var mounts []configModMount
|
2019-07-31 04:31:26 -04:00
|
|
|
|
|
|
|
for _, mount := range m.m.Mounts() {
|
2023-01-04 12:24:36 -05:00
|
|
|
mounts = append(mounts, configModMount{
|
2019-07-31 04:31:26 -04:00
|
|
|
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
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
Mounts []configModMount `json:"mounts"`
|
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(),
|
|
|
|
Meta: config.Params,
|
|
|
|
HugoVersion: config.HugoVersion,
|
|
|
|
Mounts: mounts,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-31 04:31:26 -04:00
|
|
|
return json.Marshal(&struct {
|
2023-01-04 12:24:36 -05:00
|
|
|
Path string `json:"path"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Time time.Time `json:"time"`
|
|
|
|
Owner string `json:"owner"`
|
|
|
|
Dir string `json:"dir"`
|
|
|
|
Mounts []configModMount `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
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
type configMountsCommand struct {
|
|
|
|
configCmd *configCommand
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *configMountsCommand) Commands() []simplecobra.Commander {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *configMountsCommand) Name() string {
|
|
|
|
return "mounts"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *configMountsCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args []string) error {
|
|
|
|
r := c.configCmd.r
|
|
|
|
conf, err := r.ConfigFromProvider(r.configVersionID.Load(), flagsToCfg(cd, nil))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range conf.configs.Modules {
|
|
|
|
if err := parser.InterfaceToConfig(&configModMounts{m: m, verbose: r.verbose}, metadecoders.JSON, os.Stdout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-17 12:45:23 -04:00
|
|
|
func (c *configMountsCommand) Init(cd *simplecobra.Commandeer) error {
|
|
|
|
cmd := cd.CobraCommand
|
2023-01-04 12:24:36 -05:00
|
|
|
cmd.Short = "Print the configured file mounts"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-17 12:45:23 -04:00
|
|
|
func (c *configMountsCommand) PreRun(cd, runner *simplecobra.Commandeer) error {
|
2023-01-04 12:24:36 -05:00
|
|
|
c.configCmd = cd.Parent.Command.(*configCommand)
|
|
|
|
return nil
|
|
|
|
}
|