From 2de7e20f5c5d41ddc7664112a0cef0c14c06c23c Mon Sep 17 00:00:00 2001 From: Spencer Brower Date: Thu, 11 Jun 2026 21:25:36 -0400 Subject: [PATCH] refactor(odin): ported edit-config command. --- cli.odin | 1 + cmd_edit_config.odin | 49 ++++++++++++++++++++++++++++++++++++++++++++ main.odin | 2 ++ stubs.odin | 4 ---- 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 cmd_edit_config.odin diff --git a/cli.odin b/cli.odin index bdb95e3..f0ebe3c 100644 --- a/cli.odin +++ b/cli.odin @@ -43,6 +43,7 @@ IMPLEMENTED_COMMANDS := []string{ "add", "remove", "restore", + "edit-config", } parse_args :: proc() -> (cmd: Command, ok: bool) { diff --git a/cmd_edit_config.odin b/cmd_edit_config.odin new file mode 100644 index 0000000..f4eac47 --- /dev/null +++ b/cmd_edit_config.odin @@ -0,0 +1,49 @@ +package main + +import "core:fmt" +import "core:os" +import "core:path/filepath" +import "core:strings" + +cmd_edit_config :: proc(cmd: ^Command) { + editor := os.get_env("EDITOR", context.allocator) + if len(editor) == 0 { + fmt.println("Error: $EDITOR environment variable is not set") + return + } + + config_path, join_err := filepath.join([]string{envr_dir(), "config.json"}) + if join_err != nil { + fmt.printf("Error building config path: %v\n", join_err) + return + } + + _, stat_err := os.stat(config_path, context.allocator) + if stat_err != nil { + fmt.printf("Config file does not exist at %s. Run 'envr init' first.\n", config_path) + return + } + + args := []string{editor, config_path} + desc := os.Process_Desc{ + command = args, + stdin = os.stdin, + stdout = os.stdout, + stderr = os.stderr, + } + + p, start_err := os.process_start(desc) + if start_err != nil { + fmt.printf("Error running editor: %v\n", start_err) + return + } + + state, wait_err := os.process_wait(p) + if wait_err != nil { + fmt.printf("Error waiting for editor: %v\n", wait_err) + return + } + if state.exit_code != 0 { + os.exit(int(state.exit_code)) + } +} diff --git a/main.odin b/main.odin index 7abe876..c1f5795 100644 --- a/main.odin +++ b/main.odin @@ -29,6 +29,8 @@ main :: proc() { cmd_remove(&cmd) case "restore": cmd_restore(&cmd) + case "edit-config": + cmd_edit_config(&cmd) case: fmt.printf("Unknown command: %s\n", cmd.name) print_usage() diff --git a/stubs.odin b/stubs.odin index a3c61f3..d291573 100644 --- a/stubs.odin +++ b/stubs.odin @@ -17,7 +17,3 @@ cmd_sync :: proc(cmd: ^Command) { cmd_check :: proc(cmd: ^Command) { fmt.println("TODO: check") } - -cmd_edit_config :: proc(cmd: ^Command) { - fmt.println("TODO: edit-config") -}