Skip to content

Commit 26c5b97

Browse files
committed
refactor: use samber/lo wherever possible
1 parent 0346ec0 commit 26c5b97

25 files changed

Lines changed: 252 additions & 285 deletions

appconfig/appconfig.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/chenasraf/sofmani/platform"
1010
"github.com/chenasraf/sofmani/utils"
1111
"github.com/eschao/config"
12+
"github.com/samber/lo"
1213
"gopkg.in/yaml.v3"
1314
)
1415

@@ -76,10 +77,7 @@ type AppConfigDefaults struct {
7677

7778
// GetCategoryDisplay returns the effective category display mode, defaulting to "border".
7879
func (c *AppConfig) GetCategoryDisplay() CategoryDisplayMode {
79-
if c.CategoryDisplay != nil {
80-
return *c.CategoryDisplay
81-
}
82-
return CategoryDisplayBorder
80+
return lo.FromPtrOr(c.CategoryDisplay, CategoryDisplayBorder)
8381
}
8482

8583
// Environ returns the combined environment variables as a slice of strings.
@@ -172,21 +170,9 @@ func tryConfigDir(dir string) string {
172170
// GetConfigDesc returns a string slice describing the current configuration.
173171
func (c *AppConfig) GetConfigDesc() []string {
174172
desc := []string{}
175-
isDebug := false
176-
if c.Debug != nil {
177-
isDebug = *c.Debug
178-
}
179-
checkUpdates := false
180-
if c.CheckUpdates != nil {
181-
checkUpdates = *c.CheckUpdates
182-
}
183-
showSummary := true // default is enabled
184-
if c.Summary != nil {
185-
showSummary = *c.Summary
186-
}
187-
desc = append(desc, fmt.Sprintf("Debug: %t", isDebug))
188-
desc = append(desc, fmt.Sprintf("CheckUpdates: %t", checkUpdates))
189-
desc = append(desc, fmt.Sprintf("Summary: %t", showSummary))
173+
desc = append(desc, fmt.Sprintf("Debug: %t", lo.FromPtrOr(c.Debug, false)))
174+
desc = append(desc, fmt.Sprintf("CheckUpdates: %t", lo.FromPtrOr(c.CheckUpdates, false)))
175+
desc = append(desc, fmt.Sprintf("Summary: %t", lo.FromPtrOr(c.Summary, true)))
190176

191177
if c.Env != nil {
192178
desc = append(desc, "Environment Variables:")

appconfig/appconfig_test.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/chenasraf/sofmani/platform"
10+
"github.com/samber/lo"
1011
"github.com/stretchr/testify/assert"
1112
)
1213

@@ -16,18 +17,18 @@ func TestPlatformMapResolve(t *testing.T) {
1617
platform string
1718
expected *string
1819
}{
19-
{"MacOS", "darwin", strPtr("macos")},
20-
{"Linux", "linux", strPtr("linux")},
21-
{"Windows", "windows", strPtr("windows")},
20+
{"MacOS", "darwin", lo.ToPtr("macos")},
21+
{"Linux", "linux", lo.ToPtr("linux")},
22+
{"Windows", "windows", lo.ToPtr("windows")},
2223
}
2324

2425
for _, tt := range tests {
2526
t.Run(tt.name, func(t *testing.T) {
2627
platform.SetOS(tt.platform)
2728
pm := platform.PlatformMap[string]{
28-
MacOS: strPtr("macos"),
29-
Linux: strPtr("linux"),
30-
Windows: strPtr("windows"),
29+
MacOS: lo.ToPtr("macos"),
30+
Linux: lo.ToPtr("linux"),
31+
Windows: lo.ToPtr("windows"),
3132
}
3233
assert.Equal(t, tt.expected, pm.Resolve())
3334
})
@@ -135,7 +136,3 @@ func TestFindConfigFile(t *testing.T) {
135136
assert.NoError(t, os.Chdir(dir))
136137
assert.True(t, strings.HasSuffix(FindConfigFile(), file))
137138
}
138-
139-
func strPtr(s string) *string {
140-
return &s
141-
}

cmd/root.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/chenasraf/sofmani/appconfig"
99
"github.com/chenasraf/sofmani/logger"
1010
"github.com/chenasraf/sofmani/machine"
11+
"github.com/samber/lo"
1112
"github.com/spf13/cobra"
1213
)
1314

@@ -153,26 +154,26 @@ func buildCliConfig(cmd *cobra.Command, args []string) *appconfig.AppCliConfig {
153154

154155
// Handle debug flag
155156
if cmd.Flags().Changed("debug") {
156-
config.Debug = boolPtr(true)
157+
config.Debug = lo.ToPtr(true)
157158
}
158159
if cmd.Flags().Changed("no-debug") {
159-
config.Debug = boolPtr(false)
160+
config.Debug = lo.ToPtr(false)
160161
}
161162

162163
// Handle update flag
163164
if cmd.Flags().Changed("update") {
164-
config.CheckUpdates = boolPtr(true)
165+
config.CheckUpdates = lo.ToPtr(true)
165166
}
166167
if cmd.Flags().Changed("no-update") {
167-
config.CheckUpdates = boolPtr(false)
168+
config.CheckUpdates = lo.ToPtr(false)
168169
}
169170

170171
// Handle summary flag
171172
if cmd.Flags().Changed("summary") {
172-
config.Summary = boolPtr(true)
173+
config.Summary = lo.ToPtr(true)
173174
}
174175
if cmd.Flags().Changed("no-summary") {
175-
config.Summary = boolPtr(false)
176+
config.Summary = lo.ToPtr(false)
176177
}
177178

178179
// Handle log file flag
@@ -201,10 +202,5 @@ func buildCliConfig(cmd *cobra.Command, args []string) *appconfig.AppCliConfig {
201202
return config
202203
}
203204

204-
// boolPtr returns a pointer to a boolean value.
205-
func boolPtr(b bool) *bool {
206-
return &b
207-
}
208-
209205
// RunMain is set by main.go to run the main application logic.
210206
var RunMain func(cliConfig *appconfig.AppCliConfig)

installer/apt_installer_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/chenasraf/sofmani/appconfig"
77
"github.com/chenasraf/sofmani/logger"
8+
"github.com/samber/lo"
89
)
910

1011
func newAptInstaller(data *appconfig.InstallerData) *AptInstaller {
@@ -21,7 +22,7 @@ func TestAptValidation(t *testing.T) {
2122
logger.InitLogger(false)
2223
aptInstaller := newAptInstaller(
2324
&appconfig.InstallerData{
24-
Name: strPtr("test-apt"),
25+
Name: lo.ToPtr("test-apt"),
2526
Type: appconfig.InstallerTypeApt,
2627
},
2728
)
@@ -33,7 +34,7 @@ func TestAptGetOpts(t *testing.T) {
3334

3435
// Test default opts (no options set)
3536
defaultData := &appconfig.InstallerData{
36-
Name: strPtr("vim"),
37+
Name: lo.ToPtr("vim"),
3738
Type: appconfig.InstallerTypeApt,
3839
}
3940
installer := newAptInstaller(defaultData)
@@ -50,7 +51,7 @@ func TestAptGetOpts(t *testing.T) {
5051

5152
// Test with flags option
5253
flagsData := &appconfig.InstallerData{
53-
Name: strPtr("vim"),
54+
Name: lo.ToPtr("vim"),
5455
Type: appconfig.InstallerTypeApt,
5556
Opts: &map[string]any{
5657
"flags": "-y --no-install-recommends",
@@ -64,7 +65,7 @@ func TestAptGetOpts(t *testing.T) {
6465

6566
// Test with install_flags option
6667
installFlagsData := &appconfig.InstallerData{
67-
Name: strPtr("vim"),
68+
Name: lo.ToPtr("vim"),
6869
Type: appconfig.InstallerTypeApt,
6970
Opts: &map[string]any{
7071
"install_flags": "--no-install-recommends",
@@ -78,7 +79,7 @@ func TestAptGetOpts(t *testing.T) {
7879

7980
// Test with update_flags option
8081
updateFlagsData := &appconfig.InstallerData{
81-
Name: strPtr("vim"),
82+
Name: lo.ToPtr("vim"),
8283
Type: appconfig.InstallerTypeApt,
8384
Opts: &map[string]any{
8485
"update_flags": "--only-upgrade",
@@ -92,7 +93,7 @@ func TestAptGetOpts(t *testing.T) {
9293

9394
// Test with all flags options combined
9495
allFlagsData := &appconfig.InstallerData{
95-
Name: strPtr("vim"),
96+
Name: lo.ToPtr("vim"),
9697
Type: appconfig.InstallerTypeApt,
9798
Opts: &map[string]any{
9899
"flags": "--common",

0 commit comments

Comments
 (0)