Skip to content

Commit

Permalink
feat: move helper and logger to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
amazingandyyy committed Apr 18, 2022
1 parent 66204bb commit 7c42609
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 54 deletions.
Binary file modified bin/gofuzzyclone
Binary file not shown.
76 changes: 22 additions & 54 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"strings"
"time"

"gofuzzyclone/lib/helper"
"gofuzzyclone/lib/logger"

"github.com/briandowns/spinner"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
Expand All @@ -22,36 +25,6 @@ import (

var configFilePath string = os.ExpandEnv("$HOME/.gofuzzyclone.json")

func check(e error) {
if e != nil {
panic(e)
}
}

func println(color string, message string) {
color = strings.ToLower(color)
var color_palettes = map[string]string{
"red": "\033[31m",
"green": "\033[32m",
"yellow": "\033[33m",
"reset": "\033[0m",
}

fmt.Printf("%s%s%s\n", string(color_palettes[color]), message, color_palettes["reset"])
}

func printf(color string, message string) {
color = strings.ToLower(color)
var color_palettes = map[string]string{
"red": "\033[31m",
"green": "\033[32m",
"yellow": "\033[33m",
"reset": "\033[0m",
}

fmt.Printf("%s%s%s", string(color_palettes[color]), message, color_palettes["reset"])
}

type config struct {
Ghtoken string `json:"github_token"`
}
Expand All @@ -63,27 +36,22 @@ func validateGhToken(ghToken string) {
client := github.NewClient(tc)

_, _, err := client.Repositories.List(ctx, "github", nil)
if err != nil {
println("red", "Invalid token")
os.Exit(1)
}
helper.HandleError(err, "Invalid token")
}

func renewGhToken() {
ghToken := ""
hasConfigFile := false
println("yellow", "Generate token at https://github.com/settings/tokens/new?scopes=repo&description=gofuzzyclone-cli")
logger.Println("yellow", "Generate token at https://github.com/settings/tokens/new?scopes=repo&description=gofuzzyclone-cli")
fmt.Println("GITHUB_TOKEN: ")
fmt.Scanf("%s", &ghToken)
validateGhToken(ghToken)
println("green", "Token is valid")
logger.Println("green", "Token is valid")

var jsonBlob = []byte(`{"github_token": "` + ghToken + `"}`)
cf := config{}
err := json.Unmarshal(jsonBlob, &cf)
if err != nil {
panic(err)
}
helper.HandleError(err)
if _, err := os.Stat(configFilePath); err == nil {
hasConfigFile = true
}
Expand All @@ -92,10 +60,10 @@ func renewGhToken() {
if !hasConfigFile {
// create one
f, err := os.Create(configFilePath)
check(err)
helper.HandleError(err)
defer f.Close()
err = ioutil.WriteFile(configFilePath, cfJson, 0644)
check(err)
helper.HandleError(err)
}
}
}
Expand Down Expand Up @@ -148,10 +116,7 @@ func getPersonalRepos(client *github.Client, owner string) ([]*github.Repository
allRepos = allRepos[:max]
break
}
if err != nil {
log.Fatal("Unable to fetch respositories for the user: ", err)
return nil, err
}
helper.HandleError(err, fmt.Sprintf("Unable to fetch respositories for the user: %s", err))
if len(repos) == 0 {
break
}
Expand Down Expand Up @@ -219,11 +184,11 @@ func main() {
}

if *search == "" {
fmt.Printf("search: ")
fmt.Printf("Search: ")
fmt.Scanf("%s", search)
}
if *owner == "" {
fmt.Printf("owner: ")
fmt.Printf("Owner: ")
fmt.Scanf("%s", owner)
}

Expand Down Expand Up @@ -257,18 +222,21 @@ func main() {
fmt.Printf("%d %v\n", i+1, repo.GetFullName())
}
confirm := "n"
println("green", fmt.Sprintf("Result: %d repos match %q", len(all_repos_matched), *search))
logger.Println("green", fmt.Sprintf("Result: %d repos match %q", len(all_repos_matched), *search))
if len(all_repos_matched) == 0 {
os.Exit(0)
}

if *dest == "" {
fmt.Printf("Clone %d repos to which folder? ", len(all_repos_matched))
fmt.Scanf("%s", dest)
}

printf("yellow", "Confirm? (Y/n) ")
logger.Printf("yellow", "Confirm? (Y/n) ")
fmt.Scanf("%s", &confirm)

if confirm != "Y" {
println("red", "Aborted")
logger.Println("red", "Aborted")
os.Exit(0)
}

Expand All @@ -283,14 +251,14 @@ func main() {
})
if err != nil {
if err.Error() == "repository already exists" {
println("yellow", fmt.Sprintf("Skipped: %v (already exists)", repo.GetName()))
logger.Println("yellow", fmt.Sprintf("Skipped: %v (already exists)", repo.GetName()))
} else {
println("red", fmt.Sprintf("Error cloning: %v", err.Error()))
logger.Println("red", fmt.Sprintf("Error cloning: %v", err.Error()))
}
} else {
println("green", fmt.Sprintf("Cloned: %v", repo.GetName()))
logger.Println("green", fmt.Sprintf("Cloned: %v", repo.GetName()))
}
}

println("green", fmt.Sprintf("DONE with %d repositories", len(all_repos_matched)))
logger.Println("green", fmt.Sprintf("DONE with %d repositories", len(all_repos_matched)))
}
18 changes: 18 additions & 0 deletions lib/helper/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package helper

import (
"fmt"
"gofuzzyclone/lib/logger"
)

// HandleError is a helper function to handle error
func HandleError(e error, msg ...string) {
if e != nil {
if len(msg) > 0 {
logger.Println("red", msg[0])
} else {
fmt.Println(e)
}
panic(e)
}
}
32 changes: 32 additions & 0 deletions lib/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package logger

import (
"fmt"
"strings"
)

// Println prints a message with a color with \n
func Println(color string, message string) {
color = strings.ToLower(color)
var color_palettes = map[string]string{
"red": "\033[31m",
"green": "\033[32m",
"yellow": "\033[33m",
"reset": "\033[0m",
}

fmt.Printf("%s%s%s\n", string(color_palettes[color]), message, color_palettes["reset"])
}

// Printf prints a message with a color
func Printf(color string, message string) {
color = strings.ToLower(color)
var color_palettes = map[string]string{
"red": "\033[31m",
"green": "\033[32m",
"yellow": "\033[33m",
"reset": "\033[0m",
}

fmt.Printf("%s%s%s", string(color_palettes[color]), message, color_palettes["reset"])
}

0 comments on commit 7c42609

Please sign in to comment.