Skip to content

Commit

Permalink
Added clipboard feature to generate command
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
Felix Breidenstein committed Mar 2, 2020
1 parent 4096987 commit 0024aba
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/codemonauts/shared-2fa/config"

"github.com/atotto/clipboard"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
Expand Down Expand Up @@ -44,12 +45,22 @@ var generateCmd = &cobra.Command{
totp := gotp.NewDefaultTOTP(*data.Parameter.Value)
key, expiration := totp.NowWithExpiration()
exp := expiration - time.Now().Unix()

clipboardFlag, _ := cmd.Flags().GetBool("clipboard")
if clipboardFlag {
if clipboard.Unsupported {
color.Red("Clipboard functionality is not supported on your system")
} else {
clipboard.WriteAll(key)
}
}
fmt.Printf("%s (%s)\n", key, colorExpiration(exp))
},
}

func init() {
rootCmd.AddCommand(generateCmd)
generateCmd.Flags().BoolP("clipboard", "c", false, "Write token to clipboard")
}

func colorExpiration(duration int64) string {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/codemonauts/shared-2fa
go 1.13

require (
github.com/atotto/clipboard v0.1.2
github.com/aws/aws-sdk-go v1.29.7
github.com/fatih/color v1.9.0
github.com/mitchellh/go-homedir v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aws/aws-sdk-go v1.29.7 h1:mwe1Bls/BsB3hB3I9CtUIWSpe1u3wdPcwdvtD9lkzsU=
github.com/aws/aws-sdk-go v1.29.7/go.mod h1:1KvfttTE3SPKMpo8g2c6jL3ZKfXtFvKscTgahTma5Xg=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down

0 comments on commit 0024aba

Please sign in to comment.