Skip to content

Commit dc535bc

Browse files
nficanoclaude
andcommitted
fix: handle wrapped errors in config-not-found check and create parent dirs
loadOrDefaultConfig used os.IsNotExist which cannot unwrap errors wrapped by fmt.Errorf. Replaced with errors.Is(err, fs.ErrNotExist). Also added os.MkdirAll in saveConfig so first-time registration works without manually creating the config directory. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 7cdf6d2 commit dc535bc

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

internal/cli/register.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package cli
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
7+
"io/fs"
68
"log/slog"
79
"net/http"
810
"os"
11+
"path/filepath"
912

1013
"github.com/BurntSushi/toml"
1114
"github.com/spf13/cobra"
@@ -132,7 +135,7 @@ func runRegister(cmd *cobra.Command, _ []string) error {
132135
func loadOrDefaultConfig(path string) (*config.Config, error) {
133136
cfg, err := config.Load(path)
134137
if err != nil {
135-
if os.IsNotExist(err) {
138+
if errors.Is(err, fs.ErrNotExist) {
136139
return config.DefaultConfig(), nil
137140
}
138141
return nil, fmt.Errorf("loading config from %s: %w", path, err)
@@ -141,7 +144,13 @@ func loadOrDefaultConfig(path string) (*config.Config, error) {
141144
}
142145

143146
// saveConfig writes the config to the given path in TOML format.
147+
// It creates parent directories if they do not exist.
144148
func saveConfig(path string, cfg *config.Config) error {
149+
dir := filepath.Dir(path)
150+
if err := os.MkdirAll(dir, 0o755); err != nil {
151+
return fmt.Errorf("creating config directory %s: %w", dir, err)
152+
}
153+
145154
f, err := os.Create(path)
146155
if err != nil {
147156
return fmt.Errorf("creating config file %s: %w", path, err)

0 commit comments

Comments
 (0)