From 48254730abd94372721561f246186b717b38c06e Mon Sep 17 00:00:00 2001 From: Osinachi Chukwujama Date: Sun, 25 Jun 2023 12:27:40 +0100 Subject: [PATCH 1/2] chore: add default editor handling --- utils.go | 61 +++++++++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/utils.go b/utils.go index 6c02309..b20b73e 100644 --- a/utils.go +++ b/utils.go @@ -126,55 +126,44 @@ func moveFile(src, dest string) { } func openFile(filename string) error { - if err := verifyCodeCommand(); err != nil { - return err - } - var cmd *exec.Cmd if runtime.GOOS == "windows" { // On Windows, use "code.cmd" cmd = exec.Command("code.cmd", filename) - } else { - cmd = exec.Command("code", filename) - } - - err := cmd.Start() - if err != nil { - return err - } - - return nil -} + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() -func verifyCodeCommand() error { - _, err := exec.LookPath("code") - if err != nil { - fmt.Println("code command not found. Attempting to install...") - err = installCodeCommand() if err != nil { - return fmt.Errorf("failed to verify and install code command: %s", err) + cmd = exec.Command("notepad", filename) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + runErr := cmd.Run() + + if runErr != nil { + fmt.Println(runErr) + } } - fmt.Println("code command installed successfully.") - } - return nil -} -func installCodeCommand() error { - var command string - var args []string - if runtime.GOOS == "windows" { - command = "powershell" - args = []string{"-Command", "& { Invoke-WebRequest -Uri 'https://go.microsoft.com/fwlink/?LinkID=760868' -OutFile 'vscode.zip' }"} } else { - command = "bash" - args = []string{"-c", "curl -o vscode.zip -L https://go.microsoft.com/fwlink/?LinkID=760868 && unzip vscode.zip && sudo mv 'VSCode.app' '/usr/local/bin/code'"} + // implement such that the default editor is opened + cmd = exec.Command("editor", filename) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + runErr := cmd.Run() + + if runErr != nil { + fmt.Println(runErr) + } } - cmd := exec.Command(command, args...) - err := cmd.Run() + err := cmd.Start() if err != nil { - return fmt.Errorf("failed to install code command: %s", err) + return err } return nil From 2e37aeefeee963387d46999519a31f4eb902a9fc Mon Sep 17 00:00:00 2001 From: Osinachi Chukwujama Date: Sun, 25 Jun 2023 12:32:59 +0100 Subject: [PATCH 2/2] remove duplicate code --- utils.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/utils.go b/utils.go index b20b73e..8bf0ac8 100644 --- a/utils.go +++ b/utils.go @@ -131,16 +131,12 @@ func openFile(filename string) error { if runtime.GOOS == "windows" { // On Windows, use "code.cmd" cmd = exec.Command("code.cmd", filename) - cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + handleCmdOs(cmd) err := cmd.Run() if err != nil { cmd = exec.Command("notepad", filename) - cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + handleCmdOs(cmd) runErr := cmd.Run() if runErr != nil { @@ -151,9 +147,7 @@ func openFile(filename string) error { } else { // implement such that the default editor is opened cmd = exec.Command("editor", filename) - cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + handleCmdOs(cmd) runErr := cmd.Run() if runErr != nil { @@ -168,3 +162,9 @@ func openFile(filename string) error { return nil } + +func handleCmdOs(cmd *exec.Cmd) { + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr +}