Skip to content

Commit 5c206b2

Browse files
drnclaude
andauthored
Polish codebase: fix panics, check errors, reduce duplication
Clean code analysis and refactoring across 31 files: - Add bounds checking to prevent panics on unexpected system command output - Check ignored errors from json.Unmarshal, os.WriteFile, cobra Execute() - Extract shared utilities: pkg/jsonutil.Print, pkg/run.CaptureClean - Unify component registry into single source of truth - Consolidate duplicate code in link, spotify auth, and slack packages - Replace os.Exit in cache.Log with bool return for testability - Remove unused types, fix byte-vs-rune slicing, use const where appropriate Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7013ad8 commit 5c206b2

File tree

32 files changed

+236
-217
lines changed

32 files changed

+236
-217
lines changed

cli/commands/docker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var cmdDocker = &cobra.Command{
1111
Aliases: []string{"dock"},
1212
Short: "Docker command aliases",
1313
Run: func(cmd *cobra.Command, _ []string) {
14-
cmd.Help()
14+
_ = cmd.Help()
1515
},
1616
}
1717

cli/commands/install.go

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,21 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13-
type component struct {
14-
Name string
15-
Description string
16-
Alias string
17-
}
18-
19-
var components = []component{
20-
{"bin", "Installs ~/bin/* commands", ""},
21-
{"git", "Installs git extensions", ""},
22-
{"home", "Installs ~/.* config files", ""},
23-
{"zsh", "Installs zsh config files", ""},
24-
{"fonts", "Installs fonts", ""},
25-
{"homebrew", "Installs Homebrew dependencies", "brew"},
26-
{"npm", "Installs npm packages", ""},
27-
{"languages", "Installs asdf & languages", ""},
28-
{"vim", "Installs vim config", ""},
29-
{"hammerspoon", "Installs hammerspoon configuration", "hs"},
30-
{"osx", "Installs OSX configuration", ""},
31-
{"agents", "Installs agent skills (Claude Code + Codex)", ""},
32-
}
33-
34-
func componentNames() []string {
35-
names := make([]string, len(components))
36-
for i, c := range components {
37-
names[i] = c.Name
38-
}
39-
return names
40-
}
41-
4213
var cmdInstall = &cobra.Command{
4314
Use: "install",
4415
Short: "Installs configuration",
4516
Run: func(cmd *cobra.Command, args []string) {
4617
if len(args) > 0 {
47-
cmd.Help()
18+
_ = cmd.Help()
4819
os.Exit(1)
4920
}
5021

51-
items := append([]string{"all"}, componentNames()...)
22+
components := install.Components()
23+
names := make([]string, len(components))
24+
for i, c := range components {
25+
names[i] = c.Name
26+
}
27+
items := append([]string{"all"}, names...)
5228

5329
prompt := promptui.Select{
5430
Label: "Select component to install",
@@ -79,7 +55,7 @@ func init() {
7955
},
8056
)
8157

82-
for _, c := range components {
58+
for _, c := range install.Components() {
8359
var aliases []string
8460
if c.Alias != "" {
8561
aliases = []string{c.Alias}
@@ -105,7 +81,7 @@ func installAll() {
10581
os.Exit(1)
10682
}
10783

108-
for _, name := range componentNames() {
109-
install.Call(name)
84+
for _, c := range install.Components() {
85+
install.Call(c.Name)
11086
}
11187
}

cli/commands/install/root.go

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,40 @@ import (
77
"github.com/drn/dots/pkg/run"
88
)
99

10+
// Component describes an installable component
11+
type Component struct {
12+
Name string
13+
Description string
14+
Alias string
15+
Fn func()
16+
}
17+
18+
// Components returns the ordered list of installable components.
19+
// This is the single source of truth for the component registry.
20+
func Components() []Component {
21+
return []Component{
22+
{"bin", "Installs ~/bin/* commands", "", Bin},
23+
{"git", "Installs git extensions", "", Git},
24+
{"home", "Installs ~/.* config files", "", Home},
25+
{"zsh", "Installs zsh config files", "", Zsh},
26+
{"fonts", "Installs fonts", "", Fonts},
27+
{"homebrew", "Installs Homebrew dependencies", "brew", Homebrew},
28+
{"npm", "Installs npm packages", "", Npm},
29+
{"languages", "Installs asdf & languages", "", Languages},
30+
{"vim", "Installs vim config", "", Vim},
31+
{"hammerspoon", "Installs hammerspoon configuration", "hs", Hammerspoon},
32+
{"osx", "Installs OSX configuration", "", Osx},
33+
{"agents", "Installs agent skills (Claude Code + Codex)", "", Agents},
34+
}
35+
}
36+
1037
// Call - Call install command by name
1138
func Call(command string) {
12-
installers := map[string]func(){
13-
"agents": Agents,
14-
"bin": Bin,
15-
"fonts": Fonts,
16-
"git": Git,
17-
"hammerspoon": Hammerspoon,
18-
"home": Home,
19-
"homebrew": Homebrew,
20-
"languages": Languages,
21-
"npm": Npm,
22-
"osx": Osx,
23-
"vim": Vim,
24-
"zsh": Zsh,
25-
}
26-
if fn, ok := installers[command]; ok {
27-
fn()
39+
for _, c := range Components() {
40+
if c.Name == command {
41+
c.Fn()
42+
return
43+
}
2844
}
2945
}
3046

cli/commands/install/vim.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func vimUpdatePlug() {
6767
func vimUpdatePlugins() {
6868
log.Info("Updating vim plugins:")
6969
tempPath := "/tmp/vim-update-result"
70-
os.Remove(tempPath)
70+
_ = os.Remove(tempPath)
7171
run.Silent(
7272
"nvim -c \"%s\"",
7373
strings.Join(

cli/commands/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var root = &cobra.Command{
1212
Use: "dots",
1313
Short: "The dots CLI manages your development environment dependencies",
1414
Run: func(cmd *cobra.Command, _ []string) {
15-
cmd.Help()
15+
_ = cmd.Help()
1616
},
1717
}
1818

cli/commands/spinner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var cmdSpinner = &cobra.Command{
1414
Use: "spinner",
1515
Short: "Runs simple CLI spinners",
1616
Run: func(cmd *cobra.Command, _ []string) {
17-
cmd.Help()
17+
_ = cmd.Help()
1818
},
1919
}
2020

cli/config/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ func save(cfg *ini.File) {
132132
log.Warning("Failed to create config directory: %s", err.Error())
133133
return
134134
}
135-
cfg.SaveTo(configPath())
135+
if err := cfg.SaveTo(configPath()); err != nil {
136+
log.Warning("Failed to save config: %s", err.Error())
137+
}
136138
}
137139

138140
func config() *ini.File {

cli/git/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ func Create(branch string) bool {
102102
}
103103

104104
// ResetHard - Hard resets to the specified address
105-
func ResetHard(address string) {
106-
run.Verbose("git reset --hard %s", address)
105+
func ResetHard(address string) bool {
106+
return run.Verbose("git reset --hard %s", address) == nil
107107
}
108108

109109
// Delete - Deletes the specified branch

cli/link/root.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,18 @@ import (
1010

1111
// Soft - Creates a soft link between input from and to arguments
1212
func Soft(from string, to string) {
13-
log.Info("Soft link: '%s' -> '%s'", path.Pretty(to), path.Pretty(from))
14-
15-
removeExisting(to)
16-
17-
// create soft link
18-
err := os.Symlink(from, to)
19-
20-
// log errors
21-
if err != nil {
22-
log.Error(err.Error())
23-
}
13+
create("Soft", from, to, os.Symlink)
2414
}
2515

2616
// Hard - Creates a hard link between input from and to arguments
2717
func Hard(from string, to string) {
28-
log.Info("Hard link: '%s' -> '%s'", path.Pretty(to), path.Pretty(from))
18+
create("Hard", from, to, os.Link)
19+
}
2920

21+
func create(label, from, to string, linkFn func(string, string) error) {
22+
log.Info("%s link: '%s' -> '%s'", label, path.Pretty(to), path.Pretty(from))
3023
removeExisting(to)
31-
32-
// create hard link
33-
err := os.Link(from, to)
34-
35-
// log errors
36-
if err != nil {
24+
if err := linkFn(from, to); err != nil {
3725
log.Error(err.Error())
3826
}
3927
}

cmd/battery-percent/root.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@ import (
99
)
1010

1111
func main() {
12-
info := run.Capture("pmset -g ps")
13-
info = strings.Split(info, "\n")[1]
14-
// extract percent from pmset metadata
15-
percent := strings.Fields(info)[2]
16-
// remove trailing ;
17-
percent = percent[:len(percent)-1]
12+
lines := strings.Split(run.Capture("pmset -g ps"), "\n")
13+
if len(lines) < 2 {
14+
fmt.Println("0%")
15+
return
16+
}
17+
fields := strings.Fields(lines[1])
18+
if len(fields) < 3 {
19+
fmt.Println("0%")
20+
return
21+
}
22+
percent := fields[2]
23+
if len(percent) > 0 {
24+
percent = strings.TrimRight(percent, ";")
25+
}
1826
fmt.Println(percent)
1927
}

0 commit comments

Comments
 (0)