github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/config/root.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  
     8  	"github.com/fastly/cli/pkg/argparser"
     9  	"github.com/fastly/cli/pkg/config"
    10  	"github.com/fastly/cli/pkg/global"
    11  	"github.com/fastly/cli/pkg/text"
    12  )
    13  
    14  // RootCommand is the parent command for all subcommands in this package.
    15  // It should be installed under the primary root command.
    16  type RootCommand struct {
    17  	argparser.Base
    18  
    19  	location bool
    20  	reset    bool
    21  }
    22  
    23  // NewRootCommand returns a new command registered in the parent.
    24  func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand {
    25  	var c RootCommand
    26  	c.Globals = g
    27  	c.CmdClause = parent.Command("config", "Display the Fastly CLI configuration")
    28  	c.CmdClause.Flag("location", "Print the location of the CLI configuration file").Short('l').BoolVar(&c.location)
    29  	c.CmdClause.Flag("reset", "Reset the config to a version compatible with the current CLI version").Short('r').BoolVar(&c.reset)
    30  	return &c
    31  }
    32  
    33  // Exec implements the command interface.
    34  func (c *RootCommand) Exec(_ io.Reader, out io.Writer) (err error) {
    35  	if c.reset {
    36  		if err := c.Globals.Config.UseStatic(config.FilePath); err != nil {
    37  			return err
    38  		}
    39  	}
    40  
    41  	if c.location {
    42  		if c.Globals.Flags.Verbose {
    43  			text.Break(out)
    44  		}
    45  		fmt.Fprintln(out, c.Globals.ConfigPath)
    46  		return nil
    47  	}
    48  
    49  	data, err := os.ReadFile(c.Globals.ConfigPath)
    50  	if err != nil {
    51  		c.Globals.ErrLog.Add(err)
    52  		return err
    53  	}
    54  	fmt.Fprintln(out, string(data))
    55  	return nil
    56  }