Skip to content

Commit

Permalink
Merge pull request #1 from vicradon/main
Browse files Browse the repository at this point in the history
chore: add default editor handling
  • Loading branch information
geoffrey1330 authored Jun 25, 2023
2 parents 642f9b4 + 2e37aee commit c5e817b
Showing 1 changed file with 25 additions and 36 deletions.
61 changes: 25 additions & 36 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,56 +126,45 @@ 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
}
handleCmdOs(cmd)
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)
handleCmdOs(cmd)
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)
handleCmdOs(cmd)
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
}

func handleCmdOs(cmd *exec.Cmd) {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}

0 comments on commit c5e817b

Please sign in to comment.