-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: standardize config/client creation pattern in cfl #17
Copy link
Copy link
Closed
Description
Summary
Refactor cfl to use the Options.APIClient() pattern that jtk uses, eliminating repeated config loading code.
Current State
The same 15-line config/client creation block is copy-pasted in 8+ locations:
tools/cfl/internal/cmd/page/list.go(lines 94-110)tools/cfl/internal/cmd/page/create.go(lines 113-130)tools/cfl/internal/cmd/page/view.go(lines 81-93)tools/cfl/internal/cmd/page/delete.go(lines 53-64)tools/cfl/internal/cmd/search/search.go(lines 133-150)tools/cfl/internal/cmd/space/list.go(lines 76-88)- And more...
// Current pattern (repeated everywhere)
if client == nil {
cfg, err := config.LoadWithEnv(config.DefaultConfigPath())
if err != nil {
return fmt.Errorf("failed to load config: %w (run 'cfl init' to configure)", err)
}
if err := cfg.Validate(); err != nil {
return fmt.Errorf("invalid config: %w (run 'cfl init' to configure)", err)
}
client = api.NewClient(cfg.URL, cfg.Email, cfg.APIToken)
}Proposed Changes
Follow jtk's pattern from tools/jtk/internal/cmd/root/root.go:
func (o *Options) APIClient() (*api.Client, error) {
if o.testClient != nil {
return o.testClient, nil
}
return api.New(api.ClientConfig{...})
}- Create
Optionsstruct in cfl's root command - Add
APIClient()method with lazy initialization - Pass
*Optionsto all subcommands viaRegister(parent, opts)pattern - Remove duplicate config loading from each command
Impact
~100 lines removed, 8 files changed
Additional Benefit
This also improves testability - tests can inject mock clients via testClient field.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels