optimize code to share

This commit is contained in:
Zoker 2020-06-06 13:30:25 +08:00
parent 878bf86b46
commit fa6a0a751b
3 changed files with 53 additions and 39 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
.idea
.idea
up2

View File

@ -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
}

View File

@ -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!")
}