Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ Allow using file paths in --token and GHORG_*_TOKEN… #358

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ If you don't know which to choose its likely going to be the x86_64 version for
> Note: if you are running into issues, read the troubleshooting and known issues section below

### GitHub Setup
1. Create [Personal Access Token](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line) with all `repo` scopes. Update `GHORG_GITHUB_TOKEN` in your `ghorg/conf.yaml` or as a cli flag. If your org has Saml SSO in front you will need to give your token those permissions as well, see [this doc](https://docs.github.com/en/github/authenticating-to-github/authenticating-with-saml-single-sign-on/authorizing-a-personal-access-token-for-use-with-saml-single-sign-on).
1. Create [Personal Access Token](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line) with all `repo` scopes. Update `GHORG_GITHUB_TOKEN` in your `ghorg/conf.yaml` or as a cli flag or place it in a file and add the path to `GHORG_GITHUB_TOKEN`. If your org has Saml SSO in front you will need to give your token those permissions as well, see [this doc](https://docs.github.com/en/github/authenticating-to-github/authenticating-with-saml-single-sign-on/authorizing-a-personal-access-token-for-use-with-saml-single-sign-on).
1. For cloning GitHub Enterprise (self hosted github instances) repos you must set `--base-url` e.g. `ghorg clone <github_org> --base-url=https://internal.github.com`
1. See [examples/github.md](https://github.com/gabrie30/ghorg/blob/master/examples/github.md) on how to run

Expand All @@ -183,14 +183,14 @@ If you don't know which to choose its likely going to be the x86_64 version for
### GitLab Setup

1. Create [Personal Access Token](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html) with the `read_api` scope (or `api` for self-managed GitLab older than 12.10). This token can be added to your `ghorg/conf.yaml` or as a cli flag.
1. Update the `GitLab Specific` config in your `ghorg/conf.yaml` or via cli flags
1. Update the `GitLab Specific` config in your `ghorg/conf.yaml` or via cli flags or place it in a file and add the path to `GHORG_GITLAB_TOKEN`
1. Update `GHORG_SCM_TYPE` to `gitlab` in your `ghorg/conf.yaml` or via cli flags
1. See [examples/gitlab.md](https://github.com/gabrie30/ghorg/blob/master/examples/gitlab.md) on how to run

### Gitea Setup

1. Create [Access Token](https://docs.gitea.io/en-us/api-usage/) (Settings -> Applications -> Generate Token)
1. Update `GHORG_GITEA_TOKEN` in your `ghorg/conf.yaml` or use the (--token, -t) flag.
1. Update `GHORG_GITEA_TOKEN` in your `ghorg/conf.yaml` or use the (--token, -t) flag or place it in a file and add the path to `GHORG_GITEA_TOKEN`.
1. Update `GHORG_SCM_TYPE` to `gitea` in your `ghorg/conf.yaml` or via cli flags
1. See [examples/gitea.md](https://github.com/gabrie30/ghorg/blob/master/examples/gitea.md) on how to run

Expand All @@ -216,6 +216,8 @@ See [examples](https://github.com/gabrie30/ghorg/tree/master/examples) dir for m

```bash
$ ghorg clone kubernetes --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2
# Example how to use --token with a file path
$ ghorg clone kubernetes --token=~/.config/ghorg/gitlab-token.txt
$ ghorg clone davecheney --clone-type=user --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2
$ ghorg clone gitlab-examples --scm=gitlab --preserve-dir --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2
$ ghorg clone gitlab-examples/wayne-enterprises --scm=gitlab --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2
Expand Down
10 changes: 7 additions & 3 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,22 @@ func cloneFunc(cmd *cobra.Command, argz []string) {
configs.GetOrSetToken()

if cmd.Flags().Changed("token") {
token := cmd.Flag("token").Value.String()
if configs.IsFilePath(token) {
token = configs.GetTokenFromFile(token)
}
if os.Getenv("GHORG_SCM_TYPE") == "github" {
os.Setenv("GHORG_GITHUB_TOKEN", cmd.Flag("token").Value.String())
os.Setenv("GHORG_GITHUB_TOKEN", token)
} else if os.Getenv("GHORG_SCM_TYPE") == "gitlab" {
os.Setenv("GHORG_GITLAB_TOKEN", cmd.Flag("token").Value.String())
os.Setenv("GHORG_GITLAB_TOKEN", token)
} else if os.Getenv("GHORG_SCM_TYPE") == "bitbucket" {
if cmd.Flags().Changed("bitbucket-username") {
os.Setenv("GHORG_BITBUCKET_APP_PASSWORD", cmd.Flag("token").Value.String())
} else {
os.Setenv("GHORG_BITBUCKET_OAUTH_TOKEN", cmd.Flag("token").Value.String())
}
} else if os.Getenv("GHORG_SCM_TYPE") == "gitea" {
os.Setenv("GHORG_GITEA_TOKEN", cmd.Flag("token").Value.String())
os.Setenv("GHORG_GITEA_TOKEN", token)
}
}
err := configs.VerifyTokenSet()
Expand Down
41 changes: 39 additions & 2 deletions configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,31 @@ func GhorgQuiet() bool {
return os.Getenv("GHORG_QUIET") != ""
}

func IsFilePath(path string) bool {
pathValue, err := homedir.Expand(path)
if err != nil {
log.Fatal("Error while expanding tilde to user home directory")
}
info, err := os.Stat(pathValue)
if err != nil {
return false
}
// Check if it's a regular file (not a directory or a symbolic link)
if !info.IsDir() && (info.Mode()&os.ModeType == 0) {
return true
}
return false
}

func GetTokenFromFile(path string) string {
expandedPath, _ := homedir.Expand(path)
fileContents, err := os.ReadFile(expandedPath)
if err != nil {
log.Fatal("Error while reading file")
}
return strings.TrimSpace(string(fileContents))
}

// GetOrSetToken will set token based on scm
func GetOrSetToken() {
switch os.Getenv("GHORG_SCM_TYPE") {
Expand All @@ -174,8 +199,12 @@ func GetOrSetToken() {
}

func getOrSetGitHubToken() {
var token string
if isZero(os.Getenv("GHORG_GITHUB_TOKEN")) {
var token = os.Getenv("GHORG_GITHUB_TOKEN")
if IsFilePath(token) {
os.Setenv("GHORG_GITHUB_TOKEN", GetTokenFromFile(token))
}

if isZero(token) {
if runtime.GOOS == "windows" {
return
}
Expand All @@ -191,6 +220,10 @@ func getOrSetGitHubToken() {
func getOrSetGitLabToken() {
token := os.Getenv("GHORG_GITLAB_TOKEN")

if IsFilePath(token) {
os.Setenv("GHORG_GITLAB_TOKEN", GetTokenFromFile(token))
}

if isZero(token) {
if runtime.GOOS == "windows" {
return
Expand Down Expand Up @@ -226,6 +259,10 @@ func getOrSetBitBucketToken() {
func getOrSetGiteaToken() {
token := os.Getenv("GHORG_GITEA_TOKEN")

if IsFilePath(token) {
os.Setenv("GHORG_GITEA_TOKEN", GetTokenFromFile(token))
}

if isZero(token) {
if runtime.GOOS == "windows" {
return
Expand Down
6 changes: 3 additions & 3 deletions sample-conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ GHORG_NO_TOKEN: false
# +-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+

# Add your GitHub token
# flag (--token, -t) eg: --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2
# flag (--token, -t) eg: --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2 or --token=~/path/to/file/containing/token
GHORG_GITHUB_TOKEN:

# Path to your GitHub App PEM file, for authenticating with GitHub App
Expand All @@ -178,7 +178,7 @@ GHORG_GITHUB_APP_ID:
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

# Add your GitLab token
# flag (--token, -t) eg: --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2
# flag (--token, -t) eg: --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2 or --token=~/path/to/file/containing/token
GHORG_GITLAB_TOKEN:

# clones repos in a directory structure that matches gitlab namespaces eg company/unit/subunit/app would clone into ghorg/org/unit/subunit/app
Expand All @@ -199,7 +199,7 @@ GHORG_GITLAB_GROUP_EXCLUDE_MATCH_REGEX:

# Add your Gitea token
# Settings -> Applications -> Generate Token
# flag (--token, -t) eg: --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2
# flag (--token, -t) eg: --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2 or --token=~/path/to/file/containing/token
GHORG_GITEA_TOKEN:

# Must be present if your gitea instance uses http
Expand Down
Loading