Skip to content

Commit

Permalink
Split run function into separate files for OS builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
matrixx567 committed Jan 29, 2024
1 parent 66c2575 commit 6467d36
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
21 changes: 0 additions & 21 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ package cmd
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"strings"
"syscall"

"github.com/fatih/color"
"github.com/knqyf263/pet/config"
Expand All @@ -21,23 +17,6 @@ func editFile(command, file string) error {
return run(command, os.Stdin, os.Stdout)
}

func run(command string, r io.Reader, w io.Writer) error {
var cmd *exec.Cmd
if len(config.Conf.General.Cmd) > 0 {
line := append(config.Conf.General.Cmd, command)
cmd = exec.Command(line[0], line[1:]...)
} else if runtime.GOOS == "windows" {
cmd = exec.Command("cmd.exe")
cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf("/c \"%s\"", command)}
} else {
cmd = exec.Command("sh", "-c", command)
}
cmd.Stderr = os.Stderr
cmd.Stdout = w
cmd.Stdin = r
return cmd.Run()
}

func filter(options []string, tag string) (commands []string, err error) {
var snippets snippet.Snippets
if err := snippets.Load(); err != nil {
Expand Down
25 changes: 25 additions & 0 deletions cmd/util_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build (darwin && cgo) || linux

package cmd

import (
"io"
"os"
"os/exec"

"github.com/knqyf263/pet/config"
)

func run(command string, r io.Reader, w io.Writer) error {
var cmd *exec.Cmd
if len(config.Conf.General.Cmd) > 0 {
line := append(config.Conf.General.Cmd, command)
cmd = exec.Command(line[0], line[1:]...)
} else {
cmd = exec.Command("sh", "-c", command)
}
cmd.Stderr = os.Stderr
cmd.Stdout = w
cmd.Stdin = r
return cmd.Run()
}
28 changes: 28 additions & 0 deletions cmd/util_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build windows

package cmd

import (
"fmt"
"io"
"os"
"os/exec"
"syscall"

"github.com/knqyf263/pet/config"
)

func run(command string, r io.Reader, w io.Writer) error {
var cmd *exec.Cmd
if len(config.Conf.General.Cmd) > 0 {
line := append(config.Conf.General.Cmd, command)
cmd = exec.Command(line[0], line[1:]...)
} else {
cmd = exec.Command("cmd.exe")
cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf("/c \"%s\"", command)}
}
cmd.Stderr = os.Stderr
cmd.Stdout = w
cmd.Stdin = r
return cmd.Run()
}

0 comments on commit 6467d36

Please sign in to comment.