Skip to content

Commit f8e2d27

Browse files
committed
🐼 Add no-color option (Fix #31)
1 parent bbf1dcc commit f8e2d27

File tree

12 files changed

+131
-83
lines changed

12 files changed

+131
-83
lines changed

cmd/envinfo.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"github.com/jedib0t/go-pretty/v6/table"
5-
"github.com/mitchellh/colorstring"
65
"github.com/shirou/gopsutil/v4/cpu"
76
"github.com/shirou/gopsutil/v4/host"
87
"github.com/spf13/cobra"
@@ -54,9 +53,9 @@ func printInfo() {
5453
}
5554

5655
func header(value string) string {
57-
return colorstring.Color("[bold][yellow]" + value + "[default]")
56+
return utils.ColorString("[bold][yellow]" + value + "[default]")
5857
}
5958

6059
func colorValue(value string) string {
61-
return colorstring.Color("[blue]" + value)
60+
return utils.ColorString("[blue]" + value)
6261
}

cmd/gitclone.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/go-git/go-git/v5"
77
"github.com/go-git/go-git/v5/plumbing/transport/http"
88
"github.com/google/go-github/v68/github"
9-
"github.com/mitchellh/colorstring"
109
log "github.com/sirupsen/logrus"
1110
"github.com/spf13/cobra"
1211
"github.com/yodamad/heimdall/commons"
@@ -42,7 +41,7 @@ var GitClone = &cobra.Command{
4241
log.SetLevel(log.DebugLevel)
4342
}
4443
if keepSuffix && !hostname {
45-
utils.TraceWarn(colorstring.Color("[bold]keep-suffix[reset][light_yellow] option is ignored because [bold]host[reset][light_yellow] option is not enabled"))
44+
utils.TraceWarn(utils.ColorString("[bold]keep-suffix[reset][light_yellow] option is ignored because [bold]host[reset][light_yellow] option is not enabled"))
4645
}
4746
if !strings.HasSuffix(commons.WorkDir, "/") {
4847
commons.WorkDir += "/"
@@ -79,7 +78,7 @@ func cloneGitlabGroup(groupUrl string) {
7978
hostnameOfRepo := parsedUrl.Hostname()
8079
groupPath := parsedUrl.Path
8180

82-
utils.Trace(colorstring.Color("[light_blue] Listing projects in [yellow]GitLab[light_blue] group [cyan]"+groupPath), false)
81+
utils.Trace(utils.ColorString("[light_blue] Listing projects in [yellow]GitLab[light_blue] group [cyan]"+groupPath), false)
8382

8483
gitlabClient, err := gitlab.NewClient(utils.GetToken(hostnameOfRepo, nil))
8584
if err != nil {
@@ -92,7 +91,7 @@ func cloneGitlabGroup(groupUrl string) {
9291
IncludeSubGroups: gitlab.Ptr(true),
9392
})
9493
if err != nil {
95-
utils.TraceWarn(colorstring.Color("Cannot retrieve projects from group : [red]" + err.Error()))
94+
utils.TraceWarn(utils.ColorString("Cannot retrieve projects from group : [red]" + err.Error()))
9695
}
9796

9897
for _, project := range projects {
@@ -106,14 +105,14 @@ func cloneGithubGroup(orgUrl string) {
106105
hostnameOfOrg := parsedUrl.Hostname()
107106
orgPath := parsedUrl.Path
108107

109-
utils.Trace(colorstring.Color("[light_blue] Listing projects in [yellow]GitHub[light_blue] organization [cyan]"+orgUrl), false)
108+
utils.Trace(utils.ColorString("[light_blue] Listing projects in [yellow]GitHub[light_blue] organization [cyan]"+orgUrl), false)
110109

111110
token := utils.GetToken(hostnameOfOrg, nil)
112111
githubClient := github.NewClient(nil).WithAuthToken(token)
113112
cleanUrl := strings.TrimSuffix(strings.TrimPrefix(orgPath, "/"), "/")
114113
repos, _, err := githubClient.Repositories.ListByOrg(context.Background(), cleanUrl, &github.RepositoryListByOrgOptions{})
115114
if err != nil {
116-
utils.TraceWarn(colorstring.Color("Cannot retrieve projects from group : [red]" + err.Error()))
115+
utils.TraceWarn(utils.ColorString("Cannot retrieve projects from group : [red]" + err.Error()))
117116
}
118117
for _, project := range repos {
119118
projectUrl := project.GetCloneURL()
@@ -122,7 +121,7 @@ func cloneGithubGroup(orgUrl string) {
122121
}
123122

124123
func cloneRepo(inputUrl string) {
125-
utils.Trace(colorstring.Color("[light_blue]🧬 Cloning [cyan]"+inputUrl+"..."), false)
124+
utils.Trace(utils.ColorString("[light_blue]🧬 Cloning [cyan]"+inputUrl+"..."), false)
126125

127126
parsedUrl, _ := url.Parse(inputUrl)
128127
hostnameOfRepo := parsedUrl.Hostname()
@@ -137,7 +136,7 @@ func cloneRepo(inputUrl string) {
137136
} else {
138137
doClone(inputUrl, commons.WorkDir+pathToRepo)
139138
}
140-
utils.Trace(colorstring.Color("[light_blue]✅ [cyan]"+inputUrl+"[light_blue] cloned"), false)
139+
utils.Trace(utils.ColorString("[light_blue]✅ [cyan]"+inputUrl+"[light_blue] cloned"), false)
141140
}
142141

143142
func doClone(inputUrl string, path string) {
@@ -147,14 +146,14 @@ func doClone(inputUrl string, path string) {
147146
utils.Trace("Create directory "+path, true)
148147
err := os.MkdirAll(path, os.ModePerm)
149148
if err != nil {
150-
utils.TraceWarn(colorstring.Color("❌ Cannot create path : [red] " + err.Error()))
149+
utils.TraceWarn(utils.ColorString("❌ Cannot create path : [red] " + err.Error()))
151150
}
152151
_, err = git.PlainClone(path, false, &git.CloneOptions{
153152
Auth: &http.BasicAuth{Password: utils.GetToken(hostnameOfRepo, nil)},
154153
URL: inputUrl + ".git",
155154
Progress: nil,
156155
})
157156
if err != nil {
158-
utils.TraceWarn(colorstring.Color("❌ Git clone failed: [red] " + err.Error()))
157+
utils.TraceWarn(utils.ColorString("❌ Git clone failed: [red] " + err.Error()))
159158
}
160159
}

cmd/gitinfo.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/go-git/go-git/v5"
88
"github.com/go-git/go-git/v5/plumbing/transport/http"
99
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
10-
"github.com/mitchellh/colorstring"
1110
log "github.com/sirupsen/logrus"
1211
"github.com/spf13/cobra"
1312
"github.com/yodamad/heimdall/cmd/entity"
@@ -53,23 +52,23 @@ func listGitDirs() {
5352
answer := "n"
5453

5554
for interactiveMode && !strings.EqualFold(answer, "y") {
56-
answer = commons.AskQuestion(colorstring.Color("🔍 Search in directory "+tui.PathColor+rootDir+"[default] [light_gray][Y/n][default] : "), "Y")
55+
answer = tui.AskQuestion(utils.ColorString("🔍 Search in directory "+tui.PathColor+rootDir+"[default] [light_gray][Y/n][default] : "), "Y")
5756
if strings.EqualFold(answer, "n") {
5857
if strings.EqualFold(answer, "n") {
59-
answer = commons.AskQuestion(colorstring.Color("➡️ Directory to search in : "), rootDir)
58+
answer = tui.AskQuestion(utils.ColorString("➡️ Directory to search in : "), rootDir)
6059
}
6160
rootDir = answer
6261
} else if answer == "" {
6362
answer = "y"
6463
} else if !strings.EqualFold(answer, "y") {
65-
fmt.Println(colorstring.Color("[yellow]Unknown option value : [light_gray]" + answer))
64+
fmt.Println(utils.ColorString("[yellow]Unknown option value : [light_gray]" + answer))
6665
// Reset answer
6766
answer = "n"
6867
}
6968
}
7069

7170
if !interactiveMode {
72-
utils.Trace(colorstring.Color("🔍 Search in directory "+tui.PathColor+rootDir+"[default]"), false)
71+
utils.Trace(utils.ColorString("🔍 Search in directory "+tui.PathColor+rootDir+"[default]"), false)
7372
}
7473

7574
// Initialize the spinner
@@ -83,9 +82,9 @@ func listGitDirs() {
8382
}
8483

8584
if rootDir == commons.DefaultWorkDir {
86-
m.Text = colorstring.Color("Searching in [bold]default directory[default] : " + tui.PathColor + "'" + rootDir + "'[default]")
85+
m.Text = utils.ColorString("Searching in [bold]default directory[default] : " + tui.PathColor + "'" + rootDir + "'[default]")
8786
} else {
88-
m.Text = colorstring.Color("Searching in " + tui.PathColor + "'" + rootDir + "'[default] ...")
87+
m.Text = utils.ColorString("Searching in " + tui.PathColor + "'" + rootDir + "'[default] ...")
8988
}
9089

9190
// Start the spinner
@@ -150,20 +149,20 @@ func checkDir(rootDir string, spinner *tea.Program) tea.Cmd {
150149
if nbGitFolders > 0 {
151150
utils.PrintTable(gitFolders)
152151
if nbSkippedFolders > 0 {
153-
utils.Trace(colorstring.Color("Found [green]"+strconv.Itoa(nbGitFolders)+"[default] folder(s) (Skip [yellow]"+strconv.Itoa(nbSkippedFolders)+"[default] folders because of errors, use '-v' to check in details)"), false)
152+
utils.Trace(utils.ColorString("Found [green]"+strconv.Itoa(nbGitFolders)+"[default] folder(s) (Skip [yellow]"+strconv.Itoa(nbSkippedFolders)+"[default] folders because of errors, use '-v' to check in details)"), false)
154153
} else {
155-
utils.Trace(colorstring.Color("Found [green]"+strconv.Itoa(nbGitFolders)+"[default] folder(s)"), false)
154+
utils.Trace(utils.ColorString("Found [green]"+strconv.Itoa(nbGitFolders)+"[default] folder(s)"), false)
156155
}
157156
if commons.Interactive {
158157
chooseInteractiveOption(spinner)
159158
}
160159
} else {
161160
if nbSkippedFolders > 0 {
162-
utils.Trace(colorstring.Color("😕 [red]No git folder found[default] (Skip [yellow]"+strconv.Itoa(nbSkippedFolders)+"[default] folders because of errors, use '-v' to check in details)"), false)
161+
utils.Trace(utils.ColorString("😕 [red]No git folder found[default] (Skip [yellow]"+strconv.Itoa(nbSkippedFolders)+"[default] folders because of errors, use '-v' to check in details)"), false)
163162
} else {
164-
utils.Trace(colorstring.Color("😕 [red]No git folder found"), false)
163+
utils.Trace(utils.ColorString("😕 [red]No git folder found"), false)
165164
}
166-
utils.Trace(colorstring.Color("🤔 Is "+tui.PathColor+rootDir+"[default] the correct path ?"), false)
165+
utils.Trace(utils.ColorString("🤔 Is "+tui.PathColor+rootDir+"[default] the correct path ?"), false)
167166
}
168167

169168
return tea.Quit
@@ -228,7 +227,7 @@ func chooseInteractiveOption(spinner *tea.Program) {
228227
choices = append(choices, "📥 Display remote commits of a repository")
229228
}
230229
if checkIfAtLeastOne(gitFolders, func(folder entity.GitFolder) bool { return entity.CanPull(folder) }) {
231-
choices = append(choices, colorstring.Color("🔃 Update one or several repositories ([dim]git pull[reset])"))
230+
choices = append(choices, utils.ColorString("🔃 Update one or several repositories ([dim]git pull[reset])"))
232231
}
233232
choices = append(choices, "✅ I'm done")
234233

@@ -249,11 +248,11 @@ func chooseInteractiveOption(spinner *tea.Program) {
249248
utils.PrintSeparation()
250249
folder := pickSingleItem(gitFolders, func(folder entity.GitFolder) bool { return entity.HasRemoteChanges(folder) })
251250
listRemoteChanges(folder)
252-
case colorstring.Color("🔃 Update one or several repositories ([dim]git pull[reset])"):
251+
case utils.ColorString("🔃 Update one or several repositories ([dim]git pull[reset])"):
253252
toUpdate := selectItems(gitFolders, func(folder entity.GitFolder) bool { return entity.CanPull(folder) })
254253
utils.PrintSeparation()
255254
if len(toUpdate) > 0 {
256-
utils.Trace(colorstring.Color(tui.TitleColor+"Pulling repositories :[default]\n"), false)
255+
utils.Trace(utils.ColorString(tui.TitleColor+"Pulling repositories :[default]\n"), false)
257256
}
258257
for _, folder := range toUpdate {
259258
gitPull(folder)
@@ -290,7 +289,7 @@ func listLocalChanges(path string, spinner *tea.Program) {
290289
w, _ := repo.Worktree()
291290
s, _ := w.Status()
292291

293-
utils.Trace(colorstring.Color("🚦 [dark_gray]"+strconv.Itoa(len(s))+" files"), false)
292+
utils.Trace(utils.ColorString("🚦 [dark_gray]"+strconv.Itoa(len(s))+" files"), false)
294293
for filename, _ := range s {
295294
fileStatus := s.File(filename)
296295
fmt.Printf("%s - %s \n", filename, string(fileStatus.Worktree))
@@ -305,7 +304,7 @@ func listRemoteChanges(gitFolder string) {
305304

306305
fullOutput := string(out)
307306

308-
utils.Trace(colorstring.Color("🚦 [dark_gray]"+strconv.Itoa(strings.Count(fullOutput, "\n"))+" commits"), false)
307+
utils.Trace(utils.ColorString("🚦 [dark_gray]"+strconv.Itoa(strings.Count(fullOutput, "\n"))+" commits"), false)
309308
utils.Trace(string(out), false)
310309
}
311310

@@ -340,7 +339,7 @@ func pickSingleItem(items []entity.GitFolder, fn filterFolder) string {
340339

341340
func selectItems(items []entity.GitFolder, fn filterFolder) []entity.GitFolder {
342341
utils.PrintSeparation()
343-
var q = colorstring.Color(tui.TitleColor + "Pick repositories to update:[default]")
342+
var q = utils.ColorString(tui.TitleColor + "Pick repositories to update:[default]")
344343

345344
var filteredItems []entity.GitFolder
346345
for _, item := range items {
@@ -405,7 +404,7 @@ func gitPull(folder entity.GitFolder) {
405404
worktree, _ := repo.Worktree()
406405
err := worktree.Pull(&git.PullOptions{RemoteName: "origin"})
407406
if err != nil {
408-
utils.TraceWarn(colorstring.Color("Cannot pull : [red]" + err.Error()))
407+
utils.TraceWarn(utils.ColorString("Cannot pull : [red]" + err.Error()))
409408
}
410-
utils.Trace(colorstring.Color("✅ [bold]"+folder.Path+"[reset] pulled"), false)
409+
utils.Trace(utils.ColorString("✅ [bold]"+folder.Path+"[reset] pulled"), false)
411410
}

commons/console.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

commons/const.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package commons
22

33
import (
44
"fmt"
5-
"os"
6-
"runtime"
7-
85
"github.com/mitchellh/colorstring"
96
"github.com/yodamad/heimdall/build"
7+
"os"
8+
"runtime"
109
)
1110

1211
var DefaultConfFolderFunc = func() string {
@@ -65,6 +64,9 @@ var Verbose bool
6564
// Interactive /* Interactive mode */
6665
var Interactive bool
6766

67+
// NoColor /* No color output */
68+
var NoColor bool
69+
6870
// InputConfigFile /* The config file to use */
6971
var InputConfigFile = DefaultConfFolder + DefaultConfigFile
7072

main.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,26 @@ package main
22

33
import (
44
"fmt"
5-
"os"
6-
7-
"github.com/mitchellh/colorstring"
85
log "github.com/sirupsen/logrus"
96
"github.com/spf13/cobra"
107
"github.com/yodamad/heimdall/cmd"
118
"github.com/yodamad/heimdall/commons"
129
"github.com/yodamad/heimdall/utils"
10+
"os"
1311
)
1412

1513
var rootCmd = &cobra.Command{
1614
Use: "heimdall",
17-
Short: colorstring.Color("[yellow]Heimdall[default] helps you with your git folders"),
18-
Long: colorstring.Color(`
15+
Short: utils.ColorString("[yellow]Heimdall[default] helps you with your git folders"),
16+
Long: utils.ColorString(`
1917
[yellow]Heimdall[default] is a CLI tool to help you with your git folders.
2018
You can check, update, ... everything easily
2119
`),
2220
Args: cobra.MinimumNArgs(1),
2321
Run: func(cmd *cobra.Command, args []string) {
2422
utils.PrintBanner()
2523
},
26-
Example: colorstring.Color("[light_blue]heimdall -h"),
24+
Example: utils.ColorString("[light_blue]heimdall -h"),
2725
}
2826

2927
func init() {
@@ -34,6 +32,7 @@ func init() {
3432
rootCmd.PersistentFlags().StringVarP(&commons.LogDir, "log-dir", "l", commons.DefaultLogFolder, "log directory")
3533
rootCmd.PersistentFlags().StringVarP(&commons.WorkDir, "work-dir", "w", commons.DefaultWorkDir, "work directory")
3634
rootCmd.PersistentFlags().StringVarP(&commons.InputConfigFile, "config-file", "c", commons.InputConfigFile, "config file")
35+
rootCmd.PersistentFlags().BoolVarP(&commons.NoColor, "no-color", "n", false, "no color output")
3736

3837
// Create directory for configuration
3938
_, err := os.Stat(commons.DefaultConfFolder)

0 commit comments

Comments
 (0)