cmd: use structured path (#838)

pull/842/head
Cluas 4 years ago committed by GitHub
parent 089cf9d0cb
commit e93da2468c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      cmd/kratos/internal/base/repo.go

@ -18,9 +18,11 @@ type Repo struct {
// NewRepo new a repository manager. // NewRepo new a repository manager.
func NewRepo(url string) *Repo { func NewRepo(url string) *Repo {
start := strings.Index(url, "//")
end := strings.LastIndex(url, "/")
return &Repo{ return &Repo{
url: url, url: url,
home: kratosHomeWithDir("repo"), home: kratosHomeWithDir("repo/" + url[start+2:end]),
} }
} }
@ -31,8 +33,8 @@ func (r *Repo) Path() string {
return path.Join(r.home, r.url[start+1:end]) return path.Join(r.home, r.url[start+1:end])
} }
// Pull fetchs the repository from remote url. // Pull fetch the repository from remote url.
func (r *Repo) Pull(ctx context.Context, url string) error { func (r *Repo) Pull(ctx context.Context) error {
repo, err := git.PlainOpen(r.Path()) repo, err := git.PlainOpen(r.Path())
if err != nil { if err != nil {
return err return err
@ -53,7 +55,7 @@ func (r *Repo) Pull(ctx context.Context, url string) error {
// Clone clones the repository to cache path. // Clone clones the repository to cache path.
func (r *Repo) Clone(ctx context.Context) error { func (r *Repo) Clone(ctx context.Context) error {
if _, err := os.Stat(r.Path()); !os.IsNotExist(err) { if _, err := os.Stat(r.Path()); !os.IsNotExist(err) {
return r.Pull(ctx, r.url) return r.Pull(ctx)
} }
_, err := git.PlainCloneContext(ctx, r.Path(), false, &git.CloneOptions{ _, err := git.PlainCloneContext(ctx, r.Path(), false, &git.CloneOptions{
URL: r.url, URL: r.url,

Loading…
Cancel
Save