Skip to content

Commit

Permalink
feat: use magefile sh to execute local commands (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
aweris authored Dec 12, 2022
1 parent c2467c2 commit df44d6c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magefile/mage v1.14.0 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo=
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mesosphere/dkp-cli-runtime/core v0.7.1 h1:t4MUV6X3VMaQcx4H9//UtGBU7cA0r3l9FEq4aqdczrY=
Expand Down
21 changes: 4 additions & 17 deletions pkg/sources/asdf/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,11 @@
package asdf

import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
)

func (a *Asdf) execute(stdout, stderr io.Writer, args ...string) error {
cmd := exec.Command(filepath.Join(a.path, "bin", "entrypoint.sh"), args...)

// Set process outputs
cmd.Stdout = stdout
cmd.Stderr = stderr

// prepend the env to the existing env to allow overriding from the given env
cmd.Env = append(os.Environ(), a.env...)

fmt.Println("asdf executing: ", cmd.String())
"github.com/magefile/mage/sh"
)

return cmd.Run()
func (a *Asdf) execute(args ...string) (string, error) {
return sh.OutputWith(a.env, filepath.Join(a.path, "bin", "entrypoint.sh"), args...)
}
25 changes: 13 additions & 12 deletions pkg/sources/asdf/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package asdf

import (
"fmt"
"github.com/magefile/mage/sh"
"os"
"os/exec"
"path/filepath"

_ "embed"
Expand All @@ -31,7 +31,7 @@ type Asdf struct {
path string

// env is a list of environment variables to set when executing asdf
env []string
env map[string]string
}

// New creates a new asdf source plugin. If the source plugin is not installed, it will be installed.
Expand All @@ -40,23 +40,17 @@ func New(cfg config.Config, out output.Output) (*Asdf, error) {

asdfPath := filepath.Join(cfg.SourcesDir(), "asdf")

var env []string
env := make(map[string]string)

env = append(env, fmt.Sprintf("ASDF_DIR=%s", asdfPath))
env = append(env, fmt.Sprintf("ASDF_DATA_DIR=%s", asdfPath))
env["ASDF_DIR"] = asdfPath
env["ASDF_DATA_DIR"] = asdfPath

source := &Asdf{path: asdfPath, env: env}

if _, err := os.Stat(asdfPath); os.IsNotExist(err) {
out.V(6).Info(fmt.Sprintf("installing asdf version %s to %s", version, asdfPath))

cmd := exec.Command("git", "clone", "--branch", version, "https://github.com/asdf-vm/asdf.git", asdfPath)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = os.Environ()

err := cmd.Run()
err := sh.RunV("git", "clone", "--branch", version, "https://github.com/asdf-vm/asdf.git", asdfPath)
if err != nil {
return nil, fmt.Errorf("failed to clone asdf: %w", err)
}
Expand All @@ -67,6 +61,13 @@ func New(cfg config.Config, out output.Output) (*Asdf, error) {
}
}

version, err := source.execute("version")
if err != nil {
return nil, fmt.Errorf("failed to get asdf version: %w", err)
}

out.V(6).Info(fmt.Sprintf("asdf version %s installed to %s", version, asdfPath))

return source, nil
}

Expand Down

0 comments on commit df44d6c

Please sign in to comment.