-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
176 lines (152 loc) · 4.84 KB
/
plugin.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Package compose implements a launchr plugin to do platform composition
package compose
import (
"context"
_ "embed"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/launchrctl/keyring"
"github.com/launchrctl/launchr"
"github.com/launchrctl/launchr/pkg/action"
"github.com/launchrctl/compose/compose"
)
var (
//go:embed action.compose.yaml
actionComposeYaml []byte
//go:embed action.add.yaml
actionAddYaml []byte
//go:embed action.update.yaml
actionUpdateYaml []byte
//go:embed action.delete.yaml
actionDeleteYaml []byte
)
func init() {
launchr.RegisterPlugin(&Plugin{})
}
// Plugin is [launchr.Plugin] plugin providing compose.
type Plugin struct {
wd string
k keyring.Keyring
}
// PluginInfo implements [launchr.Plugin] interface.
func (p *Plugin) PluginInfo() launchr.PluginInfo {
return launchr.PluginInfo{Weight: 10}
}
// OnAppInit implements [launchr.OnAppInitPlugin] interface.
func (p *Plugin) OnAppInit(app launchr.App) error {
app.GetService(&p.k)
p.wd = app.GetWD()
buildDir := filepath.Join(p.wd, compose.BuildDir)
app.RegisterFS(action.NewDiscoveryFS(os.DirFS(buildDir), p.wd))
return nil
}
// DiscoverActions implements [launchr.ActionDiscoveryPlugin] interface.
func (p *Plugin) DiscoverActions(_ context.Context) ([]*action.Action, error) {
// Action compose.
composeAction := action.NewFromYAML("compose", actionComposeYaml)
composeAction.SetRuntime(action.NewFnRuntime(func(_ context.Context, a *action.Action) error {
input := a.Input()
c, err := compose.CreateComposer(
p.wd,
compose.ComposerOptions{
Clean: input.Opt("clean").(bool),
WorkingDir: input.Opt("working-dir").(string),
SkipNotVersioned: input.Opt("skip-not-versioned").(bool),
ConflictsVerbosity: input.Opt("conflicts-verbosity").(bool),
Interactive: input.Opt("interactive").(bool),
},
p.k,
)
if err != nil {
return err
}
return c.RunInstall()
}))
// Action compose:add.
addAction := action.NewFromYAML("compose:add", actionAddYaml)
addAction.SetRuntime(action.NewFnRuntime(func(_ context.Context, a *action.Action) error {
input := a.Input()
if err := packagePreRunValidate(input); err != nil {
return err
}
createNew := input.Opt("allow-create").(bool)
composeDependency := getInputDependencies(input)
strategies := getInputStrategies(input)
return compose.AddPackage(createNew, composeDependency, strategies, p.wd)
}))
// Action compose:update.
updateAction := action.NewFromYAML("compose:update", actionUpdateYaml)
updateAction.SetRuntime(action.NewFnRuntime(func(_ context.Context, a *action.Action) error {
input := a.Input()
if err := packagePreRunValidate(input); err != nil {
return err
}
composeDependency := getInputDependencies(input)
strategies := getInputStrategies(input)
if composeDependency.Name != "" {
return compose.UpdatePackage(composeDependency, strategies, p.wd)
}
return compose.UpdatePackages(p.wd)
}))
// Action compose:delete.
deleteAction := action.NewFromYAML("compose:delete", actionDeleteYaml)
deleteAction.SetRuntime(action.NewFnRuntime(func(_ context.Context, a *action.Action) error {
input := a.Input()
toDeletePackages := action.InputOptSlice[string](input, "packages")
return compose.DeletePackages(toDeletePackages, p.wd)
}))
return []*action.Action{
composeAction,
addAction,
updateAction,
deleteAction,
}, nil
}
func getInputDependencies(input *action.Input) *compose.Dependency {
return &compose.Dependency{
Name: input.Opt("package").(string),
Source: compose.Source{
Type: input.Opt("type").(string),
Ref: input.Opt("ref").(string),
Tag: input.Opt("tag").(string),
URL: input.Opt("url").(string),
},
}
}
func getInputStrategies(input *action.Input) *compose.RawStrategies {
return &compose.RawStrategies{
Names: action.InputOptSlice[string](input, "strategy"),
Paths: action.InputOptSlice[string](input, "strategy-path"),
}
}
func packagePreRunValidate(input *action.Input) error {
typeFlag := input.Opt("type").(string)
if typeFlag == compose.HTTPType {
refChanged := input.Opt("ref").(string) != ""
if refChanged {
launchr.Term().Warning().Println("Ref can't be used with HTTP source")
input.SetOpt("ref", "")
}
}
strategies := action.InputOptSlice[string](input, "strategy")
paths := action.InputOptSlice[string](input, "strategy-path")
if len(strategies) > 0 || len(paths) > 0 {
if len(strategies) != len(paths) {
return errors.New("number of strategies and paths must be equal")
}
list := map[string]bool{
compose.StrategyOverwriteLocal: true,
compose.StrategyRemoveExtraLocal: true,
compose.StrategyIgnoreExtraPackage: true,
compose.StrategyFilterPackage: true,
}
for _, strategy := range strategies {
if _, ok := list[strategy]; !ok {
return fmt.Errorf("submitted strategy %s doesn't exist", strategy)
}
}
}
return nil
}