From 05bd38835fbe5f706059b04dde805d1da3668cb2 Mon Sep 17 00:00:00 2001 From: Balazs Nadasdi Date: Fri, 30 Sep 2016 15:03:03 +0200 Subject: [PATCH] Initialize credentials when it does not exist --- commands.go | 35 +++++++++++++++++++++++++++++++++++ main.go | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index d9193d5..2febefd 100644 --- a/commands.go +++ b/commands.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "os" "os/user" "path/filepath" "runtime" @@ -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() @@ -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() +} diff --git a/main.go b/main.go index 6d0b05f..978347c 100644 --- a/main.go +++ b/main.go @@ -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()