github.com/gohugoio/hugo@v0.88.1/commands/config.go (about)

     1  // Copyright 2015 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.Print the version number of Hug
    13  
    14  package commands
    15  
    16  import (
    17  	"encoding/json"
    18  	"fmt"
    19  	"os"
    20  	"reflect"
    21  	"regexp"
    22  	"sort"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/gohugoio/hugo/common/maps"
    27  
    28  	"github.com/gohugoio/hugo/parser"
    29  	"github.com/gohugoio/hugo/parser/metadecoders"
    30  
    31  	"github.com/gohugoio/hugo/modules"
    32  
    33  	"github.com/spf13/cobra"
    34  )
    35  
    36  var _ cmder = (*configCmd)(nil)
    37  
    38  type configCmd struct {
    39  	*baseBuilderCmd
    40  }
    41  
    42  func (b *commandsBuilder) newConfigCmd() *configCmd {
    43  	cc := &configCmd{}
    44  	cmd := &cobra.Command{
    45  		Use:   "config",
    46  		Short: "Print the site configuration",
    47  		Long:  `Print the site configuration, both default and custom settings.`,
    48  		RunE:  cc.printConfig,
    49  	}
    50  
    51  	printMountsCmd := &cobra.Command{
    52  		Use:   "mounts",
    53  		Short: "Print the configured file mounts",
    54  		RunE:  cc.printMounts,
    55  	}
    56  
    57  	cmd.AddCommand(printMountsCmd)
    58  
    59  	cc.baseBuilderCmd = b.newBuilderBasicCmd(cmd)
    60  
    61  	return cc
    62  }
    63  
    64  func (c *configCmd) printMounts(cmd *cobra.Command, args []string) error {
    65  	cfg, err := initializeConfig(true, false, false, &c.hugoBuilderCommon, c, nil)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	allModules := cfg.Cfg.Get("allmodules").(modules.Modules)
    71  
    72  	for _, m := range allModules {
    73  		if err := parser.InterfaceToConfig(&modMounts{m: m, verbose: c.verbose}, metadecoders.JSON, os.Stdout); err != nil {
    74  			return err
    75  		}
    76  	}
    77  	return nil
    78  }
    79  
    80  func (c *configCmd) printConfig(cmd *cobra.Command, args []string) error {
    81  	cfg, err := initializeConfig(true, false, false, &c.hugoBuilderCommon, c, nil)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	allSettings := cfg.Cfg.Get("").(maps.Params)
    87  
    88  	// We need to clean up this, but we store objects in the config that
    89  	// isn't really interesting to the end user, so filter these.
    90  	ignoreKeysRe := regexp.MustCompile("client|sorted|filecacheconfigs|allmodules|multilingual")
    91  
    92  	separator := ": "
    93  
    94  	if len(cfg.configFiles) > 0 && strings.HasSuffix(cfg.configFiles[0], ".toml") {
    95  		separator = " = "
    96  	}
    97  
    98  	var keys []string
    99  	for k := range allSettings {
   100  		if ignoreKeysRe.MatchString(k) {
   101  			continue
   102  		}
   103  		keys = append(keys, k)
   104  	}
   105  	sort.Strings(keys)
   106  	for _, k := range keys {
   107  		kv := reflect.ValueOf(allSettings[k])
   108  		if kv.Kind() == reflect.String {
   109  			fmt.Printf("%s%s\"%+v\"\n", k, separator, allSettings[k])
   110  		} else {
   111  			fmt.Printf("%s%s%+v\n", k, separator, allSettings[k])
   112  		}
   113  	}
   114  
   115  	return nil
   116  }
   117  
   118  type modMounts struct {
   119  	verbose bool
   120  	m       modules.Module
   121  }
   122  
   123  type modMount struct {
   124  	Source string `json:"source"`
   125  	Target string `json:"target"`
   126  	Lang   string `json:"lang,omitempty"`
   127  }
   128  
   129  func (m *modMounts) MarshalJSON() ([]byte, error) {
   130  	var mounts []modMount
   131  
   132  	for _, mount := range m.m.Mounts() {
   133  		mounts = append(mounts, modMount{
   134  			Source: mount.Source,
   135  			Target: mount.Target,
   136  			Lang:   mount.Lang,
   137  		})
   138  	}
   139  
   140  	var ownerPath string
   141  	if m.m.Owner() != nil {
   142  		ownerPath = m.m.Owner().Path()
   143  	}
   144  
   145  	if m.verbose {
   146  		config := m.m.Config()
   147  		return json.Marshal(&struct {
   148  			Path        string                 `json:"path"`
   149  			Version     string                 `json:"version"`
   150  			Time        time.Time              `json:"time"`
   151  			Owner       string                 `json:"owner"`
   152  			Dir         string                 `json:"dir"`
   153  			Meta        map[string]interface{} `json:"meta"`
   154  			HugoVersion modules.HugoVersion    `json:"hugoVersion"`
   155  
   156  			Mounts []modMount `json:"mounts"`
   157  		}{
   158  			Path:        m.m.Path(),
   159  			Version:     m.m.Version(),
   160  			Time:        m.m.Time(),
   161  			Owner:       ownerPath,
   162  			Dir:         m.m.Dir(),
   163  			Meta:        config.Params,
   164  			HugoVersion: config.HugoVersion,
   165  			Mounts:      mounts,
   166  		})
   167  	}
   168  
   169  	return json.Marshal(&struct {
   170  		Path    string     `json:"path"`
   171  		Version string     `json:"version"`
   172  		Time    time.Time  `json:"time"`
   173  		Owner   string     `json:"owner"`
   174  		Dir     string     `json:"dir"`
   175  		Mounts  []modMount `json:"mounts"`
   176  	}{
   177  		Path:    m.m.Path(),
   178  		Version: m.m.Version(),
   179  		Time:    m.m.Time(),
   180  		Owner:   ownerPath,
   181  		Dir:     m.m.Dir(),
   182  		Mounts:  mounts,
   183  	})
   184  
   185  }