@@ -57,174 +57,174 @@ var rootCmd = &cobra.Command{
5757 Args : cobra .ExactArgs (1 ),
5858 SilenceUsage : true ,
5959 RunE : func (cmd * cobra.Command , args []string ) error {
60- return cli .WithContext (context .Background (), func (ctx context.Context ) error {
61- report := reporter .New ()
62- report .Report (reporter.Update {
63- Message : "assembling the finalized profile..." ,
64- Status : reporter .StatusRunning ,
65- })
66-
67- baseProfile := args [0 ]
68-
69- var prof profile.Profile
60+ ctx := cmd .Context ()
7061
71- if baseProfile == "-" {
72- if err := yaml .NewDecoder (os .Stdin ).Decode (& prof ); err != nil {
73- return err
74- }
75- } else {
76- prof = profile.Profile {
77- BaseProfileName : baseProfile ,
78- Arch : cmdFlags .Arch ,
79- Platform : cmdFlags .Platform ,
80- Customization : profile.CustomizationProfile {
81- ExtraKernelArgs : cmdFlags .ExtraKernelArgs ,
82- MetaContents : cmdFlags .MetaValues .GetMetaValues (),
83- },
84- }
85-
86- extraOverlayOptions := overlay.ExtraOptions {}
87-
88- for _ , option := range cmdFlags .OverlayOptions {
89- if strings .HasPrefix (option , "@" ) {
90- data , err := os .ReadFile (option [1 :])
91- if err != nil {
92- return err
93- }
94-
95- decoder := yaml .NewDecoder (bytes .NewReader (data ))
96- decoder .KnownFields (true )
62+ report := reporter .New ()
63+ report .Report (reporter.Update {
64+ Message : "assembling the finalized profile..." ,
65+ Status : reporter .StatusRunning ,
66+ })
9767
98- if err := decoder .Decode (& extraOverlayOptions ); err != nil {
99- return err
100- }
68+ baseProfile := args [0 ]
10169
102- continue
103- }
70+ var prof profile.Profile
10471
105- k , v , _ := strings .Cut (option , "=" )
72+ if baseProfile == "-" {
73+ if err := yaml .NewDecoder (os .Stdin ).Decode (& prof ); err != nil {
74+ return err
75+ }
76+ } else {
77+ prof = profile.Profile {
78+ BaseProfileName : baseProfile ,
79+ Arch : cmdFlags .Arch ,
80+ Platform : cmdFlags .Platform ,
81+ Customization : profile.CustomizationProfile {
82+ ExtraKernelArgs : cmdFlags .ExtraKernelArgs ,
83+ MetaContents : cmdFlags .MetaValues .GetMetaValues (),
84+ },
85+ }
10686
107- if strings .HasPrefix (v , "@" ) {
108- data , err := os .ReadFile (v [1 :])
109- if err != nil {
110- return err
111- }
87+ extraOverlayOptions := overlay.ExtraOptions {}
11288
113- v = string (data )
89+ for _ , option := range cmdFlags .OverlayOptions {
90+ if strings .HasPrefix (option , "@" ) {
91+ data , err := os .ReadFile (option [1 :])
92+ if err != nil {
93+ return err
11494 }
11595
116- extraOverlayOptions [ k ] = v
117- }
96+ decoder := yaml . NewDecoder ( bytes . NewReader ( data ))
97+ decoder . KnownFields ( true )
11898
119- if cmdFlags .OverlayName != "" || cmdFlags .OverlayImage != "" {
120- prof .Overlay = & profile.OverlayOptions {
121- Name : cmdFlags .OverlayName ,
122- Image : profile.ContainerAsset {
123- ImageRef : cmdFlags .OverlayImage ,
124- },
125- ExtraOptions : extraOverlayOptions ,
99+ if err := decoder .Decode (& extraOverlayOptions ); err != nil {
100+ return err
126101 }
127102
128- prof . Input . OverlayInstaller . ImageRef = cmdFlags . OverlayImage
103+ continue
129104 }
130105
131- prof .Input .SystemExtensions = xslices .Map (
132- cmdFlags .SystemExtensionImages ,
133- func (imageRef string ) profile.ContainerAsset {
134- return profile.ContainerAsset {
135- ImageRef : imageRef ,
136- ForceInsecure : cmdFlags .Insecure ,
137- }
138- },
139- )
106+ k , v , _ := strings .Cut (option , "=" )
140107
141- if cmdFlags . OutputKind != "" {
142- outKind , err := profile . OutputKindString ( cmdFlags . OutputKind )
108+ if strings . HasPrefix ( v , "@" ) {
109+ data , err := os . ReadFile ( v [ 1 :] )
143110 if err != nil {
144111 return err
145112 }
146113
147- prof . Output . Kind = outKind
114+ v = string ( data )
148115 }
149116
150- if cmdFlags .BaseInstallerImage != "" {
151- prof .Input .BaseInstaller = profile.ContainerAsset {
152- ImageRef : cmdFlags .BaseInstallerImage ,
153- }
117+ extraOverlayOptions [k ] = v
118+ }
119+
120+ if cmdFlags .OverlayName != "" || cmdFlags .OverlayImage != "" {
121+ prof .Overlay = & profile.OverlayOptions {
122+ Name : cmdFlags .OverlayName ,
123+ Image : profile.ContainerAsset {
124+ ImageRef : cmdFlags .OverlayImage ,
125+ },
126+ ExtraOptions : extraOverlayOptions ,
154127 }
155128
156- if cmdFlags . ImageCache != "" {
157- parseOpts := []name. Option { name . StrictValidation }
129+ prof . Input . OverlayInstaller . ImageRef = cmdFlags . OverlayImage
130+ }
158131
159- if cmdFlags .Insecure {
160- parseOpts = append (parseOpts , name .Insecure )
132+ prof .Input .SystemExtensions = xslices .Map (
133+ cmdFlags .SystemExtensionImages ,
134+ func (imageRef string ) profile.ContainerAsset {
135+ return profile.ContainerAsset {
136+ ImageRef : imageRef ,
137+ ForceInsecure : cmdFlags .Insecure ,
161138 }
139+ },
140+ )
162141
163- if _ , err := name .ParseReference (cmdFlags .ImageCache , parseOpts ... ); err == nil {
164- prof .Input .ImageCache = profile.ContainerAsset {
165- ImageRef : cmdFlags .ImageCache ,
166- }
167- } else {
168- prof .Input .ImageCache = profile.ContainerAsset {
169- OCIPath : cmdFlags .ImageCache ,
170- }
171- }
142+ if cmdFlags .OutputKind != "" {
143+ outKind , err := profile .OutputKindString (cmdFlags .OutputKind )
144+ if err != nil {
145+ return err
172146 }
173147
174- if cmdFlags .Insecure {
175- prof .Input .BaseInstaller .ForceInsecure = cmdFlags .Insecure
176- prof .Input .ImageCache .ForceInsecure = cmdFlags .Insecure
148+ prof .Output .Kind = outKind
149+ }
150+
151+ if cmdFlags .BaseInstallerImage != "" {
152+ prof .Input .BaseInstaller = profile.ContainerAsset {
153+ ImageRef : cmdFlags .BaseInstallerImage ,
177154 }
155+ }
178156
179- if cmdFlags .SecurebootIncludeWellKnownCerts {
180- if prof .Input .SecureBoot == nil {
181- prof .Input .SecureBoot = & profile.SecureBootAssets {}
182- }
157+ if cmdFlags .ImageCache != "" {
158+ parseOpts := []name.Option {name .StrictValidation }
183159
184- prof .Input .SecureBoot .IncludeWellKnownCerts = true
160+ if cmdFlags .Insecure {
161+ parseOpts = append (parseOpts , name .Insecure )
185162 }
186163
187- if cmdFlags .EmbeddedConfigPath != "" {
188- data , err := os .ReadFile (cmdFlags .EmbeddedConfigPath )
189- if err != nil {
190- return fmt .Errorf ("error reading embedded config file: %w" , err )
164+ if _ , err := name .ParseReference (cmdFlags .ImageCache , parseOpts ... ); err == nil {
165+ prof .Input .ImageCache = profile.ContainerAsset {
166+ ImageRef : cmdFlags .ImageCache ,
167+ }
168+ } else {
169+ prof .Input .ImageCache = profile.ContainerAsset {
170+ OCIPath : cmdFlags .ImageCache ,
191171 }
192-
193- prof .Customization .EmbeddedMachineConfiguration = string (data )
194172 }
195173 }
196174
197- if err := os .MkdirAll (cmdFlags .OutputPath , 0o755 ); err != nil {
198- return err
175+ if cmdFlags .Insecure {
176+ prof .Input .BaseInstaller .ForceInsecure = cmdFlags .Insecure
177+ prof .Input .ImageCache .ForceInsecure = cmdFlags .Insecure
199178 }
200179
201- imager , err := imager .New (prof )
202- if err != nil {
203- return err
180+ if cmdFlags .SecurebootIncludeWellKnownCerts {
181+ if prof .Input .SecureBoot == nil {
182+ prof .Input .SecureBoot = & profile.SecureBootAssets {}
183+ }
184+
185+ prof .Input .SecureBoot .IncludeWellKnownCerts = true
204186 }
205187
206- if _ , err = imager . Execute ( ctx , cmdFlags .OutputPath , report ); err != nil {
207- report . Report (reporter. Update {
208- Message : err . Error (),
209- Status : reporter . StatusError ,
210- })
188+ if cmdFlags .EmbeddedConfigPath != "" {
189+ data , err := os . ReadFile ( cmdFlags . EmbeddedConfigPath )
190+ if err != nil {
191+ return fmt . Errorf ( "error reading embedded config file: %w" , err )
192+ }
211193
212- return err
194+ prof . Customization . EmbeddedMachineConfiguration = string ( data )
213195 }
196+ }
214197
215- if cmdFlags .TarToStdout {
216- return archiver . TarGz ( ctx , cmdFlags . OutputPath , os . Stdout )
217- }
198+ if err := os . MkdirAll ( cmdFlags .OutputPath , 0o755 ); err != nil {
199+ return err
200+ }
218201
219- return nil
220- })
202+ imager , err := imager .New (prof )
203+ if err != nil {
204+ return err
205+ }
206+
207+ if _ , err = imager .Execute (ctx , cmdFlags .OutputPath , report ); err != nil {
208+ report .Report (reporter.Update {
209+ Message : err .Error (),
210+ Status : reporter .StatusError ,
211+ })
212+
213+ return err
214+ }
215+
216+ if cmdFlags .TarToStdout {
217+ return archiver .TarGz (ctx , cmdFlags .OutputPath , os .Stdout )
218+ }
219+
220+ return nil
221221 },
222222}
223223
224224// Execute adds all child commands to the root command and sets flags appropriately.
225225// This is called by main.main(). It only needs to happen once to the rootCmd.
226226func Execute () {
227- if err := rootCmd . Execute ( ); err != nil {
227+ if _ , err := cli . WithContextC ( context . Background (), rootCmd . ExecuteContextC ); err != nil {
228228 os .Exit (1 )
229229 }
230230}
0 commit comments