From 0ea343c5e0bfaec93812f5ce8b3e72ce2fcb8065 Mon Sep 17 00:00:00 2001 From: OBrutus Date: Mon, 18 May 2026 22:57:29 +0530 Subject: [PATCH 1/4] fix: update TARGET_INSTALL_DIR to use a variable and adjust path handling for setup --- install.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/install.py b/install.py index a2b8204..94f9cbe 100755 --- a/install.py +++ b/install.py @@ -2,6 +2,8 @@ import os import sys +TARGET_INSTALL_DIR = "~/.prepare-code" +# TARGET_INSTALL_DIR = "/tmp/prepare-code" expressInstall = False ConfigFileName = "config.yaml" @@ -28,6 +30,8 @@ def clone_and_build(): os.system("rm -rf /tmp/prepare-code") tmp_cmd = "cd /tmp && git clone https://github.com/OBrutus/prepare-code.git" + # tmp_cmd = "cd /tmp && cp -r /Users/obrutus/kode/prepare-code /tmp/" + status_code = os.system(tmp_cmd) if status_code != 0: @@ -49,19 +53,21 @@ def add_to_path(): if platform == "darwin": shell_config = os.path.expanduser("~/.zshrc") with open(shell_config, "a") as f: - f.write('\nexport PATH="$HOME/.prepare-code:$PATH"\n') + f.write('\nexport PATH=' + TARGET_INSTALL_DIR + '":$PATH"\n') print(f"Added to PATH in {shell_config}. Please restart your terminal " "or run 'source {shell_config}' to apply changes.") def setup_nix(): - if not remove_dir_if_exists("~/.prepare-code", "Do you want to remove it " + if not remove_dir_if_exists(TARGET_INSTALL_DIR, "Do you want to remove it " "and continue? (Y/n)"): return None + if not clone_and_build(): return None - os.system("mkdir -p ~/.prepare-code") - os.system("mv /tmp/prepare-code/* ~/.prepare-code") + + os.system("mkdir -p "+TARGET_INSTALL_DIR) + os.system("mv /tmp/prepare-code/* " + TARGET_INSTALL_DIR) print("Setup completed to clone.") if expressInstall: choice = 'y' @@ -70,7 +76,7 @@ def setup_nix(): choice = input(choice_prompt).strip().lower() or 'y' if choice == 'y': add_to_path() - return os.path.expanduser("~/.prepare-code") + return os.path.expanduser(TARGET_INSTALL_DIR) def setup(): @@ -91,6 +97,8 @@ def setup(): def create_config_file(install_dir: str): content = "install_dir: " + install_dir + "\n" content += "bypass_prompt: " + "false" + "\n" + content += "preferred_editor: nvim \n" + content += "open_in_editor: false \n" file = open(os.path.join(install_dir, ConfigFileName), 'w') file.write(content) From 64c5848acb6e7ee3857c2f8419d0e4d029a1c5d3 Mon Sep 17 00:00:00 2001 From: OBrutus Date: Mon, 18 May 2026 22:58:51 +0530 Subject: [PATCH 2/4] feat: add support for opening files in preferred editor and update config structure --- main.go | 8 +++++++- src/config/config.go | 22 +++++++++++++++------- src/config/config.yaml | 1 + src/config/config_test.go | 4 ++-- src/editor/local_editor_open.go | 30 ++++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 src/editor/local_editor_open.go diff --git a/main.go b/main.go index efcc0e9..37c9164 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "prepare-code/src/config" + "prepare-code/src/editor" "prepare-code/src/platform" "prepare-code/src/session" ) @@ -51,7 +52,12 @@ func main() { return } - fmt.Println("File created successfully!") + fmt.Println("[DEBUG] File created successfully!") + + err = editor.TryOpenInEditor(session.FileName) + if err != nil { + fmt.Println("Err: ", err) + } } func getLanguage() string { diff --git a/src/config/config.go b/src/config/config.go index 2e41d35..1ff516e 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -15,8 +15,10 @@ var yamlFileName = "config.yaml" var config *Config type Config struct { - InstallDir string `yaml:"install_dir"` - BypassPrompt bool `yaml:"bypass_prompt"` + installDir string `yaml:"install_dir"` + bypassPrompt bool `yaml:"bypass_prompt"` + prefferedEditor string `yaml:"preferred_editor"` + openInEditor bool `yaml:"open_in_editor"` } func getOldInstallDirPath() string { @@ -29,8 +31,10 @@ func setConfig() error { configValue := GetConfigMap() config = &Config{ - InstallDir: getOldInstallDirPath(), - BypassPrompt: types.GetBoolFromString(configValue["bypass_prompt"].(string)), + installDir: getOldInstallDirPath(), + bypassPrompt: types.GetBoolFromString(configValue["bypass_prompt"].(string)), + prefferedEditor: configValue["preferred_editor"].(string), + openInEditor: types.GetBoolFromString(configValue["open_in_editor"].(string)), } return nil @@ -64,7 +68,7 @@ func GetConfigMap() map[string]interface{} { func GetInstallDir() string { if config != nil { - return config.InstallDir + return config.installDir } err := setConfig() @@ -75,7 +79,7 @@ func GetInstallDir() string { return getOldInstallDirPath() } - return config.InstallDir + return config.installDir } func CanBypassPrompt() bool { @@ -83,5 +87,9 @@ func CanBypassPrompt() bool { setConfig() } - return config.BypassPrompt + return config.bypassPrompt +} + +func GetEditor() (string, bool) { + return config.prefferedEditor, false } diff --git a/src/config/config.yaml b/src/config/config.yaml index 05f64d3..80c8d08 100644 --- a/src/config/config.yaml +++ b/src/config/config.yaml @@ -1,3 +1,4 @@ install_dir: /Users/obrutus/.prepare-code bypass_prompt: true preferred_editor: nvim +open_in_editor: false diff --git a/src/config/config_test.go b/src/config/config_test.go index eadaead..639bec0 100644 --- a/src/config/config_test.go +++ b/src/config/config_test.go @@ -12,8 +12,8 @@ func isCiEnv() bool { func Test_WithExistingConfig(t *testing.T) { // Reset global config state before test config = &Config{ - InstallDir: "/custom/install/path", - BypassPrompt: true, + installDir: "/custom/install/path", + bypassPrompt: true, } result := GetInstallDir() diff --git a/src/editor/local_editor_open.go b/src/editor/local_editor_open.go new file mode 100644 index 0000000..fd92a18 --- /dev/null +++ b/src/editor/local_editor_open.go @@ -0,0 +1,30 @@ +package editor + +import ( + "errors" + "fmt" + "os" + "os/exec" + "prepare-code/src/config" +) + +var ( + ErrOpenNotPreffered = errors.New("User preffered not to open in editor") +) + +func TryOpenInEditor(fileName string) error { + editor, toOpen := config.GetEditor() + fmt.Println(editor, " ", toOpen) + toOpen = true + if !toOpen { + return ErrOpenNotPreffered + } + + fmt.Println("Opening the file:", fileName, " in ", editor) + cmd := exec.Command(editor, fileName) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd.Run() +} From 6c39700b38e849c051ebfd1fa52739d487dcb2b6 Mon Sep 17 00:00:00 2001 From: OBrutus Date: Mon, 18 May 2026 23:01:49 +0530 Subject: [PATCH 3/4] fix: remove debug print statement and improve formatting in TryOpenInEditor --- main.go | 2 +- src/editor/local_editor_open.go | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 37c9164..7ef7b78 100644 --- a/main.go +++ b/main.go @@ -52,7 +52,7 @@ func main() { return } - fmt.Println("[DEBUG] File created successfully!") + fmt.Println("File created successfully!") err = editor.TryOpenInEditor(session.FileName) if err != nil { diff --git a/src/editor/local_editor_open.go b/src/editor/local_editor_open.go index fd92a18..396252a 100644 --- a/src/editor/local_editor_open.go +++ b/src/editor/local_editor_open.go @@ -14,13 +14,11 @@ var ( func TryOpenInEditor(fileName string) error { editor, toOpen := config.GetEditor() - fmt.Println(editor, " ", toOpen) - toOpen = true if !toOpen { return ErrOpenNotPreffered } - fmt.Println("Opening the file:", fileName, " in ", editor) + fmt.Println("Opening the file:", fileName, "in", editor) cmd := exec.Command(editor, fileName) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout From 1675956a94851548db24b40e384b2f5f44bc4ba5 Mon Sep 17 00:00:00 2001 From: OBrutus Date: Mon, 18 May 2026 23:09:21 +0530 Subject: [PATCH 4/4] fix: enhance error messages with consistent formatting in main function --- main.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 7ef7b78..ad7ac36 100644 --- a/main.go +++ b/main.go @@ -32,7 +32,7 @@ func main() { // which platform it is platform, err := platform.GetPlatform(url) if err != nil { - fmt.Println("Error while getting platform: ", err) + fmt.Println("[-] Error while getting platform: ", err) return } @@ -42,21 +42,21 @@ func main() { language := getLanguage() session, err := session.NewSession(platform, language) if err != nil { - fmt.Println("Error while creating session: ", err) + fmt.Println("[-] Error while creating session: ", err) return } err = session.CreateFile() if err != nil { - fmt.Println("Error while creating file: ", err) + fmt.Println("[-] Error while creating file: ", err) return } - fmt.Println("File created successfully!") + fmt.Println("[+] File created successfully!") err = editor.TryOpenInEditor(session.FileName) if err != nil { - fmt.Println("Err: ", err) + fmt.Println("[-] Err: ", err) } }