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) diff --git a/main.go b/main.go index efcc0e9..ad7ac36 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" ) @@ -31,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 } @@ -41,17 +42,22 @@ 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) + } } 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..396252a --- /dev/null +++ b/src/editor/local_editor_open.go @@ -0,0 +1,28 @@ +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() + 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() +}