22package launchr
33
44import (
5- "bytes"
65 "fmt"
7- "io/fs"
86 "os"
97 "path/filepath"
10- "text/template"
118
129 "github.com/spf13/cobra"
1310
14- "github.com/launchrctl/launchr/core /cli"
15- "github.com/launchrctl/launchr/core /log"
11+ "github.com/launchrctl/launchr/pkg /cli"
12+ "github.com/launchrctl/launchr/pkg /log"
1613)
1714
1815// App holds app related global variables.
1916type App struct {
2017 cmd * cobra.Command
2118 cli cli.Cli
22- wdFS fs.FS
23- version fmt.Stringer
19+ workDir string
20+ cfgDir string
21+ version * AppVersion
2422 plugins map [PluginInfo ]Plugin
2523}
2624
@@ -29,14 +27,9 @@ func NewApp() *App {
2927 return & App {}
3028}
3129
32- // GetFS returns application wdFS.
33- func (app * App ) GetFS () fs.FS {
34- return app .wdFS
35- }
36-
37- // SetFS sets application wdFS.
38- func (app * App ) SetFS (f fs.FS ) {
39- app .wdFS = f
30+ // GetWD provides app's working dir.
31+ func (app * App ) GetWD () string {
32+ return app .workDir
4033}
4134
4235// GetCli returns application cli.
@@ -49,27 +42,24 @@ func (app *App) SetCli(c cli.Cli) {
4942 app .cli = c
5043}
5144
52- // SetVersion sets application cli.
53- func (app * App ) SetVersion (v fmt.Stringer ) {
54- app .version = v
55- }
56-
57- // Version returns application version string.
58- func (app * App ) Version () string {
59- return app .version .String ()
60- }
61-
6245// Plugins returns installed app plugins.
6346func (app * App ) Plugins () map [PluginInfo ]Plugin {
6447 return app .plugins
6548}
6649
6750// Init initializes application and plugins.
6851func (app * App ) Init () error {
52+ var err error
53+ app .version = GetVersion ()
6954 // Global configuration.
55+ app .cfgDir = fmt .Sprintf (".%s" , app .version .Name )
56+ app .workDir , err = filepath .Abs ("./" )
57+ if err != nil {
58+ return err
59+ }
7060 appCli , err := cli .NewAppCli (
7161 cli .WithStandardStreams (),
72- cli .WithGlobalConfigFromDir (os .DirFS (".launchr" )), // @todo how should it work with embed?
62+ cli .WithGlobalConfigFromDir (os .DirFS (app . cfgDir )),
7363 )
7464 if err != nil {
7565 return err
@@ -87,78 +77,6 @@ func (app *App) Init() error {
8777 return nil
8878}
8979
90- func (app * App ) gen (buildPath string , wordDir string ) error {
91- var err error
92- buildPath , err = filepath .Abs (buildPath )
93- if err != nil {
94- return err
95- }
96- wordDir , err = filepath .Abs (wordDir )
97- if err != nil {
98- return err
99- }
100- // Clean build path before generating.
101- err = filepath .WalkDir (buildPath , func (path string , dir fs.DirEntry , err error ) error {
102- if path == buildPath {
103- return nil
104- }
105- if dir .IsDir () {
106- errRem := os .RemoveAll (path )
107- if errRem != nil {
108- return errRem
109- }
110- return filepath .SkipDir
111- }
112- if dir .Name () != "pkg.go" {
113- errRem := os .Remove (path )
114- if errRem != nil {
115- return errRem
116- }
117- }
118- return nil
119- })
120- if err != nil {
121- return err
122- }
123- // Call generate functions on plugins.
124- plugins := app .Plugins ()
125- initSet := make (map [string ]struct {}, len (plugins ))
126- for _ , p := range app .Plugins () {
127- p , ok := p .(GeneratePlugin )
128- if ok {
129- genData , err := p .Generate (buildPath , wordDir )
130- if err != nil {
131- return err
132- }
133- for _ , class := range genData .Plugins {
134- initSet [class ] = struct {}{}
135- }
136- }
137- }
138- if len (initSet ) > 0 {
139- var tplName = "init.gen"
140- tpl , err := template .New (tplName ).Parse (initGenTemplate )
141- if err != nil {
142- return err
143- }
144- // Generate main.go.
145- var buf bytes.Buffer
146- err = tpl .Execute (& buf , & initTplVars {
147- Plugins : initSet ,
148- })
149- if err != nil {
150- return err
151- }
152- target := filepath .Join (buildPath , tplName + ".go" )
153- err = os .WriteFile (target , buf .Bytes (), 0600 )
154- if err != nil {
155- return err
156- }
157- }
158-
159- return nil
160- }
161-
16280func (app * App ) exec () error {
16381 // Set root cobra command.
16482 var rootCmd = & cobra.Command {
@@ -174,9 +92,7 @@ func (app *App) exec() error {
17492 return cmd .Help ()
17593 },
17694 }
177- if app .version != nil {
178- rootCmd .SetVersionTemplate (app .Version ())
179- }
95+ rootCmd .SetVersionTemplate (app .version .String ())
18096 verbosityFlags (rootCmd )
18197 for _ , p := range app .Plugins () {
18298 p , ok := p .(CobraPlugin )
@@ -195,23 +111,8 @@ func (app *App) exec() error {
195111 return app .cmd .Execute ()
196112}
197113
198- // Generate runs generation of included plugins.
199- func (app * App ) Generate () int {
200- buildPath := "./gen"
201- wd := "./"
202- if len (os .Args ) > 1 {
203- wd = os .Args [1 ]
204- }
205- if err := app .gen (buildPath , wd ); err != nil {
206- fmt .Fprintln (os .Stderr , err )
207- return 125
208- }
209- return 0
210- }
211-
212114// Execute is a cobra entrypoint to the launchr app.
213115func (app * App ) Execute () int {
214- app .version = GetVersion ()
215116 if err := app .exec (); err != nil {
216117 fmt .Fprintln (os .Stderr , err )
217118 return 1
@@ -249,32 +150,3 @@ func Run() int {
249150 }
250151 return app .Execute ()
251152}
252-
253- // Gen generates application specific build files and returns os exit code.
254- func Gen () int {
255- app := NewApp ()
256- if err := app .Init (); err != nil {
257- fmt .Fprintln (os .Stderr , err )
258- return 125
259- }
260- return app .Generate ()
261- }
262-
263- type initTplVars struct {
264- Plugins map [string ]struct {}
265- }
266-
267- const initGenTemplate = `
268- {{- print "// GENERATED. DO NOT EDIT." }}
269- package gen
270-
271- import (
272- "github.com/launchrctl/launchr"
273- )
274-
275- func init() {
276- {{- range $class, $ := .Plugins }}
277- launchr.RegisterPlugin(&{{$class}}{})
278- {{- end }}
279- }
280- `
0 commit comments