Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #70 from abakermi/add-repos-clone
Browse files Browse the repository at this point in the history
Add repos clone
  • Loading branch information
profclems authored Aug 8, 2020
2 parents 9c16bd5 + b48ddb6 commit ad2a683
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 1 deletion.
39 changes: 39 additions & 0 deletions cmd/glab/utils/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,42 @@ FEEDBACK
Built with ❤ by Clement Sam <https://clementsam.tech>`)

}

//RepoHelp print help
func RepoHelp(message string) {

fmt.Println(message,`
USAGE
glab repo clone <owner/repo> [<dir>] [<options>] [<flags>]
OPTIONS
format: clone the repository as archive ,use values [zip,tar]
FLAGS
--help Show help for command
--version Show glab version
EXAMPLES
$ glab repo clone profclems/glab mydrirectory
ENVIRONMENT VARIABLES
GITLAB_TOKEN: an authentication token for API requests. Setting this avoids being
prompted to authenticate and overrides any previously stored credentials.
GITLAB_REPO: specify the Gitlab repository in "OWNER/REPO" format for commands that
otherwise operate on a local repository.
GITLAB_URI: specify the url of the gitlab server if self hosted (eg: gitlab.example.com)
LEARN MORE
Use "glab <command> <subcommand> --help" for more information about a command.
Read the manual at https://glab.clementsam.tech
FEEDBACK
Open an issue using “glab issue create -R profclems/glab”
Built with ❤ by Clement Sam <https://clementsam.tech>`)

}
128 changes: 127 additions & 1 deletion commands/repo.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package commands

import (
"bytes"
"fmt"
"glab/cmd/glab/utils"
"io"
"log"
"net/url"
"os"
"os/exec"
"strings"

"github.com/dustin/go-humanize"
"github.com/xanzy/go-gitlab"
)

func getRepoContributors() {
MakeRequest(`{}`, "projects/20131402/issues/1", "GET")
}
Expand All @@ -8,6 +23,117 @@ func newBranch() {

}

//CloneWriter w
type CloneWriter struct {
Total uint64
}

func (wc *CloneWriter) Write(p []byte) (int, error) {
n := len(p)
wc.Total += uint64(n)
wc.progress()
return n, nil
}

func (wc CloneWriter) progress() {
fmt.Printf("\r%s", strings.Repeat(" ", 35))
fmt.Printf("\rCloning... %s complete", humanize.Bytes(wc.Total))
}

func contains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}
func archiveRepo(repository string, format string, name string) {

// tar.gz, tar.bz2, tbz, tbz2, tb2, bz2, tar, and zip
extensions := []string{"tar.gz", "tar.bz2", "tbz", "tbz2", "tb2", "bz2", "tar", "zip"}
if b := contains(extensions, format); !b {

utils.RepoHelp("fatal: --format must be one of " + strings.Join(extensions[:], ","))

return
}

git, _ := InitGitlabClient()
l := &gitlab.ArchiveOptions{}
l.Format = gitlab.String(format)
ext := *l.Format
archiveName := strings.Replace(GetEnv("GITLAB_REPO"), "/", "-", -1)
if len(strings.TrimSpace(name)) != 0{
archiveName = name + "." + ext
}


bt, _, err := git.Repositories.Archive(GetEnv("GITLAB_PROJECT_ID"), l)
if err != nil {
log.Fatalf("Failed to clone repos: %v", err)
}

r := bytes.NewReader(bt)
out, err := os.Create(archiveName + ".tmp")
if err != nil {

log.Fatalf("Failed to create archive repos: %v", err)
}

counter := &CloneWriter{}
if _, err = io.Copy(out, io.TeeReader(r, counter)); err != nil {
out.Close()
log.Fatalf("Failed to write repos: %v", err)
}

fmt.Print("\n")
out.Close()

if err = os.Rename(archiveName+".tmp", archiveName); err != nil {
log.Fatalf("Failed to rename tmp repos: %v", err)
}
fmt.Println("finish ", archiveName)
}

func cloneRepo(cmdOptions map[string]string, cmdArgs map[int]string) {
repo := cmdArgs[1]
dir := cmdArgs[2]

if len(strings.TrimSpace(repo)) == 0 || !strings.Contains(repo, "/") {

utils.RepoHelp("fatal: You must specify a owner/repository to clone.")

return
}
if CommandArgExists(cmdOptions, "format") {
archiveRepo(repo, cmdOptions["format"], dir)

return

}

repos := strings.Split(repo, "/")
u, _ := url.Parse(GetEnv("GITLAB_URI"))
url := GetEnv("GITLAB_URI") + ":" + GetEnv("GITLAB_TOKEN") + "@" + u.Host + "/" + repos[0] + "/" + repos[1]
// git clone https://gitlab.com:<personal_access_token>@gitlab.com/user/repo.git'
cmd := exec.Command("git", "clone", url, dir)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("Failed to clone repos: %v", err)
}
fmt.Printf("%s\n", out)

}

// ExecRepo is ...
func ExecRepo(cmdArgs map[string]string) {
func ExecRepo(cmdArgs map[string]string, arrCmd map[int]string) {
commandList := map[interface{}]func(map[string]string, map[int]string){
"clone": cloneRepo,
}
if _, ok := commandList[arrCmd[0]]; ok {
commandList[arrCmd[0]](cmdArgs, arrCmd)
} else {
fmt.Println(arrCmd[0]+":", "Invalid Command")
}
}
1 change: 1 addition & 0 deletions glab.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func Exec(cmd string, cmdArgs map[string]string, arrCmd map[int]string) {
"mr": commands.ExecMergeRequest,
"label": commands.ExecLabel,
"pipeline": commands.ExecPipeline,
"repo": commands.ExecRepo,
"help": Help,
"config": config,
"version": printVersion,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module glab
go 1.14

require (
github.com/dustin/go-humanize v1.0.0
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/tcnksm/go-gitconfig v0.1.2
github.com/xanzy/go-gitlab v0.34.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
Expand Down

0 comments on commit ad2a683

Please sign in to comment.