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: support both https and ssh cloning #281

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Here are several ways to install microplane:

- *Pre-built release* - You can download a pre-built version of Microplane from the [Github releases](https://github.com/Clever/microplane/releases).
- *Compile it yourself* - Run `go install github.com/Clever/microplane@latest`. In this case the binary will be installed to `$GOPATH/bin/microplane`. Alternately, you can follow the steps under "Development", below.
- *Homebrew* - `brew install microplane`. The latest homebrew formula is [here](https://github.com/Homebrew/homebrew-core/blob/master/Formula/microplane.rb)
- *Homebrew* - `brew install microplane`. The latest homebrew formula is [here](https://github.com/Homebrew/homebrew-core/blob/master/Formula/m/microplane.rb)

## Usage

Expand Down
7 changes: 7 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ See https://docs.gitlab.com/ee/user/search/advanced_search_syntax.html for more
query = args[0]
}

if initCloneType != "ssh" && initCloneType != "https" {
log.Fatalf("clone-type must be 'ssh' or 'https' but was %s", initCloneType)
}

output, err := initialize.Initialize(initialize.Input{
AllRepos: initAllrepos,
Query: query,
Expand All @@ -98,6 +102,7 @@ See https://docs.gitlab.com/ee/user/search/advanced_search_syntax.html for more
ProviderURL: initProviderURL,
ReposFromFile: initFlagReposFile,
RepoSearch: initRepoSearch,
CloneType: initCloneType,
})
if err != nil {
log.Fatal(err)
Expand All @@ -119,11 +124,13 @@ var initRepoSearch bool
var initAllrepos bool
var initProvider string
var initProviderURL string
var initCloneType string

func init() {
initCmd.Flags().StringVarP(&initFlagReposFile, "file", "f", "", "get repos from a file instead of searching")
initCmd.Flags().BoolVar(&initRepoSearch, "repo-search", false, "get repos from a github repo search")
initCmd.Flags().BoolVar(&initAllrepos, "all-repos", false, "get all repos for a given org")
initCmd.Flags().StringVar(&initProvider, "provider", "github", "'github' or 'gitlab'")
initCmd.Flags().StringVar(&initProviderURL, "provider-url", "", "custom URL for enterprise setups")
initCmd.Flags().StringVar(&initCloneType, "clone-type", "ssh", "'ssh' or 'https'")
}
5 changes: 3 additions & 2 deletions docs/mp_init.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ $ mp init -f repos.txt

where repos.txt has lines like:

clever/repo2
clever/repo2
clever/repo2
clever/repo2

## (2) Init via Search

Expand Down Expand Up @@ -73,6 +73,7 @@ mp init [query] [flags]

```
--all-repos get all repos for a given org
--clone-type string 'ssh' or 'https' (default "ssh")
-f, --file string get repos from a file instead of searching
-h, --help help for init
--provider string 'github' or 'gitlab' (default "github")
Expand Down
28 changes: 26 additions & 2 deletions initialize/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Input struct {
ProviderURL string
ReposFromFile string
RepoSearch bool
CloneType string
}

// Output for Initialize
Expand All @@ -44,6 +45,7 @@ func Initialize(input Input) (Output, error) {
p := lib.NewProviderFromConfig(lib.ProviderConfig{
Backend: input.Provider,
BackendURL: input.ProviderURL,
CloneType: input.CloneType,
})

var repos []lib.Repo
Expand Down Expand Up @@ -264,9 +266,17 @@ func githubAllRepoSearch(p *lib.Provider, query string) ([]lib.Repo, error) {
func getFormattedRepos(p *lib.Provider, allRepos map[string]*github.Repository) []lib.Repo {
formattedRepos := []lib.Repo{}
for _, r := range allRepos {
var url string
if p.ProviderConfig.CloneType == "ssh" {
url = r.GetSSHURL()
} else if p.ProviderConfig.CloneType == "https" {
url = r.GetCloneURL()
}

formattedRepos = append(formattedRepos, lib.Repo{
Name: r.GetName(),
Owner: r.Owner.GetLogin(),
CloneURL: url,
ProviderConfig: p.ProviderConfig,
})
}
Expand Down Expand Up @@ -310,10 +320,17 @@ func gitlabSearch(p *lib.Provider, query string) ([]lib.Repo, error) {
fmt.Println(err)
}
if _, ok := repoNames[project.Name]; !ok {
var url string
if p.ProviderConfig.CloneType == "ssh" {
url = project.SSHURLToRepo
} else if p.ProviderConfig.CloneType == "https" {
url = project.HTTPURLToRepo
}

repos = append(repos, lib.Repo{
Name: project.Name,
Owner: project.Namespace.FullPath,
CloneURL: project.SSHURLToRepo,
CloneURL: url,
ProviderConfig: p.ProviderConfig,
})
repoNames[project.Name] = true
Expand All @@ -332,10 +349,17 @@ func gitlabSearch(p *lib.Provider, query string) ([]lib.Repo, error) {
}
for _, project := range projects {
if _, ok := repoNames[project.Name]; !ok {
var url string
if p.ProviderConfig.CloneType == "ssh" {
url = project.SSHURLToRepo
} else if p.ProviderConfig.CloneType == "https" {
url = project.HTTPURLToRepo
}

repos = append(repos, lib.Repo{
Name: project.Name,
Owner: project.Namespace.FullPath,
CloneURL: project.SSHURLToRepo,
CloneURL: url,
ProviderConfig: p.ProviderConfig,
})
repoNames[project.Name] = true
Expand Down
1 change: 1 addition & 0 deletions lib/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type ProviderConfig struct {
Backend string
BackendURL string
CloneType string
}

func (pc ProviderConfig) IsEnterprise() bool {
Expand Down