Skip to content

Commit

Permalink
feat: add git output to the clone command
Browse files Browse the repository at this point in the history
  • Loading branch information
juev committed Dec 28, 2024
1 parent ebd8517 commit e34b7d9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/juev/gclone

go 1.22.1

require github.com/spf13/pflag v1.0.5
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
30 changes: 20 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,48 @@ import (
"path/filepath"
"regexp"
"strings"

"github.com/spf13/pflag"
)

var (
version = "0.3.2"
version = "0.3.3"
commit = "none"
date = "unknown"
)

var r = regexp.MustCompile(`^(?:.*://)?(?:[^@]+@)?([^:/]+)(?::\d+)?(?:/|:)?(.*)$`)

func main() {
if len(os.Args) < 2 {
var showCommandHelp, showVersionInfo, quiet bool
pflag.BoolVarP(&showCommandHelp, "help", "h", false, "Show this help message and exit")
pflag.BoolVarP(&showVersionInfo, "version", "v", false, "Show the version number and exit")
pflag.BoolVarP(&quiet, "quiet", "q", false, "Suppress output")
pflag.Parse()

if showCommandHelp {
usage()
return
}

var repository string
switch os.Args[1] {
case "-h", "--help", "help":
usage()
return
case "-v", "--version", "version":
if showVersionInfo {
if commit != "none" {
fmt.Printf("gclone version %s, commit %s, built at %s\n", version, commit, date)
} else {
fmt.Printf("gclone version %s\n", version)
}
return
default:
repository = os.Args[1]
}

if _, err := exec.LookPath("git"); err != nil {
log.Fatalf("git not found")
}

var repository string
if args := pflag.Args(); len(args) > 0 {
repository = args[0]
}

projectDir := getProjectDir(repository)

if ok := isDirectoryNotEmpty(projectDir); ok {
Expand All @@ -56,6 +62,10 @@ func main() {
}

cmd := exec.Command("git", "clone", repository)
if !quiet {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
cmd.Dir = filepath.Dir(projectDir)
if err := cmd.Run(); err != nil {
log.Fatalf("failed clone repository: %s", err)
Expand Down

0 comments on commit e34b7d9

Please sign in to comment.