-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
122 lines (102 loc) · 2.95 KB
/
plugin.go
File metadata and controls
122 lines (102 loc) · 2.95 KB
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
// Package scaffold implements a scaffold launchr plugin
package scaffold
import (
"context"
_ "embed"
"path/filepath"
"github.com/launchrctl/launchr"
"github.com/launchrctl/launchr/pkg/action"
)
//go:embed action.yaml
var actionYaml []byte
func init() {
launchr.RegisterPlugin(&Plugin{})
}
// Plugin is [launchr.Plugin] providing scaffold functionality.
type Plugin struct {
m action.Manager
}
// PluginInfo implements [launchr.Plugin] interface.
func (p *Plugin) PluginInfo() launchr.PluginInfo {
return launchr.PluginInfo{
Weight: 20,
}
}
// OnAppInit implements [launchr.OnAppInitPlugin] interface.
func (p *Plugin) OnAppInit(app launchr.App) error {
app.GetService(&p.m)
return nil
}
// DiscoverActions implements [launchr.ActionDiscoveryPlugin] interface.
func (p *Plugin) DiscoverActions(_ context.Context) ([]*action.Action, error) {
_ = action.Definition{}
a := action.NewFromYAML("scaffold", actionYaml)
a.SetRuntime(action.NewFnRuntime(func(_ context.Context, a *action.Action) error {
outputDir := a.Input().Opt("output").(string)
runtimeType := a.Input().Opt("runtime").(string)
id := a.Input().Opt("id").(string)
title := a.Input().Opt("title").(string)
containerPreset := a.Input().Opt("preset").(string)
interactive := a.Input().Opt("interactive").(bool)
interactive = interactive && a.Input().Streams() != nil && a.Input().Streams().In().IsTerminal()
scaffold := scaffoldAction{
manager: p.m,
outputDir: outputDir,
runtime: action.DefRuntimeType(runtimeType),
id: id,
title: title,
containerPreset: containerPreset,
interactive: interactive,
}
return scaffold.run()
}))
return []*action.Action{a}, nil
}
type scaffoldAction struct {
manager action.Manager
runtime action.DefRuntimeType
id string
title string
outputDir string
interactive bool
containerPreset string
}
func (s *scaffoldAction) getDefaultValues() *templateValues {
v := &templateValues{
Definition: &action.Definition{
Action: &action.DefAction{
Title: s.title,
Description: "",
Aliases: []string{},
Arguments: []*action.DefParameter{},
Options: []*action.DefParameter{},
},
Runtime: &action.DefRuntime{
Type: s.runtime,
Container: &action.DefRuntimeContainer{},
Shell: &action.DefRuntimeShell{},
},
},
ID: s.id,
ContainerPreset: s.containerPreset,
}
return v
}
// run runs the generator based on command-line arguments
func (s *scaffoldAction) run() error {
defaults := s.getDefaultValues()
metadata := newMetadataCollector(s.manager, s.interactive)
values, err := metadata.collectActionInfo(defaults)
if err != nil {
return err
}
var outputDir string
switch values.Runtime.Type {
case runtimePlugin:
outputDir = filepath.Join(s.outputDir, "plugins")
default:
outputDir = filepath.Join(s.outputDir, "actions")
}
gen := newGenerator(outputDir)
return gen.generate(values)
}