From 15a541b7bbb2272fadeb5a7ec24cef3bcf020d64 Mon Sep 17 00:00:00 2001 From: Traci Kamp Date: Sun, 1 Oct 2023 11:01:30 -0700 Subject: [PATCH] feat: add login command (#4) Adds a login command that authenticates the user with the configured email and password. --- README.md | 9 +++++++++ internal/cmd/login.go | 27 +++++++++++++++++++++++++++ internal/cmd/root.go | 1 + 3 files changed, 37 insertions(+) create mode 100644 internal/cmd/login.go diff --git a/README.md b/README.md index 7fd674c..280fd8d 100644 --- a/README.md +++ b/README.md @@ -59,3 +59,12 @@ $ litterrobot config show --show-secrets email: email@example.com password: changeIt ``` + +### Login +Use the `login` command to authenticate to Litter Robot. + +This command writes the `token` to the config file. Keep it secret! + +```console +$ litterobot login +``` diff --git a/internal/cmd/login.go b/internal/cmd/login.go new file mode 100644 index 0000000..4d8e73d --- /dev/null +++ b/internal/cmd/login.go @@ -0,0 +1,27 @@ +package cmd + +import ( + "context" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + lrc "github.com/tlkamp/litter-api/v2/pkg/client" +) + +func newLoginCmd(c *lrc.Client) *cobra.Command { + return &cobra.Command{ + Use: "login", + Short: "log into the litter robot 3", + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { + c = lrc.New(viper.GetString("email"), viper.GetString("password")) + + if err := c.Login(context.Background()); err != nil { + return err + } + + viper.Set("token", c.Token()) + return viper.WriteConfig() + }, + } +} diff --git a/internal/cmd/root.go b/internal/cmd/root.go index f64c267..89f393b 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -32,6 +32,7 @@ func newLitterRobotCmd(c *lrc.Client, version string) *cobra.Command { cmd.AddCommand( newVersionCmd(version), newConfigCmd(), + newLoginCmd(c), ) return cmd