Add self config commands#373
Conversation
Reviewer's GuideAdds Sequence diagram for lets self config edit commandsequenceDiagram
actor User
participant RootCmd
participant SelfCmd
participant ConfigCmd
participant Util
participant OS
User->>RootCmd: lets self config edit
RootCmd->>SelfCmd: Execute self
SelfCmd->>ConfigCmd: Execute config edit
ConfigCmd->>Util: LetsUserFile(config.yaml)
Util-->>ConfigCmd: path
ConfigCmd->>OS: MkdirAll(config_dir)
OS-->>ConfigCmd: result
ConfigCmd->>Util: OpenEditor(path)
Util-->>ConfigCmd: result
ConfigCmd-->>User: Config opened in EDITOR
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- The user config filename
config.yamlis hard-coded in multiple places (both commands and tests); consider centralizing this as a constant or helper in one place to avoid divergence if the name ever changes. - The
Longhelp text ininitConfigCommandhard-codes~/.config/lets/config.yaml, while the actual path is resolved viautil.LetsUserFile; it would be more robust to derive the path from the same helper to keep the help text in sync with the real behavior.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The user config filename `config.yaml` is hard-coded in multiple places (both commands and tests); consider centralizing this as a constant or helper in one place to avoid divergence if the name ever changes.
- The `Long` help text in `initConfigCommand` hard-codes `~/.config/lets/config.yaml`, while the actual path is resolved via `util.LetsUserFile`; it would be more robust to derive the path from the same helper to keep the help text in sync with the real behavior.
## Individual Comments
### Comment 1
<location path="internal/util/editor.go" line_range="10-16" />
<code_context>
+ "os/exec"
+)
+
+func OpenEditor(path string) error {
+ editor := os.Getenv("EDITOR")
+ if editor == "" {
+ return errors.New("EDITOR is not set")
+ }
+
+ cmd := exec.Command(editor, path) //nolint:gosec
+ cmd.Stdin = os.Stdin
+ cmd.Stdout = os.Stdout
</code_context>
<issue_to_address>
**issue (bug_risk):** EDITOR values containing arguments (e.g. "vim -u ...") will not work with exec.Command as used here.
Many users configure EDITOR with flags or spaces in the path (e.g. `code --wait`, `vim -u ~/.vimrc`). `exec.Command(editor, path)` treats the entire EDITOR value as the binary name and doesn’t split arguments, so these setups will fail. Consider either parsing EDITOR into binary + args and appending `path`, or invoking via a shell (e.g. `exec.Command("sh", "-c", editor+" "+shellQuote(path))`).
</issue_to_address>
### Comment 2
<location path="internal/cmd/self_config.go" line_range="62" />
<code_context>
+ Use: "path",
+ Short: "Print lets user config path",
+ Args: cobra.NoArgs,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ path, err := util.LetsUserFile("config.yaml")
+ if err != nil {
+ return err
+ }
+
+ _, err = fmt.Fprintln(cmd.OutOrStdout(), path)
+
+ return err
+ },
+ }
+}
+
+func initConfigEditCommand(openEditor func(string) error) *cobra.Command {
+ return &cobra.Command{
+ Use: "edit",
+ Short: "Open lets user config in EDITOR",
+ Args: cobra.NoArgs,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ path, err := util.LetsUserFile("config.yaml")
+ if err != nil {
+ return err
+ }
+
+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
+ return fmt.Errorf("creating config directory: %w", err)
+ }
</code_context>
<issue_to_address>
**🚨 suggestion (security):** The config directory permissions (0755) might be too permissive for potentially sensitive user configuration.
`0o755` allows group/others to read and traverse the directory. If this config may hold tokens or credentials, defaulting to `0o700` is safer and aligns with typical per-user config directories. Please restrict permissions unless there’s a concrete need for broader access.
```suggestion
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| func OpenEditor(path string) error { | ||
| editor := os.Getenv("EDITOR") | ||
| if editor == "" { | ||
| return errors.New("EDITOR is not set") | ||
| } | ||
|
|
||
| cmd := exec.Command(editor, path) //nolint:gosec |
There was a problem hiding this comment.
issue (bug_risk): EDITOR values containing arguments (e.g. "vim -u ...") will not work with exec.Command as used here.
Many users configure EDITOR with flags or spaces in the path (e.g. code --wait, vim -u ~/.vimrc). exec.Command(editor, path) treats the entire EDITOR value as the binary name and doesn’t split arguments, so these setups will fail. Consider either parsing EDITOR into binary + args and appending path, or invoking via a shell (e.g. exec.Command("sh", "-c", editor+" "+shellQuote(path))).
Summary
lets self config pathto print the user settings pathlets self config editto open the user settings file innvimTests
go test ./...lets lintCloses #370
Summary by Sourcery
Add CLI subcommands under
lets self configto manage the per-user settings file, including printing its path and opening it in an editor.New Features:
lets self config pathto print the resolved user settings file path.lets self config editto open the user settings file in the configured editor.Enhancements:
Documentation:
lets self config pathandlets self config editcommands in the settings docs and changelog.Tests: