Skip to content

Commit

Permalink
create platform independent token file (fixes #4) (#5)
Browse files Browse the repository at this point in the history
* create platform independent token file (fixes #4)

* uncapitalize error message strings

Since the release of Staticcheck 2019.1 ST1005 seems to be enabled which
triggers the error message: 'error strings should not be capitalized'.

* use filepath.Join() and set tokenFile as a flag
  • Loading branch information
jnsn authored and jessfraz committed Jan 2, 2019
1 parent a058aac commit 5cd66cc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Flags:
-d, --debug enable debug logging (default: false)
-e, --export export existing filters (default: false)
-f, --creds-file Gmail credential file (or env var GMAIL_CREDENTIAL_FILE) (default: <none>)
-t, --token-file Gmail oauth token file (default: /tmp/token.json)

Commands:

Expand Down
8 changes: 4 additions & 4 deletions gmail.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// getClient retrieves a token, saves the token, then returns the generated client.
func getClient(ctx context.Context, config *oauth2.Config) (*http.Client, error) {
func getClient(ctx context.Context, tokenFile string, config *oauth2.Config) (*http.Client, error) {
// Try reading the token from the file.
tok, err := tokenFromFile(tokenFile)
if err != nil {
Expand Down Expand Up @@ -41,12 +41,12 @@ func getTokenFromWeb(ctx context.Context, config *oauth2.Config) (*oauth2.Token,

var authCode string
if _, err := fmt.Scan(&authCode); err != nil {
return nil, fmt.Errorf("Unable to read authorization code: %v", err)
return nil, fmt.Errorf("unable to read authorization code: %v", err)
}

tok, err := config.Exchange(ctx, authCode)
if err != nil {
return nil, fmt.Errorf("Unable to retrieve token from web: %v", err)
return nil, fmt.Errorf("unable to retrieve token from web: %v", err)
}

return tok, nil
Expand All @@ -71,7 +71,7 @@ func saveToken(path string, token *oauth2.Token) error {

f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("Unable to cache oauth token: %v", err)
return fmt.Errorf("unable to cache oauth token: %v", err)
}
defer f.Close()

Expand Down
13 changes: 9 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/genuinetools/pkg/cli"
Expand All @@ -18,13 +19,14 @@ import (
)

const (
tokenFile = "/tmp/token.json"
gmailUser = "me"
)

var (
credsFile string

tokenFile string

api *gmail.Service

debug bool
Expand Down Expand Up @@ -52,6 +54,9 @@ func main() {
p.FlagSet.StringVar(&credsFile, "creds-file", os.Getenv("GMAIL_CREDENTIAL_FILE"), "Gmail credential file (or env var GMAIL_CREDENTIAL_FILE)")
p.FlagSet.StringVar(&credsFile, "f", os.Getenv("GMAIL_CREDENTIAL_FILE"), "Gmail credential file (or env var GMAIL_CREDENTIAL_FILE)")

p.FlagSet.StringVar(&tokenFile, "token-file", filepath.Join(os.TempDir(), "token.json"), "Gmail oauth token file")
p.FlagSet.StringVar(&tokenFile, "t", filepath.Join(os.TempDir(), "token.json"), "Gmail oauth token file")

// Set the before function.
p.Before = func(ctx context.Context) error {
// Set the log level.
Expand All @@ -60,12 +65,12 @@ func main() {
}

if len(credsFile) < 1 {
return errors.New("Gmail credential file cannot be empty")
return errors.New("the Gmail credential file cannot be empty")
}

// Make sure the file exists.
if _, err := os.Stat(credsFile); os.IsNotExist(err) {
return fmt.Errorf("Credential file %s does not exist", credsFile)
return fmt.Errorf("credential file %s does not exist", credsFile)
}

// Read the credentials file.
Expand All @@ -85,7 +90,7 @@ func main() {
}

// Get the client from the config.
client, err := getClient(ctx, config)
client, err := getClient(ctx, tokenFile, config)
if err != nil {
return fmt.Errorf("creating client failed: %v", err)
}
Expand Down

0 comments on commit 5cd66cc

Please sign in to comment.