Skip to content

Commit

Permalink
Adds the option to define sync access tokens via environment variables (
Browse files Browse the repository at this point in the history
  • Loading branch information
riscie authored and knqyf263 committed Jan 13, 2019
1 parent 31df594 commit 92102cb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ $ pet search
### Gist
You must obtain access token.
Go https://github.com/settings/tokens/new and create access token (only need "gist" scope).
Set that to `access_token` in `[Gist]`.
Set that to `access_token` in `[Gist]` or use an environment variable with the name `$PET_GITHUB_ACCESS_TOKEN`.

After setting, you can upload snippets to Gist.
If `gist_id` is not set, new gist will be created.
Expand Down Expand Up @@ -320,7 +320,7 @@ Upload success
### GitLab Snippets
You must obtain access token.
Go https://gitlab.com/profile/personal_access_tokens and create access token.
Set that to `access_token` in `[GitLab]`.
Set that to `access_token` in `[GitLab]` or use an environment variable with the name `$PET_GITLAB_ACCESS_TOKEN`..

After setting, you can upload snippets to GitLab Snippets.
If `id` is not set, new snippet will be created.
Expand Down
26 changes: 20 additions & 6 deletions sync/gist.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sync
import (
"context"
"fmt"
"os"
"time"

"github.com/briandowns/spinner"
Expand All @@ -11,6 +12,9 @@ import (
"github.com/pkg/errors"
"golang.org/x/oauth2"
)
const (
githubTokenEnvVariable = "PET_GITHUB_ACCESS_TOKEN"
)

// GistClient manages communication with Gist
type GistClient struct {
Expand All @@ -20,20 +24,30 @@ type GistClient struct {

// NewGistClient returns GistClient
func NewGistClient() (Client, error) {
if config.Conf.Gist.AccessToken == "" {
accessToken, err := getGithubAccessToken()
if err != nil {
return nil, fmt.Errorf(`access_token is empty.
Go https://github.com/settings/tokens/new and create access_token (only need "gist" scope).
Write access_token in config file (pet configure).
`)
Write access_token in config file (pet configure) or export $%v.
`, githubTokenEnvVariable)
}

client := GistClient{
Client: githubClient(),
Client: githubClient(accessToken),
ID: config.Conf.Gist.GistID,
}
return client, nil
}

func getGithubAccessToken() (string, error) {
if config.Conf.Gist.AccessToken != "" {
return config.Conf.Gist.AccessToken, nil
} else if os.Getenv(githubTokenEnvVariable) != "" {
return os.Getenv(githubTokenEnvVariable), nil
}
return "", errors.New("Github AccessToken not found in any source")
}

// GetSnippet returns the remote snippet
func (g GistClient) GetSnippet() (*Snippet, error) {
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
Expand Down Expand Up @@ -121,9 +135,9 @@ func (g GistClient) updateGist(ctx context.Context, gist *github.Gist) (err erro
return nil
}

func githubClient() *github.Client {
func githubClient(accessToken string) *github.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Conf.Gist.AccessToken},
&oauth2.Token{AccessToken: accessToken},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)
Expand Down
23 changes: 19 additions & 4 deletions sync/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sync
import (
"context"
"fmt"
"os"
"strconv"
"time"

Expand All @@ -12,6 +13,10 @@ import (
"github.com/xanzy/go-gitlab"
)

const (
gitlabTokenEnvVariable = "PET_GITLAB_ACCESS_TOKEN"
)

// GitLabClient manages communication with GitLab Snippets
type GitLabClient struct {
Client *gitlab.Client
Expand All @@ -20,15 +25,16 @@ type GitLabClient struct {

// NewGitLabClient returns GitLabClient
func NewGitLabClient() (Client, error) {
if config.Conf.GitLab.AccessToken == "" {
accessToken, err := getGitlabAccessToken()
if err != nil {
return nil, fmt.Errorf(`access_token is empty.
Go https://gitlab.com/profile/personal_access_tokens and create access_token.
Write access_token in config file (pet configure).
`)
Write access_token in config file (pet configure) or export $%v.
`, gitlabTokenEnvVariable)
}

client := GitLabClient{
Client: gitlab.NewClient(nil, config.Conf.GitLab.AccessToken),
Client: gitlab.NewClient(nil, accessToken),
ID: 0,
}

Expand All @@ -48,6 +54,15 @@ Write access_token in config file (pet configure).
return client, nil
}

func getGitlabAccessToken() (string, error) {
if config.Conf.GitLab.AccessToken != "" {
return config.Conf.GitLab.AccessToken, nil
} else if os.Getenv(gitlabTokenEnvVariable) != "" {
return os.Getenv(gitlabTokenEnvVariable), nil
}
return "", errors.New("GitLab AccessToken not found in any source")
}

// GetSnippet returns the remote snippet
func (g GitLabClient) GetSnippet() (*Snippet, error) {
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
Expand Down

0 comments on commit 92102cb

Please sign in to comment.