Skip to content

Commit

Permalink
chore(grum): execx pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
jaronnie committed May 22, 2024
1 parent acf9949 commit 0bcf7a7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 53 deletions.
56 changes: 4 additions & 52 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ package cmd

import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/jaronnie/grum/pkg/execx"

"github.com/go-git/go-git/v5"
"github.com/spf13/cobra"
)
Expand All @@ -27,15 +26,15 @@ var cloneCmd = &cobra.Command{
RunE: clone,
}

func clone(cmd *cobra.Command, args []string) error {
func clone(_ *cobra.Command, args []string) error {
url := args[0]

s, err := getNewRemoteUrl(url)
if err != nil {
return err
}
command := fmt.Sprintf("git clone %s", s)
err = RunCommand(command)
err = execx.Run(command)
cobra.CheckErr(err)

ep, err := Endpoint(url)
Expand Down Expand Up @@ -68,50 +67,3 @@ func clone(cmd *cobra.Command, args []string) error {
func init() {
rootCmd.AddCommand(cloneCmd)
}

func RunCommand(arg string) error {
goos := runtime.GOOS
var cmd *exec.Cmd
switch goos {
case "darwin", "linux":
cmd = exec.Command("sh", "-c", arg)
case "windows":
cmd = exec.Command("cmd.exe", "/c", arg)
default:
return fmt.Errorf("unexpected os: %v", goos)
}

stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}

stderr, err := cmd.StderrPipe()
if err != nil {
return err
}

pwd, err := os.Getwd()
if err != nil {
return err
}
cmd.Dir = pwd

if err := cmd.Start(); err != nil {
return err
}

go func() {
_, _ = io.Copy(os.Stdout, stdout)
}()

go func() {
_, _ = io.Copy(os.Stderr, stderr)
}()

if err := cmd.Wait(); err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var rootCmd = &cobra.Command{
Run: run,
}

func run(cmd *cobra.Command, args []string) {
func run(*cobra.Command, []string) {
pwd, _ := os.Getwd()
repo, err := git.PlainOpen(pwd)
cobra.CheckErr(err)
Expand Down
56 changes: 56 additions & 0 deletions pkg/execx/execx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package execx

import (
"fmt"
"io"
"os"
"os/exec"
"runtime"
)

func Run(arg string) error {
goos := runtime.GOOS
var cmd *exec.Cmd
switch goos {
case "darwin", "linux":
cmd = exec.Command("sh", "-c", arg)
case "windows":
cmd = exec.Command("cmd.exe", "/c", arg)
default:
return fmt.Errorf("unexpected os: %v", goos)
}

stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}

stderr, err := cmd.StderrPipe()
if err != nil {
return err
}

pwd, err := os.Getwd()
if err != nil {
return err
}
cmd.Dir = pwd

if err := cmd.Start(); err != nil {
return err
}

go func() {
_, _ = io.Copy(os.Stdout, stdout)
}()

go func() {
_, _ = io.Copy(os.Stderr, stderr)
}()

if err := cmd.Wait(); err != nil {
return err
}

return nil
}

0 comments on commit 0bcf7a7

Please sign in to comment.