Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow environment variables to be set #23

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 67 additions & 9 deletions internal/service/aquifer/aquifer.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package aquifer

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

"github.com/anoriqq/qpm/internal/config"
"github.com/fatih/color"
Expand All @@ -16,6 +19,7 @@ import (
const (
envOS = "QPM_OS"
envArch = "QPM_ARCH"
envEnv = "QPM_ENV"
)

var headerOutput = color.New(color.FgHiCyan).Add(color.Bold)
Expand Down Expand Up @@ -96,29 +100,83 @@ func getAquifer(aquiferPath string) (Aquifer, error) {
func getPlan(aquifer Aquifer, os string) (Plan, error) {
plan, ok := aquifer.Install[os]
if !ok {
return Plan{}, fmt.Errorf("not declared os: %s", runtime.GOOS)
return Plan{}, fmt.Errorf("not declared os: %s", os)
}

return plan, nil
}

func installAquifer(plan Plan) error {
type envMap map[string]string
func (e envMap) ToEnvSlice() []string {
result := make([]string, len(e))

var i int
for k, v := range e {
result[i] = fmt.Sprintf("%s=%s", k, v)
i++
}

return result
}
func (e envMap) Append(key, val string) {
e[key] = val
}

func installAquifer(plan Plan) (err error) {
envFilePath := path.Join(config.Cfg.AquiferDir, "tmp.env")

f, err := os.Create(envFilePath)
if err != nil {
return err
}

defer func() {
f.Close()
err = os.Remove(envFilePath)
}()

envs := envMap{
envArch: runtime.GOARCH,
envOS: runtime.GOOS,
envEnv: envFilePath,
}

for i, v := range plan.Run {
headerOutput.Printf("[%d] %s\n", i+1, v)
headerOutput.Printf("[%d/%d] %s\n", i+1, len(plan.Run), v)

c := exec.Command("bash", "-c", v)
c.Env = append(c.Env,
fmt.Sprintf("%s=%s", envOS, runtime.GOARCH),
fmt.Sprintf("%s=%s", envArch, runtime.GOOS),
)

output, err := c.Output()
c.Dir = config.Cfg.AquiferDir

envFileBytes, err := io.ReadAll(f)
if err != nil {
return err
}

envStrings := strings.Split(string(envFileBytes), "\n")
for _, v := range envStrings {
keyval := strings.Split(v, "=")
if len(keyval) == 2 {
envs.Append(keyval[0], keyval[1])
}
}

c.Env = append(c.Env, envs.ToEnvSlice()...)

var stdout, stderr bytes.Buffer
c.Stdout, c.Stderr = &stdout, &stderr

err = c.Run()
if err != nil {
fmt.Printf("%v", stdout.String())
fmt.Printf("%v", stderr.String())
return err
}

fmt.Printf("%v", string(output))
fmt.Printf("%v", stdout.String())
}

headerOutput.Println("[complete]")

return nil
}