Skip to content

Commit

Permalink
add follow option to generate + sort accounts on list (#72)
Browse files Browse the repository at this point in the history
* add follow option to generate + sort accounts on list

* code review

* Apply suggestions from code review

Co-authored-by: Balazs Nadasdi <[email protected]>

---------

Co-authored-by: Balazs Nadasdi <[email protected]>
  • Loading branch information
Olivier Bouchet and yitsushi authored May 1, 2023
1 parent 59c28f2 commit 0db55f8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,26 @@ totp-cli set-prefix ns account myprefix
totp-cli set-prefix ns account --clear
```

To generate an OTP, you simply use the `generate` command like:

```shell
totp-cli generate namespace account
Password: ***
889840
```

You can also use the `--follow` flag on the `generate` command if you want to
have the OTP token automatically refreshed once expired:

```shell
totp-cli generate --follow namespace account
Password: ***
889840
343555
463346
```


### Changing the location of the credentials file

Simply put this into your `.zshrc` (or `.{YourShell}rc` or `.profile`):
Expand Down
28 changes: 27 additions & 1 deletion internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func (c *Generate) Execute(opts *commander.CommandHelper) {
return
}

mustFollow := opts.Flag("follow")

storage, err := s.PrepareStorage()
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
Expand All @@ -48,6 +50,27 @@ func (c *Generate) Execute(opts *commander.CommandHelper) {
os.Exit(1)
}

code := generateCode(account)
fmt.Println(code)

if !mustFollow {
return
}

previousCode := code

for {
code := generateCode(account)
if code != previousCode {
fmt.Println(code)
previousCode = code
}

time.Sleep(time.Second)
}
}

func generateCode(account *s.Account) string {
code, err := security.GenerateOTPCode(account.Token, time.Now())
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
Expand All @@ -57,13 +80,16 @@ func (c *Generate) Execute(opts *commander.CommandHelper) {
code = account.Prefix + code
}

fmt.Println(code)
return code
}

// NewGenerate creates a new Generate command.
func NewGenerate(appName string) *commander.CommandWrapper {
return &commander.CommandWrapper{
Handler: &Generate{},
Arguments: []*commander.Argument{
{Name: "follow", Type: "bool", Value: false},
},
Help: &commander.CommandDescriptor{
Name: "generate",
ShortDescription: "Generate a specific OTP",
Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"sort"

"github.com/yitsushi/go-commander"

Expand Down Expand Up @@ -35,6 +36,10 @@ func (c *List) Execute(opts *commander.CommandHelper) {
os.Exit(1)
}

sort.Slice(namespace.Accounts, func(i, j int) bool {
return namespace.Accounts[i].Name < namespace.Accounts[j].Name
})

for _, account := range namespace.Accounts {
fmt.Printf("%s.%s\n", namespace.Name, account.Name)
}
Expand Down

0 comments on commit 0db55f8

Please sign in to comment.