@@ -35,78 +35,82 @@ func (p *Plugin) InitApp(*launchr.App) error {
3535 return nil
3636}
3737
38+ type builderInput struct {
39+ name string
40+ out string
41+ timeout string
42+ plugins []string
43+ replace []string
44+ debug bool
45+ }
46+
3847// CobraAddCommands implements launchr.CobraPlugin interface to provide build functionality.
3948func (p * Plugin ) CobraAddCommands (rootCmd * cobra.Command ) error {
4049 // Flag options.
41- var (
42- name string
43- out string
44- timeoutStr string
45- plugins []string
46- replace []string
47- debug bool
48- )
50+ flags := builderInput {}
4951
5052 var buildCmd = & cobra.Command {
5153 Use : "build" ,
5254 RunE : func (cmd * cobra.Command , args []string ) error {
5355 // Don't show usage help on a runtime error.
5456 cmd .SilenceUsage = true
55-
56- // Set build timeout.
57- ctx := cmd .Context ()
58- timeout , err := time .ParseDuration (timeoutStr )
59- if err != nil {
60- return err
61- }
62- if timeout != 0 {
63- // Execute build.
64- var cancel context.CancelFunc
65- ctx , cancel = context .WithTimeout (ctx , timeout )
66- defer cancel ()
67- }
68-
69- // Parse dependencies definition.
70- allplugs , err := parsePlugins (plugins )
71- if err != nil {
72- return err
73- }
74- allrepl , err := parseReplace (replace )
75- if err != nil {
76- return err
77- }
78-
79- // Set output path if it's not defined.
80- if len (out ) == 0 && len (name ) > 0 {
81- out = "./" + name
82- }
83-
84- opts := & BuildOptions {
85- LaunchrVersion : launchr .GetVersion (),
86- CorePkg : UsePluginInfo {Package : launchrPkg },
87- PkgName : name ,
88- ModReplace : allrepl ,
89- Plugins : allplugs ,
90- BuildOutput : out ,
91- Debug : debug ,
92- }
93-
94- return Execute (ctx , opts )
57+ return Execute (cmd .Context (), & flags )
9558 },
9659 }
9760 // Command flags.
98- buildCmd .Flags ().StringVarP (& name , "name" , "n" , "launchr" , `Result application name` )
99- buildCmd .Flags ().StringVarP (& out , "output" , "o" , "" , `Build output file, by default application name is used` )
100- buildCmd .Flags ().StringVarP (& timeoutStr , "timeout" , "t" , "120s" , `Build timeout duration, example: 0, 100ms, 1h23m` )
101- buildCmd .Flags ().StringSliceVarP (& plugins , "plugin" , "p" , nil , `Include PLUGIN into the build with an optional version` )
102- buildCmd .Flags ().StringSliceVarP (& replace , "replace" , "r" , nil , `Replace go dependency, see "go mod edit -replace"` )
103- buildCmd .Flags ().BoolVarP (& debug , "debug" , "d" , false , `Include debug flags into the build to support go debugging with "delve". If not specified, debugging info is trimmed` )
61+ buildCmd .Flags ().StringVarP (& flags . name , "name" , "n" , "launchr" , `Result application name` )
62+ buildCmd .Flags ().StringVarP (& flags . out , "output" , "o" , "" , `Build output file, by default application name is used` )
63+ buildCmd .Flags ().StringVarP (& flags . timeout , "timeout" , "t" , "120s" , `Build timeout duration, example: 0, 100ms, 1h23m` )
64+ buildCmd .Flags ().StringSliceVarP (& flags . plugins , "plugin" , "p" , nil , `Include PLUGIN into the build with an optional version` )
65+ buildCmd .Flags ().StringSliceVarP (& flags . replace , "replace" , "r" , nil , `Replace go dependency, see "go mod edit -replace"` )
66+ buildCmd .Flags ().BoolVarP (& flags . debug , "debug" , "d" , false , `Include debug flags into the build to support go debugging with "delve". If not specified, debugging info is trimmed` )
10467 rootCmd .AddCommand (buildCmd )
10568 return nil
10669}
10770
10871// Execute runs launchr and executes build of launchr.
109- func Execute (ctx context.Context , opts * BuildOptions ) error {
72+ func Execute (ctx context.Context , flags * builderInput ) error {
73+ // Set build timeout.
74+ timeout , err := time .ParseDuration (flags .timeout )
75+ if err != nil {
76+ return err
77+ }
78+ if timeout != 0 {
79+ // Execute build.
80+ var cancel context.CancelFunc
81+ ctx , cancel = context .WithTimeout (ctx , timeout )
82+ defer cancel ()
83+ }
84+
85+ // Parse dependencies definition.
86+ plugins , err := parsePlugins (flags .plugins )
87+ if err != nil {
88+ return err
89+ }
90+ replace , err := parseReplace (flags .replace )
91+ if err != nil {
92+ return err
93+ }
94+
95+ // Set output path if it's not defined.
96+ if len (flags .out ) == 0 && len (flags .name ) > 0 {
97+ flags .out = "./" + flags .name
98+ }
99+
100+ opts := & BuildOptions {
101+ LaunchrVersion : launchr .GetVersion (),
102+ CorePkg : UsePluginInfo {Path : launchrPkg },
103+ PkgName : flags .name ,
104+ ModReplace : replace ,
105+ Plugins : plugins ,
106+ BuildOutput : flags .out ,
107+ Debug : flags .debug ,
108+ }
109+
110+ if err = opts .Validate (); err != nil {
111+ return err
112+ }
113+
110114 builder , err := NewBuilder (opts )
111115 if err != nil {
112116 return err
@@ -116,34 +120,28 @@ func Execute(ctx context.Context, opts *BuildOptions) error {
116120}
117121
118122func parsePlugins (plugins []string ) ([]UsePluginInfo , error ) {
119- // Collect unique plugins. Include default launchr plugins.
120- defaultPlugins := launchrPkg + "/pkg/plugins"
121- setplugs := map [string ]UsePluginInfo {
122- defaultPlugins : {defaultPlugins , "" },
123- }
123+ // Collect unique plugins.
124+ unique := map [string ]UsePluginInfo {}
124125 for _ , pdef := range plugins {
125- pv := strings .SplitN (pdef , "@" , 2 )
126- if len (pv ) == 1 {
127- pv = append (pv , "" )
128- }
129- setplugs [pv [0 ]] = UsePluginInfo {pv [0 ], pv [1 ]}
126+ pi := UsePluginInfoFromString (pdef )
127+ unique [pi .Path ] = pi
130128 }
131- allplugs := make ([]UsePluginInfo , 0 , len (setplugs ))
132- for _ , p := range setplugs {
133- allplugs = append (allplugs , p )
129+ res := make ([]UsePluginInfo , 0 , len (unique ))
130+ for _ , p := range unique {
131+ res = append (res , p )
134132 }
135- return allplugs , nil
133+ return res , nil
136134}
137135
138136func parseReplace (replace []string ) (map [string ]string , error ) {
139137 // Replace module dependencies, e.g. with local paths for development or different version.
140- allrepl := map [string ]string {}
138+ repl := map [string ]string {}
141139 for _ , rdef := range replace {
142140 oldnew := strings .SplitN (rdef , "=" , 2 )
143141 if len (oldnew ) == 1 {
144142 return nil , fmt .Errorf ("incorrect replace definition: %s" , rdef )
145143 }
146- allrepl [oldnew [0 ]] = oldnew [1 ]
144+ repl [oldnew [0 ]] = oldnew [1 ]
147145 }
148- return allrepl , nil
146+ return repl , nil
149147}
0 commit comments