init project and add repo detect
This commit is contained in:
commit
878bf86b46
|
@ -0,0 +1 @@
|
|||
.idea
|
|
@ -0,0 +1,8 @@
|
|||
module up2GitX
|
||||
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/gookit/color v1.2.5
|
||||
github.com/gookit/gcli/v2 v2.2.1
|
||||
)
|
|
@ -0,0 +1,32 @@
|
|||
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/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/gookit/color v1.1.7/go.mod h1:R3ogXq2B9rTbXoSHJ1HyUVAZ3poOJHpd9nQmyGZsfvQ=
|
||||
github.com/gookit/color v1.2.5 h1:s1gzb/fg3HhkSLKyWVUsZcVBUo+R1TwEYTmmxH8gGFg=
|
||||
github.com/gookit/color v1.2.5/go.mod h1:AhIE+pS6D4Ql0SQWbBeXPHw7gY0/sjHoA4s/n1KB7xg=
|
||||
github.com/gookit/gcli v1.2.1 h1:a/DApyzDT/468l/v8oTGwd1TYDdD6Q+a1aWQ+IZoD24=
|
||||
github.com/gookit/gcli/v2 v2.2.1 h1:7uzRBODHIOl0vbmG3sPmQ2dn4f+0mxFPaoj2Mb2IFts=
|
||||
github.com/gookit/gcli/v2 v2.2.1/go.mod h1:E0Xwlbtx42V2bpx/Z9v0t01yTbOxwTEcwVZAesbkC/Q=
|
||||
github.com/gookit/goutil v0.2.7 h1:9cI9yDIBKT+RJHq6o3kRa3snQfozUkJoOwVH1aHGJhA=
|
||||
github.com/gookit/goutil v0.2.7/go.mod h1:8emMcACka2rFot/L9ZO7r3zjWiitzIhB/CfWXUCW75w=
|
||||
github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc=
|
||||
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e h1:D5TXcfTk7xF7hvieo4QErS3qqCB4teTffacDWr7CI+0=
|
||||
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
@ -0,0 +1,54 @@
|
|||
package platform
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"up2GitX/share"
|
||||
|
||||
"github.com/gookit/color"
|
||||
"github.com/gookit/gcli/v2"
|
||||
"github.com/gookit/gcli/v2/interact"
|
||||
)
|
||||
|
||||
// options for the command
|
||||
var nesOpts = struct {
|
||||
forceSync bool
|
||||
}{}
|
||||
|
||||
func GiteeCommand() *gcli.Command {
|
||||
gitee := &gcli.Command{
|
||||
Func: syncGitee,
|
||||
Name: "gitee",
|
||||
UseFor: "This command is used for sync local repo to Gitee",
|
||||
Examples: `Simple usage: <cyan>{$binName} {$cmd} /Users/Zoker/repos/</>`,
|
||||
}
|
||||
|
||||
// bind options
|
||||
gitee.BoolOpt(&nesOpts.forceSync, "force", "f", false, "Sync local repo to Gitee whether repo exists or not, like git push --force all")
|
||||
|
||||
// bind args with names
|
||||
gitee.AddArg("repoDir", "Tell me which repos your want to sync, is required", false)
|
||||
|
||||
return gitee
|
||||
}
|
||||
|
||||
func syncGitee(c *gcli.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
fmt.Printf("Tell me which repos your want to sync, Usage: ")
|
||||
color.Cyan.Println("up2 gitee /Users/Zoker/repos/")
|
||||
fmt.Println("See 'up2 gitee -h' for more details")
|
||||
} else {
|
||||
repoDir := args[0]
|
||||
// todo nesOpts.forceSync
|
||||
if share.DirExists(repoDir) {
|
||||
repos, _ := share.GetGitDir(repoDir)
|
||||
fmt.Println(repos)
|
||||
toGitee, _ := interact.ReadLine("Continue to auth Gitee? (y/n Default: y)")
|
||||
fmt.Println(toGitee)
|
||||
} else {
|
||||
fmt.Println("The path you provided is not a dir or not exists")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
package share
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gookit/color"
|
||||
)
|
||||
|
||||
func DirExists(path string) bool {
|
||||
s, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return s.IsDir()
|
||||
}
|
||||
|
||||
func GetGitDir(repoDir string) (repos []string, err error) {
|
||||
repos = make([]string, 0, 10)
|
||||
|
||||
dir, err := ioutil.ReadDir(repoDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pathSep := string(os.PathSeparator)
|
||||
|
||||
for _, repo := range dir {
|
||||
if !repo.IsDir() {
|
||||
continue
|
||||
}
|
||||
repoPath := repoDir + pathSep + repo.Name()
|
||||
isGit := isGitRepo(repoPath) // todo goroutine
|
||||
if isGit {
|
||||
repos = append(repos, repoPath)
|
||||
}
|
||||
}
|
||||
|
||||
printRepos(repos)
|
||||
|
||||
return repos, nil
|
||||
}
|
||||
|
||||
func isGitRepo(repoPath string) (isGit bool) {
|
||||
cmd := exec.Command("git", "tag")
|
||||
cmd.Dir = repoPath
|
||||
output, _ := cmd.CombinedOutput()
|
||||
result := string(output)
|
||||
if strings.Contains(result, "not a git repository") {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func dirSize(path string) (float32, bool, error) {
|
||||
var size int64
|
||||
err := filepath.Walk(path,func(_ string,info os.FileInfo,err error) error {
|
||||
if !info.IsDir() {
|
||||
size += info.Size()
|
||||
}
|
||||
return err
|
||||
})
|
||||
|
||||
outOf1G := false
|
||||
if size > 1024 * 1024 * 1024 {
|
||||
outOf1G = true
|
||||
}
|
||||
sizeMB := float32(size) / 1024.0 / 1024.0
|
||||
return sizeMB, outOf1G, err
|
||||
}
|
||||
|
||||
func printRepos(repos []string) {
|
||||
color.Yellow.Println(len(repos), "repositories detected, please check bellow: ")
|
||||
alertFlag := false
|
||||
for _, repo := range repos { // todo goroutine
|
||||
fmt.Printf(repo)
|
||||
size, outAlert, _ := dirSize(repo)
|
||||
alertFlag = alertFlag || outAlert
|
||||
if outAlert {
|
||||
color.Red.Printf(" %.2f", size)
|
||||
color.Red.Println("M")
|
||||
} else {
|
||||
color.Green.Printf(" %.2f", size)
|
||||
color.Green.Println("M")
|
||||
}
|
||||
}
|
||||
|
||||
if alertFlag {
|
||||
color.Yellow.Println("Warning: some of your local repo is out of 1G, please make sure that you account have permission to sync repository that size more than 1G")
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"up2GitX/platform"
|
||||
"github.com/gookit/gcli/v2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
up2 := gcli.NewApp()
|
||||
up2.Version = "1.0.0"
|
||||
up2.Description = "A tool for easily sync multiple local repo to different platform like Gitee, Github or Gitlab"
|
||||
up2.Add(platform.GiteeCommand())
|
||||
up2.Run()
|
||||
}
|
Loading…
Reference in New Issue