Skip to content

Commit 759ec8a

Browse files
committed
feat(jzero): before executing command print command first
1 parent 53c0ecc commit 759ec8a

4 files changed

Lines changed: 73 additions & 15 deletions

File tree

cmd/jzero/internal/hooks/hooks.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import (
1414
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/execx"
1515
)
1616

17+
var (
18+
runOutputFn = execx.RunOutput
19+
printHookCommandFn = printHookCommand
20+
printHookOutputFn = printHookOutput
21+
)
22+
1723
func Run(cmd *cobra.Command, hookAction, hooksName string, hooks []string) error {
1824
wd, _ := os.Getwd()
1925

@@ -51,10 +57,10 @@ func Run(cmd *cobra.Command, hookAction, hooksName string, hooks []string) error
5157
}
5258

5359
for _, v := range hooks {
54-
output, err := execx.RunOutput(v, wd, "JZERO_HOOK_TRIGGERED=true")
5560
if !quiet {
56-
printHookCommand(v, err == nil)
61+
printHookCommandFn(v)
5762
}
63+
output, err := runOutputFn(v, wd, "JZERO_HOOK_TRIGGERED=true")
5864
if err != nil {
5965
lines := console.NormalizeErrorLines(output)
6066
if len(lines) == 0 {
@@ -74,7 +80,7 @@ func Run(cmd *cobra.Command, hookAction, hooksName string, hooks []string) error
7480
return console.MarkRenderedError(err)
7581
}
7682
if !quiet && output != "" {
77-
printHookOutput(output)
83+
printHookOutputFn(output)
7884
}
7985
}
8086

@@ -121,14 +127,10 @@ func Run(cmd *cobra.Command, hookAction, hooksName string, hooks []string) error
121127
return nil
122128
}
123129

124-
func printHookCommand(command string, success bool) {
130+
func printHookCommand(command string) {
125131
if strings.Contains(command, "\n") {
126132
lines := strings.Split(command, "\n")
127-
if success {
128-
fmt.Printf("%s\n", console.BoxItem(console.Cyan("Executing")))
129-
} else {
130-
fmt.Printf("%s\n", console.BoxErrorItem(console.Cyan("Executing")))
131-
}
133+
fmt.Printf("%s\n", console.BoxInfoItem(console.Cyan("Executing")))
132134
for _, line := range lines {
133135
trimmedLine := strings.TrimSpace(line)
134136
if trimmedLine != "" {
@@ -139,12 +141,7 @@ func printHookCommand(command string, success bool) {
139141
}
140142

141143
item := fmt.Sprintf("%s %s", console.Cyan("Executing"), command)
142-
if success {
143-
fmt.Printf("%s\n", console.BoxItem(item))
144-
return
145-
}
146-
147-
fmt.Printf("%s\n", console.BoxErrorItem(item))
144+
fmt.Printf("%s\n", console.BoxInfoItem(item))
148145
}
149146

150147
func printHookOutput(output string) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package hooks
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func TestRunPrintsHookCommandBeforeExecution(t *testing.T) {
11+
cmd := &cobra.Command{Use: "test"}
12+
cmd.Flags().Bool("quiet", false, "")
13+
14+
oldRunOutputFn := runOutputFn
15+
oldPrintHookCommandFn := printHookCommandFn
16+
oldPrintHookOutputFn := printHookOutputFn
17+
t.Cleanup(func() {
18+
runOutputFn = oldRunOutputFn
19+
printHookCommandFn = oldPrintHookCommandFn
20+
printHookOutputFn = oldPrintHookOutputFn
21+
})
22+
23+
var calls []string
24+
printHookCommandFn = func(command string) {
25+
calls = append(calls, "print:"+command)
26+
}
27+
runOutputFn = func(arg, dir string, env ...string) (string, error) {
28+
calls = append(calls, "run:"+arg)
29+
return "hook output", nil
30+
}
31+
printHookOutputFn = func(output string) {
32+
calls = append(calls, "output:"+output)
33+
}
34+
35+
if err := Run(cmd, "Before", "gen", []string{"echo test"}); err != nil {
36+
t.Fatalf("Run() error = %v", err)
37+
}
38+
39+
want := []string{
40+
"print:echo test",
41+
"run:echo test",
42+
"output:hook output",
43+
}
44+
45+
if !reflect.DeepEqual(calls, want) {
46+
t.Fatalf("Run() calls = %v, want %v", calls, want)
47+
}
48+
}

cmd/jzero/internal/pkg/console/console.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ func BoxItem(item string) string {
6161
return fmt.Sprintf("│ %s %s", CheckMark(), item)
6262
}
6363

64+
// BoxInfoItem creates a neutral informational box item.
65+
func BoxInfoItem(item string) string {
66+
return fmt.Sprintf("│ %s %s", Cyan(">"), item)
67+
}
68+
6469
// BoxErrorItem 创建box样式的错误条目
6570
func BoxErrorItem(item string) string {
6671
return fmt.Sprintf("│ %s %s", CrossMark(), item)

cmd/jzero/internal/pkg/console/console_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ func TestBoxErrorItem(t *testing.T) {
1010
}
1111
}
1212

13+
func TestBoxInfoItem(t *testing.T) {
14+
got := BoxInfoItem("Executing echo test")
15+
want := "│ " + Cyan(">") + " Executing echo test"
16+
if got != want {
17+
t.Fatalf("BoxInfoItem() = %q, want %q", got, want)
18+
}
19+
}
20+
1321
func TestBoxFooters(t *testing.T) {
1422
if got, want := BoxSuccessFooter(), "└─ "+Cyan("✓")+" "+Cyan("Complete"); got != want {
1523
t.Fatalf("BoxSuccessFooter() = %q, want %q", got, want)

0 commit comments

Comments
 (0)