Skip to content

Commit 5a60156

Browse files
committed
refactor: introduce display package for improved package listing and output management
1 parent 0aace78 commit 5a60156

File tree

5 files changed

+97
-48
lines changed

5 files changed

+97
-48
lines changed

cmd/list.go

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

33
import (
44
"fmt"
5-
"os"
65
"strings"
76

87
"github.com/fatih/color"
98
"github.com/gambitier/tag-manager/pkg/discovery"
10-
"github.com/olekukonko/tablewriter"
9+
"github.com/gambitier/tag-manager/pkg/display"
1110
"github.com/spf13/cobra"
1211
)
1312

@@ -40,44 +39,13 @@ func runList(cmd *cobra.Command, args []string) error {
4039
return nil
4140
}
4241

43-
color.Cyan("Discovered %d Go packages:", len(packages))
44-
color.White("Search paths: %s", strings.Join(searchPaths, ", "))
45-
color.White("")
46-
47-
// Create table with modern API
48-
table := tablewriter.NewWriter(os.Stdout)
49-
42+
// Determine display mode
43+
mode := display.Compact
5044
if verbose {
51-
table.Header("#", "Module", "Package", "Go Version", "GitHub", "Latest Tag")
52-
} else {
53-
table.Header("#", "Package", "Latest Tag")
54-
}
55-
56-
// Add rows
57-
for i, pkg := range packages {
58-
// Handle empty values
59-
goVersion := pkg.GoVersion
60-
if goVersion == "" {
61-
goVersion = "-"
62-
}
63-
64-
github := pkg.GitHubRepo
65-
if github == "" {
66-
github = "-"
67-
}
68-
69-
latestTag := pkg.LatestTag
70-
if latestTag == "" {
71-
latestTag = "(no tags)"
72-
}
73-
74-
if verbose {
75-
table.Append(fmt.Sprintf("%d", i+1), pkg.ModulePath, pkg.PackageName, goVersion, github, latestTag)
76-
} else {
77-
table.Append(fmt.Sprintf("%d", i+1), pkg.PackageName, latestTag)
78-
}
45+
mode = display.Verbose
7946
}
8047

81-
table.Render()
48+
// Show package list with header
49+
display.ShowPackageListWithHeader(packages, mode, searchPaths)
8250
return nil
8351
}

cmd/update.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/fatih/color"
99
"github.com/gambitier/tag-manager/pkg/config"
1010
"github.com/gambitier/tag-manager/pkg/discovery"
11+
"github.com/gambitier/tag-manager/pkg/display"
1112
"github.com/gambitier/tag-manager/pkg/interactive"
1213
"github.com/gambitier/tag-manager/pkg/tagutils"
1314
"github.com/spf13/cobra"
@@ -45,6 +46,11 @@ func runUpdate(cmd *cobra.Command, args []string) error {
4546
return nil
4647
}
4748

49+
// Display available packages
50+
color.Cyan("Available packages:")
51+
display.ShowPackageList(packages, display.Compact)
52+
color.White("")
53+
4854
// Let user select a package
4955
selectedPackage, err := interactive.SelectPackage(packages)
5056
if err != nil {

pkg/display/display.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package display
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
"github.com/fatih/color"
9+
"github.com/gambitier/tag-manager/pkg/discovery"
10+
"github.com/olekukonko/tablewriter"
11+
)
12+
13+
// DisplayMode represents the display mode for package lists
14+
type DisplayMode int
15+
16+
const (
17+
// Compact shows only package name and latest tag
18+
Compact DisplayMode = iota
19+
// Verbose shows all details
20+
Verbose
21+
)
22+
23+
// ShowPackageList displays a list of packages in a table format
24+
func ShowPackageList(packages []discovery.Package, mode DisplayMode) {
25+
if len(packages) == 0 {
26+
color.Red("No Go packages found.")
27+
return
28+
}
29+
30+
// Create table
31+
table := tablewriter.NewWriter(os.Stdout)
32+
33+
if mode == Verbose {
34+
table.Header("#", "Module", "Package", "Go Version", "GitHub", "Latest Tag")
35+
} else {
36+
table.Header("#", "Package", "Latest Tag")
37+
}
38+
39+
// Add rows
40+
for i, pkg := range packages {
41+
// Handle empty values
42+
goVersion := pkg.GoVersion
43+
if goVersion == "" {
44+
goVersion = "-"
45+
}
46+
47+
github := pkg.GitHubRepo
48+
if github == "" {
49+
github = "-"
50+
}
51+
52+
latestTag := pkg.LatestTag
53+
if latestTag == "" {
54+
latestTag = "(no tags)"
55+
}
56+
57+
if mode == Verbose {
58+
table.Append(fmt.Sprintf("%d", i+1), pkg.ModulePath, pkg.PackageName, goVersion, github, latestTag)
59+
} else {
60+
table.Append(fmt.Sprintf("%d", i+1), pkg.PackageName, latestTag)
61+
}
62+
}
63+
64+
table.Render()
65+
}
66+
67+
// ShowPackageListWithHeader displays a list of packages with a header
68+
func ShowPackageListWithHeader(packages []discovery.Package, mode DisplayMode, searchPaths []string) {
69+
color.Cyan("Discovered %d Go packages:", len(packages))
70+
color.White("Search paths: %s", strings.Join(searchPaths, ", "))
71+
color.White("")
72+
73+
ShowPackageList(packages, mode)
74+
}

pkg/interactive/interactive.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,7 @@ func SelectPackage(packages []discovery.Package) (*discovery.Package, error) {
142142
return nil, fmt.Errorf("no packages found")
143143
}
144144

145-
color.Cyan("\nAvailable packages:")
146-
for i, pkg := range packages {
147-
color.White("%d. %s (%s)", i+1, pkg.ModulePath, pkg.PackageName)
148-
}
149-
145+
// No need to display packages again - they're already shown in the table
150146
selection, err := selectOption(1, len(packages))
151147
if err != nil {
152148
return nil, err

pkg/tagutils/tagutils.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,16 @@ func ExtractPackageNameFromModule(modulePath string) string {
116116

117117
// ValidateTagFormat validates a tag format string
118118
func ValidateTagFormat(format string) error {
119-
// Check for required placeholders
120-
required := []string{"{major}", "{minor}", "{patch}"}
121-
for _, placeholder := range required {
122-
if !strings.Contains(format, placeholder) {
123-
return fmt.Errorf("tag format must contain %s placeholder", placeholder)
119+
// Check if format contains {version} (which is valid on its own)
120+
if strings.Contains(format, "{version}") {
121+
// {version} is valid on its own, no other placeholders required
122+
} else {
123+
// If not using {version}, check for required individual placeholders
124+
required := []string{"{major}", "{minor}", "{patch}"}
125+
for _, placeholder := range required {
126+
if !strings.Contains(format, placeholder) {
127+
return fmt.Errorf("tag format must contain %s placeholder", placeholder)
128+
}
124129
}
125130
}
126131

0 commit comments

Comments
 (0)