github.com/goliatone/go-envset@v0.7.0/cmd/envset/rc/rc.go (about)

     1  package rc
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/goliatone/go-envset/pkg/config"
     8  	"github.com/urfave/cli/v2"
     9  )
    10  
    11  //GetCommand returns a new cli.Command for the
    12  //rc command.
    13  func GetCommand(cnf *config.Config) *cli.Command {
    14  	return &cli.Command{
    15  		Name:        "config",
    16  		Aliases:     []string{"rc"},
    17  		Usage:       "generate an envsetrc file",
    18  		Description: "creates an envsetrc file with either the current options or the default options",
    19  		Flags: []cli.Flag{
    20  			&cli.BoolFlag{
    21  				Name:  "print",
    22  				Usage: "only print the contents to stdout, don't write file",
    23  			},
    24  			&cli.StringFlag{
    25  				Name:  "filename",
    26  				Usage: "metadata file `name`",
    27  				Value: cnf.Meta.File,
    28  			},
    29  			&cli.StringFlag{
    30  				Name:  "filepath",
    31  				Usage: "metadata file `path`",
    32  				Value: cnf.Meta.Dir,
    33  			},
    34  			&cli.StringFlag{
    35  				Name:  "env-file",
    36  				Value: cnf.Filename,
    37  				Usage: "load environment from `FILE`",
    38  			},
    39  			&cli.BoolFlag{
    40  				Name:  "overwrite",
    41  				Usage: "set true to prevent overwrite metadata file",
    42  				Value: true,
    43  			},
    44  			&cli.BoolFlag{
    45  				Name:  "values",
    46  				Usage: "add flag to show values in the output",
    47  			},
    48  			&cli.BoolFlag{
    49  				Name:  "globals",
    50  				Usage: "include global section",
    51  				Value: false,
    52  			},
    53  			&cli.StringFlag{
    54  				Name:    "secret",
    55  				Usage:   "`password` used to encode hash values",
    56  				EnvVars: []string{"ENVSET_HASH_SECRET"},
    57  			},
    58  		},
    59  		Action: func(c *cli.Context) error {
    60  			fmt.Printf("%s", config.GetDefaultConfig())
    61  			return nil
    62  		},
    63  		Subcommands: []*cli.Command{
    64  			{
    65  				Name:        "get",
    66  				Usage:       "get option value for key",
    67  				UsageText:   "get option value for key",
    68  				Description: "retrieves configuration value of given key",
    69  				Action: func(c *cli.Context) error {
    70  					if c.Args().Len() == 0 {
    71  						return errors.New("envset config get requires exactly one argument, e.g.\nenvset config get <path>")
    72  					}
    73  					key := c.Args().First()
    74  					val := cnf.Get(key)
    75  					fmt.Println(val)
    76  					return nil
    77  				},
    78  			},
    79  			{
    80  				Name:        "list",
    81  				Usage:       "list available key paths",
    82  				UsageText:   "list available key paths",
    83  				Description: "prints a list of all key paths for configuration options",
    84  				Action: func(c *cli.Context) error {
    85  					keys := cnf.ListKeys()
    86  					for _, k := range keys {
    87  						fmt.Println(k)
    88  					}
    89  					return nil
    90  				},
    91  			},
    92  		},
    93  	}
    94  }