github.com/fighterlyt/hugo@v0.47.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  	"reflect"
    18  	"sort"
    19  
    20  	"github.com/spf13/cobra"
    21  	jww "github.com/spf13/jwalterweatherman"
    22  	"github.com/spf13/viper"
    23  )
    24  
    25  var _ cmder = (*configCmd)(nil)
    26  
    27  type configCmd struct {
    28  	hugoBuilderCommon
    29  	*baseCmd
    30  }
    31  
    32  func newConfigCmd() *configCmd {
    33  	cc := &configCmd{}
    34  	cc.baseCmd = newBaseCmd(&cobra.Command{
    35  		Use:   "config",
    36  		Short: "Print the site configuration",
    37  		Long:  `Print the site configuration, both default and custom settings.`,
    38  		RunE:  cc.printConfig,
    39  	})
    40  
    41  	cc.cmd.Flags().StringVarP(&cc.source, "source", "s", "", "filesystem path to read files relative from")
    42  
    43  	return cc
    44  }
    45  
    46  func (c *configCmd) printConfig(cmd *cobra.Command, args []string) error {
    47  	cfg, err := initializeConfig(true, false, &c.hugoBuilderCommon, c, nil)
    48  
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	allSettings := cfg.Cfg.(*viper.Viper).AllSettings()
    54  
    55  	var separator string
    56  	if allSettings["metadataformat"] == "toml" {
    57  		separator = " = "
    58  	} else {
    59  		separator = ": "
    60  	}
    61  
    62  	var keys []string
    63  	for k := range allSettings {
    64  		keys = append(keys, k)
    65  	}
    66  	sort.Strings(keys)
    67  	for _, k := range keys {
    68  		kv := reflect.ValueOf(allSettings[k])
    69  		if kv.Kind() == reflect.String {
    70  			jww.FEEDBACK.Printf("%s%s\"%+v\"\n", k, separator, allSettings[k])
    71  		} else {
    72  			jww.FEEDBACK.Printf("%s%s%+v\n", k, separator, allSettings[k])
    73  		}
    74  	}
    75  
    76  	return nil
    77  }