Skip to content

Commit

Permalink
feat: add login command (#4)
Browse files Browse the repository at this point in the history
Adds a login command that authenticates the user with the configured email and password.
  • Loading branch information
tlkamp authored Oct 1, 2023
1 parent 1d9f031 commit 15a541b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
27 changes: 27 additions & 0 deletions internal/cmd/login.go
Original file line number Diff line number Diff line change
@@ -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()
},
}
}
1 change: 1 addition & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func newLitterRobotCmd(c *lrc.Client, version string) *cobra.Command {
cmd.AddCommand(
newVersionCmd(version),
newConfigCmd(),
newLoginCmd(c),
)

return cmd
Expand Down

0 comments on commit 15a541b

Please sign in to comment.