-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
exec.go
79 lines (72 loc) · 2.01 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package controller
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/suzuki-shunsuke/go-timeout/timeout"
)
func (ctrl *Controller) Exec(ctx context.Context, param *Param, args []string) error {
if len(args) == 0 {
return errors.New("command is required")
}
exeName := args[0]
cfg := &Config{}
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("get the current directory: %w", err)
}
param.ConfigFilePath = ctrl.getConfigFilePath(wd, param.ConfigFilePath)
if param.ConfigFilePath == "" {
return errConfigFileNotFound
}
if err := ctrl.readConfig(wd, param.ConfigFilePath, cfg); err != nil {
return err
}
if cfg.BinDir == "" {
cfg.BinDir = filepath.Join(filepath.Dir(param.ConfigFilePath), ".aqua", "bin")
}
inlineRepo := make(map[string]*PackageInfo, len(cfg.InlineRepository))
for _, pkgInfo := range cfg.InlineRepository {
inlineRepo[pkgInfo.Name] = pkgInfo
}
for _, pkg := range cfg.Packages {
if pkg.Name != exeName {
continue
}
if err := ctrl.installPackage(ctx, inlineRepo, pkg, cfg); err != nil {
return err
}
pkgInfo, ok := inlineRepo[pkg.Name]
if !ok {
return fmt.Errorf("repository isn't found %s", pkg.Name)
}
return ctrl.exec(ctx, exeName, pkg, pkgInfo, args[1:])
}
return nil
}
func (ctrl *Controller) exec(ctx context.Context, exeName string, pkg *Package, pkgInfo *PackageInfo, args []string) error {
assetName, err := pkgInfo.Artifact.Execute(map[string]interface{}{
"Package": pkg,
"PackageInfo": pkgInfo,
"OS": runtime.GOOS,
"Arch": runtime.GOARCH,
})
if err != nil {
return fmt.Errorf("render the asset name: %w", err)
}
pkgPath := getPkgPath(ctrl.RootDir, pkg, pkgInfo, assetName)
exePath := filepath.Join(pkgPath, exeName) // TODO
cmd := exec.Command(exePath, args...)
cmd.Stdin = ctrl.Stdin
cmd.Stdout = ctrl.Stdout
cmd.Stderr = ctrl.Stderr
runner := timeout.NewRunner(0)
if err := runner.Run(ctx, cmd); err != nil {
return fmt.Errorf("execute the command: %w", err)
}
return nil
}