Skip to content

Commit

Permalink
Create parent folders and config file if they don't exist. Remove unn…
Browse files Browse the repository at this point in the history
…ecessary token outputs.
  • Loading branch information
UtkarshVerma committed Jul 24, 2020
1 parent de76eeb commit ecd60f0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
23 changes: 13 additions & 10 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"os/exec"
"path"
"runtime"
"time"

Expand Down Expand Up @@ -156,7 +157,6 @@ func GetToken(conf *oauth2.Config, token **oauth2.Token, params *params) {
log.Fatalf("Error: Unable to retrieve token from the web.\n%v", err)
} else {
*token = tok
fmt.Printf("%+v", *Token)
fmt.Println("qGmail has been successfully authorized.")
}
}
Expand All @@ -175,8 +175,7 @@ func getAuthCode(conf *oauth2.Config, authURL string, params *params) {
if *mustPaste {
fmt.Println("Paste the authorization code here:")
if _, err := fmt.Scan(code); err != nil {
fmt.Println()
log.Fatalf("Unable to read authorization code: %v", err)
log.Fatalf("\nUnable to read authorization code: %v", err)
}
fmt.Println()
} else {
Expand All @@ -198,14 +197,18 @@ func openURL(url string) {
}
}

func SaveToken(tokenFile string, token *oauth2.Token) {
// can't create a folder
f, err := os.OpenFile(tokenFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("Error: Unable to cache oauth token.\n%v", err)
func SaveToken(tokenPath string, token *oauth2.Token) {
tokenFile, _ := json.Marshal(*token)

// Create parent folders if non-existent.
tokenDir := path.Dir(tokenPath)
if _, err := os.Stat(tokenDir); os.IsNotExist(err) {
os.MkdirAll(tokenDir, os.ModePerm)
}

if err := ioutil.WriteFile(tokenPath, tokenFile, 0600); err != nil {
log.Fatalf("Error: Unable to cache OAuth token.\n%v", err)
}
defer f.Close()
json.NewEncoder(f).Encode(token)
}

func ReadToken(tokenFile string, token **oauth2.Token) error {
Expand Down
14 changes: 10 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,19 @@ func init() {
*config.TokenFile, _ = homedir.Expand(*config.TokenFile)
}

func (conf *conf) read(configFile string) {
if _, err := os.Stat(configFile); os.IsNotExist(err) {
log.Fatal(err)
func (conf *conf) read(configPath string) {
if _, err := os.Stat(configPath); os.IsNotExist(err) {
// If the config file doesn't exist at default path, create it.
if defaultPath, _ := homedir.Expand("~/.config/qgmail/config.json"); configPath == defaultPath {
configFile, _ := json.MarshalIndent(*config, "", "\t")
_ = ioutil.WriteFile(configPath, configFile, 0644)
} else {
log.Fatal(err)
}
} else {
c := &tmpConf{}

f, _ := os.Open(configFile)
f, _ := os.Open(configPath)
defer f.Close()

byteValue, _ := ioutil.ReadAll(f)
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func main() {
fmt.Println("Authorization token not found. Fetching a new one...")
auth.GetToken(oAuthConf, token, auth.Params)
auth.SaveToken(tokenFile, *token)
fmt.Printf("%+v", *token)
}

if !cli.InitMode {
Expand Down

0 comments on commit ecd60f0

Please sign in to comment.