Skip to content

Commit

Permalink
Merge pull request #66 from asdf-vm/tb/shim-exec-2
Browse files Browse the repository at this point in the history
feat(golang-rewrite): create `asdf exec` command
  • Loading branch information
Stratus3D authored Sep 14, 2024
2 parents 9f78c9d + 10a6fec commit d03e78e
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"asdf/internal/config"
"asdf/internal/exec"
"asdf/internal/info"
"asdf/internal/installs"
"asdf/internal/plugins"
Expand Down Expand Up @@ -44,6 +45,15 @@ func Execute(version string) {
Usage: "The multiple runtime version manager",
UsageText: usageText,
Commands: []*cli.Command{
{
Name: "exec",
Action: func(cCtx *cli.Context) error {
command := cCtx.Args().Get(0)
args := cCtx.Args().Slice()

return execCommand(logger, command, args)
},
},
{
Name: "info",
Action: func(_ *cli.Context) error {
Expand Down Expand Up @@ -159,6 +169,43 @@ func Execute(version string) {
}
}

func execCommand(logger *log.Logger, command string, args []string) error {
if command == "" {
logger.Printf("no command specified")
return fmt.Errorf("no command specified")
}

conf, err := config.LoadConfig()
if err != nil {
logger.Printf("error loading config: %s", err)
return err
}

currentDir, err := os.Getwd()
if err != nil {
logger.Printf("unable to get current directory: %s", err)
return err
}

executable, found, err := shims.FindExecutable(conf, command, currentDir)
if err != nil {
logger.Printf("executable not found due to reason: %s", err.Error())
return err
}

if !found {
logger.Print("executable not found")
return fmt.Errorf("executable not found")
}
if len(args) > 1 {
args = args[1:]
} else {
args = []string{}
}

return exec.Exec(executable, args, os.Environ())
}

func pluginAddCommand(_ *cli.Context, conf config.Config, logger *log.Logger, pluginName, pluginRepo string) error {
if pluginName == "" {
// Invalid arguments
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/go-git/go-git/v5 v5.11.0
github.com/mitchellh/go-homedir v1.1.0
github.com/otiai10/copy v1.14.0
github.com/rogpeppe/go-internal v1.11.0
github.com/sethvargo/go-envconfig v1.0.0
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.27.1
Expand Down
12 changes: 12 additions & 0 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Package exec handles replacing the asdf go process with
package exec

import (
"syscall"
)

// Exec invokes syscall.Exec to exec an executable. Requires an absolute path to
// executable.
func Exec(executablePath string, args []string, env []string) error {
return syscall.Exec(executablePath, append([]string{executablePath}, args...), env)
}
34 changes: 34 additions & 0 deletions internal/exec/exec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package exec

import (
"fmt"
"os"
"os/exec"
"testing"

"github.com/rogpeppe/go-internal/testscript"
)

func execit() int {
// Exec only works with absolute path
cmdPath, _ := exec.LookPath(os.Args[1])
err := Exec(cmdPath, os.Args[2:], os.Environ())

if err != nil {
fmt.Printf("Err: %#+v\n", err.Error())
}

return 0
}

func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"execit": execit,
}))
}

func TestExec(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/script",
})
}
3 changes: 3 additions & 0 deletions internal/exec/testdata/script/exec-env.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
env ENV=foo
execit echo this is a $ENV
stdout 'this is a foo\n'
2 changes: 2 additions & 0 deletions internal/exec/testdata/script/exec-simple.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
execit echo this is a test
stdout 'this is a test\n'

0 comments on commit d03e78e

Please sign in to comment.