Skip to content
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
30 changes: 25 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"

sshgit "github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/gomicro/align/client/remotes"
"github.com/gomicro/align/client/repos"
"github.com/gomicro/align/config"
"github.com/gomicro/scribe"
"github.com/gomicro/scribe/color"
Expand All @@ -20,12 +22,12 @@ import (
)

type Client struct {
*repos.Repos
remoteMgr *remotes.Remotes
cfg *config.Config
ghClient *github.Client
rate *rate.Limiter
ghSSHAuth *sshgit.PublicKeys
ghHTTPSAuth *sshgit.Password
scrb scribe.Scriber
}

func New(cfg *config.Config) (*Client, error) {
Expand Down Expand Up @@ -104,13 +106,15 @@ func New(cfg *config.Config) (*Client, error) {
return nil, fmt.Errorf("scribe: %w", err)
}

ghClient := github.NewClient(oauth2.NewClient(ctx, ts))

return &Client{
Repos: repos.New(scrb, ghClient, rl),
remoteMgr: remotes.New(scrb),
cfg: cfg,
ghClient: github.NewClient(oauth2.NewClient(ctx, ts)),
rate: rl,
ghClient: ghClient,
ghSSHAuth: publicKeys,
ghHTTPSAuth: pass,
scrb: scrb,
}, nil
}

Expand Down Expand Up @@ -149,3 +153,19 @@ func (c *Client) GetLogins(ctx context.Context) ([]string, error) {

return logins, nil
}

func (c *Client) Remotes(ctx context.Context, dirs []string, args ...string) error {
return c.remoteMgr.Remotes(ctx, dirs, args...)
}

func (c *Client) Add(ctx context.Context, dirs []string, name, baseURL string) error {
return c.remoteMgr.Add(ctx, dirs, name, baseURL)
}

func (c *Client) Remove(ctx context.Context, dirs []string, name string) error {
return c.remoteMgr.Remove(ctx, dirs, name)
}

func (c *Client) SetURLs(ctx context.Context, dirs []string, name, baseURL string) error {
return c.remoteMgr.SetURLs(ctx, dirs, name, baseURL)
}
6 changes: 4 additions & 2 deletions client/clienter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package client
import (
"context"

clientctx "github.com/gomicro/align/client/context"
"github.com/gomicro/align/client/repos"
"github.com/google/go-github/github"
)

