From fa6a0a751bd7a8b90417b6f7fe9a83960c25f235 Mon Sep 17 00:00:00 2001 From: Zoker Date: Sat, 6 Jun 2020 13:30:25 +0800 Subject: [PATCH] optimize code to share --- .gitignore | 3 +- platform/gitee.go | 19 ++----------- share/tools.go | 70 +++++++++++++++++++++++++++++++++-------------- 3 files changed, 53 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 723ef36..e2f2a3a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea \ No newline at end of file +.idea +up2 \ No newline at end of file diff --git a/platform/gitee.go b/platform/gitee.go index 15fa3b9..ba880d8 100644 --- a/platform/gitee.go +++ b/platform/gitee.go @@ -1,12 +1,9 @@ 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 @@ -33,22 +30,10 @@ func GiteeCommand() *gcli.Command { 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") + share.InvalidAlert(c.Name) } 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 + share.ReadyToAuth(args[0]) } return nil } \ No newline at end of file diff --git a/share/tools.go b/share/tools.go index 2d0e2fc..cd7bc11 100644 --- a/share/tools.go +++ b/share/tools.go @@ -9,8 +9,15 @@ import ( "strings" "github.com/gookit/color" + "github.com/gookit/gcli/v2/interact" ) +func InvalidAlert(platform string) { + fmt.Printf("Tell me which repos dir your want to sync, Usage: ") + color.Yellow.Printf("up2 %s /Users/Zoker/repos/\n", platform) + fmt.Printf("See 'up2 %s -h' for more details\n", platform) +} + func DirExists(path string) bool { s, err := os.Stat(path) if err != nil { @@ -33,15 +40,12 @@ func GetGitDir(repoDir string) (repos []string, err error) { if !repo.IsDir() { continue } - repoPath := repoDir + pathSep + repo.Name() - isGit := isGitRepo(repoPath) // todo goroutine - if isGit { + repoPath := repoDir + pathSep + repo.Name() // todo check repo path valid + if isGitRepo(repoPath) { // todo goroutine repos = append(repos, repoPath) } } - printRepos(repos) - return repos, nil } @@ -57,7 +61,28 @@ func isGitRepo(repoPath string) (isGit bool) { } } -func dirSize(path string) (float32, bool, error) { +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, _ := repoSize(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") + } +} + +func repoSize(path string) (float32, bool, error) { var size int64 err := filepath.Walk(path,func(_ string,info os.FileInfo,err error) error { if !info.IsDir() { @@ -74,23 +99,26 @@ func dirSize(path string) (float32, bool, error) { 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") +func ReadyToAuth(repoDir string) []string { + if DirExists(repoDir) { + repos, _ := GetGitDir(repoDir) + if len(repos) == 0 { + color.Red.Printf("No git repositories detected in %s \n", repoDir) } else { - color.Green.Printf(" %.2f", size) - color.Green.Println("M") + printRepos(repos) + toGitee, _ := interact.ReadLine("Continue to auth Gitee? (y/n)") + if toGitee == "y" { + return repos + } else { + ExitMessage() + } } + } else { + color.Red.Println("The path you provided is not a dir or not exists") } + return nil +} - 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") - } +func ExitMessage() { + color.Yellow.Println("Bye, see you next time!") } \ No newline at end of file