Skip to content

Commit

Permalink
Initialize credentials when it does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Balazs Nadasdi committed Sep 30, 2016
1 parent 2554868 commit 05bd388
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"os"
"os/user"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -37,6 +38,12 @@ var commandDescriptions map[string]string = map[string]string{
}

func prepareStorage() {
initStorage()

if storage != nil {
return
}

pin := AskPIN(32, "")

currentUser, err := user.Current()
Expand Down Expand Up @@ -236,3 +243,31 @@ func askForAddTokenDetails() (namespace, account, token string) {

return
}

func initStorage() {
currentUser, err := user.Current()
check(err)
homePath := currentUser.HomeDir
documentDirectory := filepath.Join(homePath, ".config/totp-cli")

if _, err := os.Stat(documentDirectory); err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(documentDirectory, 0700)
check(err)
} else {
check(err)
}
}

if _, err := os.Stat(filepath.Join(documentDirectory, "credentials")); err == nil {
return
}

pin := AskPIN(32, "You PIN/Password (do not forget it)")
storage = &Storage{
File: filepath.Join(documentDirectory, "credentials"),
PIN: pin,
}

storage.Save()
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import "flag"

const AppName string = "totp-cli"
const AppVersion string = "1.0.1"
const AppVersion string = "1.0.2"

func main() {
flag.Parse()
Expand Down

0 comments on commit 05bd388

Please sign in to comment.