Skip to content

Commit 176584c

Browse files
AnnatarHeclaude
andcommitted
refactor(dotfiles): improve app handling with typed names and dynamic filtering
- Add DotfileAppName type with enum-like constants for all available apps - Create GetAllAppsMap() function to centralize app handler initialization - Update dotfiles_pull.go to support filtering by specific apps parameter - Use typed app names instead of string literals for better type safety - Improve code maintainability and reduce duplication in app management 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5fae10e commit 176584c

2 files changed

Lines changed: 70 additions & 16 deletions

File tree

commands/dotfiles_pull.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,21 @@ func pullDotfiles(c *cli.Context) error {
5656
return nil
5757
}
5858

59-
// Initialize all available app handlers
60-
allApps := map[string]model.DotfileApp{
61-
"nvim": model.NewNvimApp(),
62-
"fish": model.NewFishApp(),
63-
"git": model.NewGitApp(),
64-
"zsh": model.NewZshApp(),
65-
"bash": model.NewBashApp(),
66-
"ghostty": model.NewGhosttyApp(),
67-
"claude": model.NewClaudeApp(),
68-
"starship": model.NewStarshipApp(),
69-
"npm": model.NewNpmApp(),
70-
"ssh": model.NewSshApp(),
71-
"kitty": model.NewKittyApp(),
72-
"kubernetes": model.NewKubernetesApp(),
59+
// Initialize app handlers based on apps parameter
60+
var allApps map[model.DotfileAppName]model.DotfileApp
61+
if len(apps) == 0 {
62+
// If no specific apps specified, use all available apps
63+
allApps = model.GetAllAppsMap()
64+
} else {
65+
// Only include specified apps
66+
allAppsMap := model.GetAllAppsMap()
67+
allApps = make(map[model.DotfileAppName]model.DotfileApp)
68+
for _, appNameStr := range apps {
69+
appName := model.DotfileAppName(appNameStr)
70+
if appHandler, exists := allAppsMap[appName]; exists {
71+
allApps[appName] = appHandler
72+
}
73+
}
7374
}
7475

7576
// Process fetched dotfiles
@@ -78,7 +79,8 @@ func pullDotfiles(c *cli.Context) error {
7879
totalFailed := 0
7980

8081
for _, appData := range resp.Data.FetchUser.Dotfiles.Apps {
81-
app, exists := allApps[appData.App]
82+
appName := model.DotfileAppName(appData.App)
83+
app, exists := allApps[appName]
8284
if !exists {
8385
logrus.Warnf("Unknown app type: %s", appData.App)
8486
continue

model/dotfile_apps.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,58 @@ import (
1313
"github.com/sirupsen/logrus"
1414
)
1515

16+
type DotfileAppName string
17+
18+
// Available app names as enum-like constants
19+
const (
20+
AppNvim DotfileAppName = "nvim"
21+
AppFish DotfileAppName = "fish"
22+
AppGit DotfileAppName = "git"
23+
AppZsh DotfileAppName = "zsh"
24+
AppBash DotfileAppName = "bash"
25+
AppGhostty DotfileAppName = "ghostty"
26+
AppClaude DotfileAppName = "claude"
27+
AppStarship DotfileAppName = "starship"
28+
AppNpm DotfileAppName = "npm"
29+
AppSsh DotfileAppName = "ssh"
30+
AppKitty DotfileAppName = "kitty"
31+
AppKubernetes DotfileAppName = "kubernetes"
32+
)
33+
34+
// AllAvailableApps contains all available app names
35+
var AllAvailableApps = []DotfileAppName{
36+
AppNvim,
37+
AppFish,
38+
AppGit,
39+
AppZsh,
40+
AppBash,
41+
AppGhostty,
42+
AppClaude,
43+
AppStarship,
44+
AppNpm,
45+
AppSsh,
46+
AppKitty,
47+
AppKubernetes,
48+
}
49+
50+
// GetAllAppsMap returns a map of all available app handlers
51+
func GetAllAppsMap() map[DotfileAppName]DotfileApp {
52+
return map[DotfileAppName]DotfileApp{
53+
AppNvim: NewNvimApp(),
54+
AppFish: NewFishApp(),
55+
AppGit: NewGitApp(),
56+
AppZsh: NewZshApp(),
57+
AppBash: NewBashApp(),
58+
AppGhostty: NewGhosttyApp(),
59+
AppClaude: NewClaudeApp(),
60+
AppStarship: NewStarshipApp(),
61+
AppNpm: NewNpmApp(),
62+
AppSsh: NewSshApp(),
63+
AppKitty: NewKittyApp(),
64+
AppKubernetes: NewKubernetesApp(),
65+
}
66+
}
67+
1668
// DotfileApp interface defines methods for handling app-specific dotfiles
1769
type DotfileApp interface {
1870
Name() string
@@ -239,7 +291,7 @@ func (b *BaseApp) Save(ctx context.Context, files map[string]string) error {
239291
// Create patches from the diffs and apply them to get merged content
240292
patches := dmp.PatchMake(existingContent, diffs)
241293
mergedContent, results := dmp.PatchApply(patches, existingContent)
242-
294+
243295
// Check if patches were applied successfully
244296
for i, success := range results {
245297
if !success {

0 commit comments

Comments
 (0)