Skip to content

Commit 950cc57

Browse files
authored
Merge pull request #34 from gomicro/verbose-output
adding verbose output with colorized log
2 parents 44cc487 + 95556a9 commit 950cc57

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

cmd/root.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func init() {
1818

1919
RootCmd.AddCommand(config.ConfigCmd)
2020

21-
RootCmd.PersistentFlags().Bool("verbose", false, "show more verbose output")
21+
RootCmd.PersistentFlags().BoolP("verbose", "v", false, "show more verbose output")
2222
err := viper.BindPFlag("verbose", RootCmd.PersistentFlags().Lookup("verbose"))
2323
if err != nil {
2424
fmt.Printf("Error setting up: %s\n", err)
@@ -71,6 +71,18 @@ func Execute() {
7171
}
7272
}
7373

74+
var forgeTheme = &scribe.Theme{
75+
Describe: func(s string) string {
76+
return "\033[1;36m" + s + "\033[0m"
77+
},
78+
Print: func(s string) string {
79+
return "\033[2m" + s + "\033[0m"
80+
},
81+
Error: func(err error) string {
82+
return "\033[1;31m" + err.Error() + "\033[0m"
83+
},
84+
}
85+
7486
func rootFunc(cmd *cobra.Command, args []string) error {
7587
verbose := viper.GetBool("verbose")
7688

@@ -79,7 +91,7 @@ func rootFunc(cmd *cobra.Command, args []string) error {
7991
writer = os.Stdout
8092
}
8193

82-
scrb, err := scribe.NewScribe(writer, scribe.DefaultTheme)
94+
scrb, err := scribe.NewScribe(writer, forgeTheme)
8395
if err != nil {
8496
return fmt.Errorf("setting up output: %w", err)
8597
}
@@ -97,12 +109,14 @@ func rootFunc(cmd *cobra.Command, args []string) error {
97109
}
98110

99111
for _, s := range args {
100-
err := conf.Steps[s].Execute(conf.Steps, conf.Envs, conf.Vars, scrb)
112+
err := conf.Steps[s].Execute(s, conf.Steps, conf.Envs, conf.Vars, scrb)
101113
if err != nil {
102114
return fmt.Errorf("executing step %v: %w", s, err)
103115
}
104116
}
105117

118+
fmt.Fprintln(writer)
119+
106120
return nil
107121
}
108122

confile/step.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,49 @@ type Step struct {
2626

2727
// Execute runs the command that is specified for the step. It returns the output
2828
// of the command and any errors it encounters.
29-
func (s *Step) Execute(allSteps map[string]*Step, projectEnvs map[string]string, vars *vars.Vars, scrb scribe.Scriber) error {
29+
func (s *Step) Execute(name string, allSteps map[string]*Step, projectEnvs map[string]string, vars *vars.Vars, scrb scribe.Scriber) error {
3030
skipPre := viper.GetBool("solo") || viper.GetBool("no-pre")
3131
skipPost := viper.GetBool("solo") || viper.GetBool("no-post")
3232

3333
s.projectEnvs = projectEnvs
3434
s.vars = vars
3535

3636
if len(s.Pre) > 0 && !skipPre {
37+
scrb.BeginDescribe(name + ": pre")
3738
err := s.executeSteps(s.Pre, allSteps, scrb)
39+
scrb.EndDescribe()
3840
if err != nil {
3941
return fmt.Errorf("step: execute pre: %w", err)
4042
}
4143
}
4244

4345
if len(s.Steps) > 0 {
46+
scrb.BeginDescribe(name)
4447
err := s.executeSteps(s.Steps, allSteps, scrb)
48+
scrb.EndDescribe()
4549
if err != nil {
4650
return fmt.Errorf("step: execute steps: %w", err)
4751
}
4852
} else if len(s.Cmds) > 0 {
53+
scrb.BeginDescribe(name)
4954
err := s.executeCmds(scrb)
55+
scrb.EndDescribe()
5056
if err != nil {
5157
return fmt.Errorf("step: execute cmds: %w", err)
5258
}
5359
} else {
60+
scrb.BeginDescribe(name)
5461
err := s.executeCmd(scrb)
62+
scrb.EndDescribe()
5563
if err != nil {
5664
return fmt.Errorf("step: execute cmd: %w", err)
5765
}
5866
}
5967

6068
if len(s.Post) > 0 && !skipPost {
69+
scrb.BeginDescribe(name + ": post")
6170
err := s.executeSteps(s.Post, allSteps, scrb)
71+
scrb.EndDescribe()
6272
if err != nil {
6373
return fmt.Errorf("step: execute post: %w", err)
6474
}
@@ -86,13 +96,13 @@ func (s *Step) executeCmds(scrb scribe.Scriber) error {
8696
}
8797

8898
func (s *Step) executeSteps(execList []string, allSteps map[string]*Step, scrb scribe.Scriber) error {
89-
for _, step := range execList {
90-
s, ok := allSteps[step]
99+
for _, stepName := range execList {
100+
step, ok := allSteps[stepName]
91101
if !ok {
92-
return fmt.Errorf("step does not exist: %v", step)
102+
return fmt.Errorf("step does not exist: %v", stepName)
93103
}
94104

95-
err := s.Execute(allSteps, s.projectEnvs, s.vars, scrb)
105+
err := step.Execute(stepName, allSteps, step.projectEnvs, step.vars, scrb)
96106
if err != nil {
97107
return err
98108
}

0 commit comments

Comments
 (0)