type Clienter interface {
Add(ctx context.Context, dirs []string, name, baseURL string) error
Branches(ctx context.Context, repoDirs []string, args ...string) error
CheckoutRepos(ctx context.Context, repoDirs []string, args ...string) error
CloneRepos(ctx context.Context, dir string) ([]*Repository, error)
CloneRepos(ctx context.Context, dir string) ([]*clientctx.Repository, error)
CommitRepos(ctx context.Context, dirs []string, args ...string) error
DiffRepos(ctx context.Context, repoDirs []string, cfg *DiffConfig) error
DiffRepos(ctx context.Context, repoDirs []string, cfg *repos.DiffConfig) error
GetBranchAndTagNames(ctx context.Context, dirs []string) ([]string, error)
GetBranchNames(ctx context.Context, dirs []string) ([]string, error)
GetDirs(ctx context.Context, dir string) ([]string, error)
Expand Down
57 changes: 26 additions & 31 deletions client/context.go → client/context/context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package context

import (
"context"
Expand All @@ -10,18 +10,36 @@ import (
)

type reposContext int
type verboseContextKey struct{}

var (
reposContextKey reposContext = 0
excludesContextKey reposContext = 1
verboseContextKey reposContext = 2
verboseKey = verboseContextKey{}

ErrReposNotFoundInContext = errors.New("repos map not found in context")
)

func WithVerbose(ctx context.Context, verbose bool) context.Context {
return context.WithValue(ctx, verboseKey, verbose)
}

func Verbose(ctx context.Context) bool {
v := ctx.Value(verboseKey)
verbose, ok := v.(bool)
if !ok {
return false
}
return verbose
}

type Repository struct {
Name string
URL string
}

func WithRepos(ctx context.Context, repos []*github.Repository) context.Context {
repoMap := parseDirRepoMap(repos)

return context.WithValue(ctx, reposContextKey, repoMap)
}

Expand All @@ -31,7 +49,6 @@ func RepoMap(ctx context.Context) (map[string][]*Repository, error) {
if !ok {
return nil, ErrReposNotFoundInContext
}

return repoMap, nil
}

Expand All @@ -45,35 +62,28 @@ func Excludes(ctx context.Context) ([]*Repository, error) {
if !ok {
return nil, nil
}

return excludes, nil
}

type Repository struct {
name string
url string
}

func parseDirRepoMap(repos []*github.Repository) map[string][]*Repository {
var dirRepo = map[string][]*Repository{}
dirRepo := map[string][]*Repository{}
for _, repo := range repos {
parts := strings.Split(*repo.SSHURL, "/")

dir := strings.Split(parts[0], ":")[1]
name := strings.TrimSuffix(parts[1], ".git")

r := &Repository{
name: name,
url: *repo.SSHURL,
Name: name,
URL: *repo.SSHURL,
}

dirRepo[dir] = append(dirRepo[dir], r)
}

return dirRepo
}

func removeExcludes(ctx context.Context, repoMap map[string][]*Repository) (map[string][]*Repository, error) {
func RemoveExcludes(ctx context.Context, repoMap map[string][]*Repository) (map[string][]*Repository, error) {
newMap := map[string][]*Repository{}

excludes, err := Excludes(ctx)
Expand All @@ -85,12 +95,11 @@ func removeExcludes(ctx context.Context, repoMap map[string][]*Repository) (map[
for i := range rs {
keep := true
for j := range excludes {
if rs[i].url == excludes[j].url {
if rs[i].URL == excludes[j].URL {
keep = false
break
}
}

if keep {
newMap[dir] = append(newMap[dir], rs[i])
}
Expand All @@ -99,17 +108,3 @@ func removeExcludes(ctx context.Context, repoMap map[string][]*Repository) (map[

return newMap, nil
}

func WithVerbose(ctx context.Context, verbose bool) context.Context {
return context.WithValue(ctx, verboseContextKey, verbose)
}

func Verbose(ctx context.Context) bool {
v := ctx.Value(verboseContextKey)
verbose, ok := v.(bool)
if !ok {
return false
}

return verbose
}
File renamed without changes.
35 changes: 0 additions & 35 deletions client/remotes.go

This file was deleted.

27 changes: 14 additions & 13 deletions client/remotes_add.go → client/remotes/add.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package remotes

import (
"bytes"
Expand All @@ -7,33 +7,34 @@ import (
"os/exec"
"strings"

ctxhelper "github.com/gomicro/align/client/context"
"github.com/gosuri/uiprogress"
)

func (c *Client) Add(ctx context.Context, dirs []string, name, baseURL string) error {
func (r *Remotes) Add(ctx context.Context, dirs []string, name, baseURL string) error {
count := len(dirs)

args := append([]string{"remote", "add"}, name)

verbose := Verbose(ctx)
verbose := ctxhelper.Verbose(ctx)

var bar *uiprogress.Bar
currRepo := ""

if verbose {
c.scrb.BeginDescribe("Command")
defer c.scrb.EndDescribe()
r.scrb.BeginDescribe("Command")
defer r.scrb.EndDescribe()

var vargs []string
vargs = append(vargs, args...)

url := buildURL(baseURL, "<dir>")
vargs = append(vargs, url)

c.scrb.Print(fmt.Sprintf("git %s", strings.Join(vargs, " ")))
r.scrb.Print(fmt.Sprintf("git %s", strings.Join(vargs, " ")))

c.scrb.BeginDescribe("directories")
defer c.scrb.EndDescribe()
r.scrb.BeginDescribe("directories")
defer r.scrb.EndDescribe()
} else {
bar = uiprogress.AddBar(count).
AppendCompleted().
Expand Down Expand Up @@ -69,15 +70,15 @@ func (c *Client) Add(ctx context.Context, dirs []string, name, baseURL string) e
}

if verbose {
c.scrb.BeginDescribe(dir)
r.scrb.BeginDescribe(dir)
if err != nil {
c.scrb.Error(err)
c.scrb.PrintLines(errout)
r.scrb.Error(err)
r.scrb.PrintLines(errout)
} else {
c.scrb.PrintLines(out)
r.scrb.PrintLines(out)
}

c.scrb.EndDescribe()
r.scrb.EndDescribe()
} else {
bar.Incr()
}
Expand Down
45 changes: 45 additions & 0 deletions client/remotes/remotes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package remotes

import (
"bytes"
"context"
"os/exec"

"github.com/gomicro/scribe"
)

type Remotes struct {
scrb scribe.Scriber
}

func New(scrb scribe.Scriber) *Remotes {
return &Remotes{scrb: scrb}
}

func (r *Remotes) Remotes(ctx context.Context, dirs []string, args ...string) error {
args = append([]string{"remote"}, args...)

for _, dir := range dirs {
out := &bytes.Buffer{}
errout := &bytes.Buffer{}

cmd := exec.CommandContext(ctx, "git", args...)
cmd.Stdout = out
cmd.Stderr = errout
cmd.Dir = dir

r.scrb.BeginDescribe(dir)

err := cmd.Run()
if err != nil {
r.scrb.Error(err)
r.scrb.PrintLines(errout)
} else {
r.scrb.PrintLines(out)
}

r.scrb.EndDescribe()
}

return nil
}
Loading
Loading