github.com/rhzs/mockery/v2@v2.31.10/cmd/showconfig.go (about)

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  
     9  	"github.com/spf13/cobra"
    10  	"github.com/spf13/viper"
    11  	"github.com/vektra/mockery/v2/pkg/config"
    12  	"github.com/vektra/mockery/v2/pkg/logging"
    13  	"github.com/vektra/mockery/v2/pkg/stackerr"
    14  	"gopkg.in/yaml.v2"
    15  )
    16  
    17  func NewShowConfigCmd() *cobra.Command {
    18  	return &cobra.Command{
    19  		Use:   "showconfig",
    20  		Short: "Show the yaml config",
    21  		Long:  `Print out a yaml representation of the yaml config file. This does not show config from exterior sources like CLI, environment etc.`,
    22  		RunE:  func(cmd *cobra.Command, args []string) error { return showConfig(cmd, args, viperCfg, os.Stdout) },
    23  	}
    24  }
    25  
    26  func showConfig(
    27  	cmd *cobra.Command,
    28  	args []string,
    29  	v *viper.Viper,
    30  	outputter io.Writer,
    31  ) error {
    32  	if v == nil {
    33  		v = viperCfg
    34  	}
    35  	ctx := context.Background()
    36  	config, err := config.NewConfigFromViper(v)
    37  	if err != nil {
    38  		return stackerr.NewStackErrf(err, "failed to unmarshal config")
    39  	}
    40  	if err := config.Initialize(ctx); err != nil {
    41  		return err
    42  	}
    43  	cfgMap, err := config.CfgAsMap(ctx)
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  	out, err := yaml.Marshal(cfgMap)
    48  	if err != nil {
    49  		return stackerr.NewStackErrf(err, "failed to marshal yaml")
    50  	}
    51  	log, err := logging.GetLogger(config.LogLevel)
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  	log.Info().Msgf("Using config: %s", config.Config)
    56  
    57  	fmt.Fprintf(outputter, "%s", string(out))
    58  	return nil
    59  }