Skip to content

Commit d8b357e

Browse files
committed
Update printing
1 parent 69777f3 commit d8b357e

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

cmd/run.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ var runCmd = &cobra.Command{
6969
if len(args) == 1 {
7070
dir = args[0]
7171
}
72-
diffMap, err := gitkustomizediff.Run(dir, opts)
72+
res, err := gitkustomizediff.Run(dir, opts)
7373
if err != nil {
7474
fmt.Printf("%+v\n", err)
7575
}
7676

77-
printDiffMap(dir, opts, diffMap)
77+
printRunResult(dir, opts, res)
7878

7979
return nil
8080
},
@@ -93,36 +93,40 @@ func init() {
9393
runCmd.PersistentFlags().BoolVar(&runOpts.allowDirty, "allow-dirty", false, "allow dirty tree")
9494
}
9595

96-
func printDiffMap(dirPath string, opts gitkustomizediff.RunOpts, diffMap *gitkustomizediff.DiffMap) {
97-
dirs := diffMap.Dirs()
96+
func printRunResult(dirPath string, opts gitkustomizediff.RunOpts, res *gitkustomizediff.RunResult) {
97+
dirs := res.DiffMap.Dirs()
9898
fmt.Printf("# Git Kustomize Diff\n\n")
99+
100+
fmt.Printf("%s...%s\n\n", res.BaseCommit, res.TargetCommit)
101+
102+
fmt.Printf("<details><summary>Options</summary>\n\n")
99103
fmt.Println("| name | value |")
100104
fmt.Println("|-|-|")
101105
fmt.Printf("| dir | %s |\n", dirPath)
102106
fmt.Printf("| base | %s |\n", opts.Base)
103107
fmt.Printf("| target | %s |\n", opts.Target)
104-
fmt.Println("")
108+
fmt.Printf("\n</details>\n\n")
105109

106-
fmt.Printf("## Target Kustomizations\n\n")
110+
fmt.Printf("<details><summary>Target Kustomizations</summary>\n\n")
107111
if len(dirs) > 0 {
108112
fmt.Printf("```\n%s\n```\n", strings.Join(dirs, "\n"))
109113
} else {
110114
fmt.Println("N/A")
111115
}
112-
fmt.Println("")
116+
fmt.Printf("\n</details>\n\n")
113117

114-
fmt.Printf("## Diff\n\n")
115-
lines := make([]string, 0)
118+
found := false
116119
for _, dir := range dirs {
117-
text := diffMap.Results[dir].AsMarkdown()
120+
text := res.DiffMap.Results[dir].AsMarkdown()
118121
if text != "" {
119-
lines = append(lines, fmt.Sprintf("### %s:\n%s", dir, text))
122+
fmt.Printf("## %s\n\n", dir)
123+
fmt.Printf("<details><summary>diff</summary>\n\n")
124+
fmt.Println(text)
125+
fmt.Printf("\n</details>\n\n")
126+
found = true
120127
}
121128
}
122-
if len(lines) > 0 {
123-
fmt.Println(strings.Join(lines, "\n"))
124-
} else {
129+
if !found {
125130
fmt.Println("N/A")
126131
}
127-
fmt.Println("")
128132
}

pkg/gitkustomizediff/run.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ type RunOpts struct {
3737
AllowDirty bool
3838
}
3939

40-
func Run(dirPath string, opts RunOpts) (*DiffMap, error) {
40+
type RunResult struct {
41+
BaseCommit string
42+
TargetCommit string
43+
DiffMap *DiffMap
44+
}
45+
46+
func Run(dirPath string, opts RunOpts) (*RunResult, error) {
4147
log.Info("Start run")
4248
currentGitDir := utils.NewGitDir(dirPath, opts.GitPath)
4349
baseCommitish := opts.Base
@@ -121,5 +127,9 @@ func Run(dirPath string, opts RunOpts) (*DiffMap, error) {
121127
return nil, err
122128
}
123129

124-
return diffMap, nil
130+
return &RunResult{
131+
BaseCommit: baseCommit,
132+
TargetCommit: targetCommit,
133+
DiffMap: diffMap,
134+
}, nil
125135
}

pkg/gitkustomizediff/run_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ func TestRun(t *testing.T) {
5151
if !assert.NoError(t, err) {
5252
t.FailNow()
5353
}
54-
diffMap, err := Run(tmpGitDir, RunOpts{
54+
res, err := Run(tmpGitDir, RunOpts{
5555
Base: "origin/main",
5656
Target: "origin/a-branch",
5757
})
5858
if !assert.NoError(t, err) {
5959
t.FailNow()
6060
}
61-
assert.Equal(t, []string{"foo"}, diffMap.Dirs())
62-
assert.Equal(t, expectedFooDiff, diffMap.Results["foo"].ToString())
61+
assert.Equal(t, "6206e0c", res.BaseCommit)
62+
assert.Equal(t, "5a1c160", res.TargetCommit)
63+
assert.Equal(t, []string{"foo"}, res.DiffMap.Dirs())
64+
assert.Equal(t, expectedFooDiff, res.DiffMap.Results["foo"].ToString())
6365
}

0 commit comments

Comments
 (0)