Skip to content

Commit

Permalink
Switch to cobra
Browse files Browse the repository at this point in the history
  • Loading branch information
rxbn committed Aug 5, 2022
1 parent c229d9b commit 43cf895
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 30 deletions.
24 changes: 24 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"github.com/spf13/cobra"
"os"
)

var completionCmd = &cobra.Command{
Use: "completion [bash|zsh|powershell]",
Short: "Generate completion scripts",
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "powershell"},
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
_ = cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
_ = cmd.Root().GenZshCompletion(os.Stdout)
case "powershell":
_ = cmd.Root().GenPowerShellCompletion(os.Stdout)
}
},
}
35 changes: 35 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"fmt"
"github.com/pquerna/otp/totp"
"github.com/spf13/cobra"
"os"
"time"
)

var rootCmd = &cobra.Command{
Use: "otpgen [secret]",
Short: "Generate one-time passwords",
Long: `otpgen is a command line tool to generate one-time passwords.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
token, err := totp.GenerateCode(args[0], time.Now())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Print(token)
},
}

func init() {
rootCmd.AddCommand(completionCmd, versionCmd)
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
17 changes: 17 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
)

const version = "3.0.0"

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of otpgen",
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version)
},
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/containeroo/otpgen
go 1.16

require (
github.com/spf13/pflag v1.0.5
github.com/xlzd/gotp v0.0.0-20220110052318-fab697c03c2c
github.com/pquerna/otp v1.3.0
github.com/spf13/cobra v1.5.0
)
21 changes: 19 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8WK8raXaxBx6fRVTlJILwEwQGL1I/ByEI=
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pquerna/otp v1.3.0 h1:oJV/SkzR33anKXwQU3Of42rL4wbrffP4uvUf1SvS5Xs=
github.com/pquerna/otp v1.3.0/go.mod h1:dkJfzwRKNiegxyNb54X/3fLwhCynbMspSyWKnvi1AEg=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/xlzd/gotp v0.0.0-20220110052318-fab697c03c2c h1:LZpKQbMSngtN4ycCtogkxYl5ec0FimAA8rSrI4ZMGTM=
github.com/xlzd/gotp v0.0.0-20220110052318-fab697c03c2c/go.mod h1:ndLJ3JKzi3xLmUProq4LLxCuECL93dG9WASNLpHz8qg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
28 changes: 2 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
package main

import (
"fmt"
"log"
"os"

flag "github.com/spf13/pflag"
"github.com/xlzd/gotp"
"github.com/containeroo/otpgen/cmd"
)

const version = "2.0.1"

func main() {
secretKey := flag.StringP("secretkey", "s", "", "supply a valid TOTP secret key to generate a token from")
printVersion := flag.BoolP("version", "v", false, "Print the current version and exit")
flag.Parse()

if *printVersion {
fmt.Println(version)
os.Exit(0)
}

if *secretKey == "" {
log.Fatal("secretkey cannot be empty")
}

if len(*secretKey) != 16 && len(*secretKey) != 32 {
log.Fatal("secretkey must be 16 or 32 characters long")
}

fmt.Print(gotp.NewDefaultTOTP(*secretKey).Now())
cmd.Execute()
}

0 comments on commit 43cf895

Please sign in to comment.