Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Commit

Permalink
fix: create dir and file before writing to it
Browse files Browse the repository at this point in the history
  • Loading branch information
mainawycliffe committed Dec 9, 2019
1 parent 209864a commit 222d1bd
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/mitchellh/go-homedir"
)

const refreshTokenFilePath = ".kamanda/refresh_token.json"
const configDir = ".kamanda"

type RefreshToken struct {
ClientID string `json:"client_id"`
Expand Down Expand Up @@ -48,9 +48,25 @@ func SaveRefreshToken(token RefreshToken) error {
if err != nil {
return fmt.Errorf("Error converting to json: %w", err)
}
err = ioutil.WriteFile(fmt.Sprintf("%s/%s", home, refreshTokenFilePath), jsonToken, os.ModeAppend)
if err != nil {
filePath := fmt.Sprintf("%s/%s", home, refreshTokenFilePath)
_, err = os.Stat(filePath)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("Error saving configs: %w", err)
}
if os.IsNotExist(err) {
err := os.MkdirAll(fmt.Sprintf("%s/%s", home, configDir), os.ModePerm)
if err != nil {
return fmt.Errorf("Error creating refresh token dir: %w", err)
}
}
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, os.ModePerm)
if err != nil {
return fmt.Errorf("Error saving refresh token: %w", err)
}
defer file.Close()
_, err = file.Write(jsonToken)
if err != nil {
return fmt.Errorf("Error saving refresh token: %w", err)
}
return nil
}

0 comments on commit 222d1bd

Please sign in to comment.