github.com/pluralsh/plural-cli@v0.9.5/cmd/plural/config.go (about)

     1  package plural
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  
     7  	"github.com/pluralsh/plural-cli/pkg/config"
     8  	"github.com/pluralsh/plural-cli/pkg/utils"
     9  	"github.com/urfave/cli"
    10  )
    11  
    12  func configCommands() []cli.Command {
    13  	return []cli.Command{
    14  		{
    15  			Name:      "amend",
    16  			Usage:     "modify config",
    17  			ArgsUsage: "[key] [value]",
    18  			Action:    latestVersion(handleAmend),
    19  		},
    20  		{
    21  			Name:      "read",
    22  			Usage:     "dumps config",
    23  			ArgsUsage: "",
    24  			Action:    latestVersion(handleRead),
    25  		},
    26  		{
    27  			Name:   "import",
    28  			Usage:  "imports a new config from a given token",
    29  			Action: latestVersion(handleConfigImport),
    30  		},
    31  	}
    32  }
    33  
    34  func profileCommands() []cli.Command {
    35  	return []cli.Command{
    36  		{
    37  			Name:      "use",
    38  			Usage:     "moves the config in PROFILE to the current config",
    39  			ArgsUsage: "PROFILE",
    40  			Action:    latestVersion(handleUseProfile),
    41  		},
    42  		{
    43  			Name:      "save",
    44  			Usage:     "saves the current config as PROFILE",
    45  			ArgsUsage: "PROFILE",
    46  			Action:    latestVersion(handleSaveProfile),
    47  		},
    48  		{
    49  			Name:      "show",
    50  			Usage:     "displays the configuration for the current profile",
    51  			ArgsUsage: "PROFILE",
    52  			Action:    latestVersion(handleRead),
    53  		},
    54  		{
    55  			Name:      "list",
    56  			Usage:     "lists all saved profiles",
    57  			ArgsUsage: "",
    58  			Action:    latestVersion(listProfiles),
    59  		},
    60  	}
    61  }
    62  
    63  func handleAmend(c *cli.Context) error {
    64  	return config.Amend(c.Args().Get(0), c.Args().Get(1))
    65  }
    66  
    67  func handleRead(c *cli.Context) error {
    68  	conf := config.Read()
    69  	d, err := conf.Marshal()
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	os.Stdout.Write(d)
    75  	return nil
    76  }
    77  
    78  func handleConfigImport(c *cli.Context) error {
    79  	data, err := io.ReadAll(os.Stdin)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	return config.FromToken(string(data))
    85  }
    86  
    87  func handleUseProfile(c *cli.Context) error {
    88  	return config.Profile(c.Args().Get(0))
    89  }
    90  
    91  func handleSaveProfile(c *cli.Context) error {
    92  	conf := config.Read()
    93  	return conf.SaveProfile(c.Args().Get(0))
    94  }
    95  
    96  func listProfiles(c *cli.Context) error {
    97  	profiles, err := config.Profiles()
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	headers := []string{"Name", "Email", "Endpoint"}
   103  	return utils.PrintTable(profiles, headers, func(profile *config.VersionedConfig) ([]string, error) {
   104  		return []string{profile.Metadata.Name, profile.Spec.Email, profile.Spec.BaseUrl()}, nil
   105  	})
   106  }