-
Notifications
You must be signed in to change notification settings - Fork 200
/
pkgmgmt.go
79 lines (65 loc) · 1.78 KB
/
pkgmgmt.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 mixin
import (
"bytes"
"io/ioutil"
"os/exec"
"get.porter.sh/porter/pkg/config"
"get.porter.sh/porter/pkg/pkgmgmt"
"get.porter.sh/porter/pkg/pkgmgmt/client"
"get.porter.sh/porter/pkg/portercontext"
)
const (
Directory = "mixins"
)
var _ MixinProvider = &PackageManager{}
// PackageManager handles package management for mixins.
type PackageManager struct {
*client.FileSystem
}
func NewPackageManager(c *config.Config) *PackageManager {
client := &PackageManager{
FileSystem: client.NewFileSystem(c, Directory),
}
client.PreRun = client.PreRunMixinCommandHandler
client.BuildMetadata = func() pkgmgmt.PackageMetadata {
return &Metadata{}
}
return client
}
func (c *PackageManager) PreRunMixinCommandHandler(command string, cmd *exec.Cmd) {
if !IsCoreMixinCommand(command) {
// For custom commands, don't call the mixin as "mixin CUSTOM" but as "mixin invoke --action CUSTOM"
for i := range cmd.Args {
if cmd.Args[i] == command {
cmd.Args[i] = "invoke"
break
}
}
cmd.Args = append(cmd.Args, "--action", command)
}
}
func (c *PackageManager) GetSchema(name string) (string, error) {
mixinDir, err := c.GetPackageDir(name)
if err != nil {
return "", err
}
r := client.NewRunner(name, mixinDir, false)
// Copy the existing context and tweak to pipe the output differently
mixinSchema := &bytes.Buffer{}
var mixinContext portercontext.Context
mixinContext = *c.Context
mixinContext.Out = mixinSchema
if !c.Debug {
mixinContext.Err = ioutil.Discard
}
r.Context = &mixinContext
cmd := pkgmgmt.CommandOptions{Command: "schema", PreRun: c.PreRun}
err = r.Run(cmd)
if err != nil {
return "", err
}
return mixinSchema.String(), nil
}
var _ pkgmgmt.PackageMetadata = Metadata{}
// Metadata about an installed mixin.
type Metadata = pkgmgmt.Metadata