Skip to content

Commit

Permalink
chore: Code style
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Mar 12, 2024
1 parent dcde73a commit 3a752c7
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 119 deletions.
8 changes: 4 additions & 4 deletions internal/pkg/command/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func getCloneCmd() *cobra.Command {
},
}

cloneCmd.Flags().StringVarP(&cloneProtocol, "clone-protocol", "p", "ssh", "Clone protocol (ssh, https)")
cloneCmd.Flags().StringVarP(&cloneProtocol, "clone-protocol", "p", "ssh", "Clone protocol (ssh, https)")

return cloneCmd
}
Expand Down Expand Up @@ -68,9 +68,9 @@ func cloneRepo(repo string) (string, error) {
}

func makeURL(u *url.URL, renameRepo map[string]string) (string, error) {
if u.Scheme == "http" || u.Scheme == "https" {
return u.String(), nil
}
if u.Scheme == "http" || u.Scheme == "https" {
return u.String(), nil
}

for host, match := range renameRepo {
r := regexp.MustCompile(regexp.QuoteMeta(match))
Expand Down
22 changes: 11 additions & 11 deletions internal/pkg/command/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Test_makeURL(t *testing.T) {
{
name: "notinma",
renameMap: map[string]string{
"bb-personal": "bitbucket.org/bla/bla",
"bb-personal": "bitbucket.org/bla/bla",
},
URL: "ssh://[email protected]/bla/bla",
want: "ssh://[email protected]/bla/bla",
Expand All @@ -32,21 +32,21 @@ func Test_makeURL(t *testing.T) {
{
name: "replace",
renameMap: map[string]string{
"gh-personal": "github.com/bla",
"gh-personal": "github.com/bla",
},
URL: "ssh://[email protected]/bla/bla",
want: "ssh://git@gh-personal/bla/bla",
wantErr: false,
},
{
name: "no-replace-in-http",
renameMap: map[string]string{
"github.com/bla": "gh-personal",
},
URL: "http://github.com/bla/bla",
want: "http://github.com/bla/bla",
wantErr: false,
},
{
name: "no-replace-in-http",
renameMap: map[string]string{
"github.com/bla": "gh-personal",
},
URL: "http://github.com/bla/bla",
want: "http://github.com/bla/bla",
wantErr: false,
},
}

for _, tt := range tests {
Expand Down
17 changes: 8 additions & 9 deletions internal/pkg/command/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import (

func getListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list_fzf",
Short: "List all repositories",
Hidden: true,
Use: "list_fzf",
Short: "List all repositories",
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
baseDir := config.GetBaseDir()
repos := repo.GetRepoPathsChan(baseDir, true)
for repo := range repos {
fmt.Println(repo)
}
baseDir := config.GetBaseDir()
repos := repo.GetRepoPathsChan(baseDir, true)
for repo := range repos {
fmt.Println(repo)
}
},
}
return cmd
}

16 changes: 8 additions & 8 deletions internal/pkg/command/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ The credentials are read from the environment variables:
&org.BitbucketProvider{},
}

provider, err := org.GetProviderFromURL(providers, args[0], typeHint)
provider, err := org.GetProviderFromURL(providers, args[0], typeHint)
if err != nil {
logrus.Fatal(err)
}
Expand All @@ -71,12 +71,12 @@ The credentials are read from the environment variables:
logrus.Infof("Found %d repos", len(repos))

for _, repo := range repos {
var cloneURL string
if cloneProtocol == "https" {
cloneURL = repo.CloneURL
} else {
cloneURL = repo.SSHURL
}
var cloneURL string
if cloneProtocol == "https" {
cloneURL = repo.CloneURL
} else {
cloneURL = repo.SSHURL
}

if _, err := cloneRepo(cloneURL); err != nil {
if err == ErrDirectoryAlreadyExists || errors.Unwrap(err) == ErrDirectoryAlreadyExists {
Expand All @@ -93,7 +93,7 @@ The credentials are read from the environment variables:

cmd.Flags().BoolVarP(&cloneForks, "forks", "f", false, "Clone forks")
cmd.Flags().StringVarP(&typeHint, "type-hint", "t", "", "Add a type hint to the URL to force a specific provider")
cmd.Flags().StringVarP(&cloneProtocol, "clone-protocol", "p", "ssh", "Clone protocol (ssh, https)")
cmd.Flags().StringVarP(&cloneProtocol, "clone-protocol", "p", "ssh", "Clone protocol (ssh, https)")

return cmd
}
2 changes: 1 addition & 1 deletion internal/pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Execute() {
rootCmd.AddCommand(getOrgCmd())
rootCmd.AddCommand(getWDIDCmd())
rootCmd.AddCommand(getTmuxCmd())
rootCmd.AddCommand(getListCmd())
rootCmd.AddCommand(getListCmd())

cobra.OnInitialize(config.InitConfig)
cobra.OnInitialize(initLogging)
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/command/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func getSetupCmd() *cobra.Command {
logrus.Fatal(err)
}

logrus.Info("Setup complete")
logrus.Info("If you want to make more advanced changes, edit the config file at ~/.config/project-helper/config.yaml")
logrus.Info("Setup complete")
logrus.Info("If you want to make more advanced changes, edit the config file at ~/.config/project-helper/config.yaml")
},
}

Expand Down
1 change: 0 additions & 1 deletion internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,3 @@ func GetBaseDir() string {

return repo.ExpandPath(baseDir)
}

39 changes: 19 additions & 20 deletions internal/pkg/org/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ import (
)

type GithubProvider struct {
Org string
Org string
}

func (GithubProvider) FromURL(url string, typeHint string) (OrgProvider, error) {
parts := strings.Split(url, "/")
parts := strings.Split(url, "/")

if parts[0] == "github.com" {
if len(parts) < 2 {
return nil, fmt.Errorf("Invalid repo")
}
return &GithubProvider{
Org: parts[1],
}, nil
}
if parts[0] == "github.com" {
if len(parts) < 2 {
return nil, fmt.Errorf("Invalid repo")
}
return &GithubProvider{
Org: parts[1],
}, nil
}

return nil, nil
return nil, nil
}

func (g *GithubProvider) GetRepos() ([]*Repo, bool, error) {
Expand All @@ -44,19 +44,19 @@ func (g *GithubProvider) GetRepos() ([]*Repo, bool, error) {
}
}

result := []*Repo{}
result := []*Repo{}
for _, repo := range repos {
if strings.EqualFold(repo.Owner.GetLogin(), g.Org) {
result = append(result, &Repo{
Name: repo.GetName(),
URL: repo.GetHTMLURL(),
CloneURL: repo.GetCloneURL(),
SSHURL: repo.GetSSHURL(),
})
result = append(result, &Repo{
Name: repo.GetName(),
URL: repo.GetHTMLURL(),
CloneURL: repo.GetCloneURL(),
SSHURL: repo.GetSSHURL(),
})
}
}

return result, true, nil
return result, true, nil
}

func getRepoFunc(org string) func() ([]*github.Repository, bool, error) {
Expand Down Expand Up @@ -90,4 +90,3 @@ func getRepoFunc(org string) func() ([]*github.Repository, bool, error) {
return repos, resp.NextPage == 0, err
}
}

91 changes: 45 additions & 46 deletions internal/pkg/org/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,62 @@ import (
)

type GitlabProvider struct {
User string
Host string
User string
Host string
}

func (GitlabProvider) FromURL(url string, typeHint string) (OrgProvider, error) {
parts := strings.Split(url, "/")
parts := strings.Split(url, "/")

if parts[0] == "gitlab.com" || typeHint == "gitlab" {
if len(parts) < 2 {
return nil, fmt.Errorf("Invalid repo")
}
return &GitlabProvider{
User: strings.Join(parts[1:], "/"),
Host: fmt.Sprintf("https://%s", parts[0]),
}, nil
}
if parts[0] == "gitlab.com" || typeHint == "gitlab" {
if len(parts) < 2 {
return nil, fmt.Errorf("Invalid repo")
}
return &GitlabProvider{
User: strings.Join(parts[1:], "/"),
Host: fmt.Sprintf("https://%s", parts[0]),
}, nil
}

return nil, nil
return nil, nil
}

func (g *GitlabProvider) GetRepos() ([]*Repo, bool, error) {
gl, err := gitlab.NewClient(
os.Getenv("GITLAB_TOKEN"),
gitlab.WithBaseURL(g.Host),
)
if err != nil {
return nil, false, fmt.Errorf("failed to create gitlab client: %w", err)
}
gl, err := gitlab.NewClient(
os.Getenv("GITLAB_TOKEN"),
gitlab.WithBaseURL(g.Host),
)
if err != nil {
return nil, false, fmt.Errorf("failed to create gitlab client: %w", err)
}

opts := &gitlab.ListGroupProjectsOptions{
IncludeSubGroups: gitlab.Ptr(true),
ListOptions: gitlab.ListOptions{
PerPage: 100,
},
}
opts := &gitlab.ListGroupProjectsOptions{
IncludeSubGroups: gitlab.Ptr(true),
ListOptions: gitlab.ListOptions{
PerPage: 100,
},
}

var repos []*Repo
for {
projects, resp, err := gl.Groups.ListGroupProjects(g.User, opts)
if err != nil {
return nil, false, fmt.Errorf("failed to list projects: %w", err)
}
var repos []*Repo
for {
projects, resp, err := gl.Groups.ListGroupProjects(g.User, opts)
if err != nil {
return nil, false, fmt.Errorf("failed to list projects: %w", err)
}

for _, project := range projects {
repos = append(repos, &Repo{
Name: project.Name,
URL: project.WebURL,
CloneURL: project.HTTPURLToRepo,
SSHURL: project.SSHURLToRepo,
})
for _, project := range projects {
repos = append(repos, &Repo{
Name: project.Name,
URL: project.WebURL,
CloneURL: project.HTTPURLToRepo,
SSHURL: project.SSHURLToRepo,
})

if resp.CurrentPage >= resp.TotalPages {
return repos, true, nil
}
if resp.CurrentPage >= resp.TotalPages {
return repos, true, nil
}

opts.Page = resp.NextPage
}
}
opts.Page = resp.NextPage
}
}
}

33 changes: 16 additions & 17 deletions internal/pkg/org/org.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package org

type OrgProvider interface {
GetRepos() ([]*Repo, bool, error)
FromURL(url string, typeHint string) (OrgProvider, error)
GetRepos() ([]*Repo, bool, error)
FromURL(url string, typeHint string) (OrgProvider, error)
}

type Repo struct {
Name string
URL string
CloneURL string
SSHURL string
Name string
URL string
CloneURL string
SSHURL string
}

func GetProviderFromURL(providers []OrgProvider, url string, typeHint string) (OrgProvider, error) {
for _, provider := range providers {
p, err := provider.FromURL(url, typeHint)
if err != nil {
return nil, err
}
if p != nil {
return p, nil
}
}
for _, provider := range providers {
p, err := provider.FromURL(url, typeHint)
if err != nil {
return nil, err
}
if p != nil {
return p, nil
}
}

return nil, nil
return nil, nil
}

0 comments on commit 3a752c7

Please sign in to comment.