@@ -32,6 +32,7 @@ import (
3232 "github.com/oasisprotocol/cli/cmd/common"
3333 roflCmdBuild "github.com/oasisprotocol/cli/cmd/rofl/build"
3434 roflCommon "github.com/oasisprotocol/cli/cmd/rofl/common"
35+ roflProvider "github.com/oasisprotocol/cli/cmd/rofl/provider"
3536 cliConfig "github.com/oasisprotocol/cli/config"
3637)
3738
4041 deployOffer string
4142 deployMachine string
4243 deployForce bool
43- deployShowOffers bool
4444 deployReplaceMachine bool
4545
4646 deployCmd = & cobra.Command {
@@ -129,9 +129,17 @@ var (
129129
130130 fmt .Printf ("Using provider: %s (%s)\n " , machine .Provider , providerAddr )
131131
132- if deployShowOffers {
132+ if roflCommon . ShowOffers {
133133 // Display all offers supported by the provider.
134- showProviderOffers (ctx , npa , conn , * providerAddr )
134+ offers , err := fetchProviderOffers (ctx , npa , conn , * providerAddr )
135+ cobra .CheckErr (err )
136+
137+ fmt .Println ()
138+ fmt .Printf ("Offers available from the selected provider:\n " )
139+ for _ , offer := range offers {
140+ roflProvider .ShowOfferSummary (npa , offer )
141+ }
142+ fmt .Println ()
135143 return
136144 }
137145
@@ -183,12 +191,17 @@ var (
183191 }
184192 }
185193 if offer == nil {
186- showProviderOffers (ctx , npa , conn , * providerAddr )
194+ fmt .Println ()
195+ fmt .Printf ("Offers available from the selected provider:\n " )
196+ for _ , of := range offers {
197+ roflProvider .ShowOfferSummary (npa , of )
198+ }
199+ fmt .Println ()
187200 return nil , nil , fmt .Errorf ("offer '%s' not found for provider '%s'" , machine .Offer , providerAddr )
188201 }
189202
190203 fmt .Printf ("Taking offer:\n " )
191- showProviderOffer ( ctx , offer )
204+ roflProvider . ShowOfferSummary ( npa , offer )
192205
193206 term := detectTerm (offer )
194207 if roflCommon .TermCount < 1 {
@@ -203,7 +216,7 @@ var (
203216 }
204217 cobra .CheckErr (totalPrice .Mul (qTermCount ))
205218 tp := types .NewBaseUnits (totalPrice , offer .Payment .Native .Denomination )
206- fmt .Printf ("Selected per-%s pricing term, total price is " , term2str (term ))
219+ fmt .Printf ("Selected per-%s pricing term, total price is " , roflCommon . FormatTerm (term ))
207220 tp .PrettyPrint (ctx , "" , os .Stdout )
208221 fmt .Println ("." )
209222 // Warn the user about the non-refundable rental policy before first renting.
@@ -350,7 +363,7 @@ func pushBundleToOciRepository(orcFilename string, ociRepository string) (string
350363func detectTerm (offer * roflmarket.Offer ) (term roflmarket.Term ) {
351364 if offer == nil {
352365 cobra .CheckErr (fmt .Errorf ("no offers exist to determine payment term" ))
353- return // Linter complains otherwise.
366+ return term // Linter complains otherwise.
354367 }
355368 if offer .Payment .Native == nil {
356369 cobra .CheckErr (fmt .Errorf ("no payment terms available for offer '%s'" , offer .ID ))
@@ -362,7 +375,7 @@ func detectTerm(offer *roflmarket.Offer) (term roflmarket.Term) {
362375 if _ , ok := offer .Payment .Native .Terms [term ]; ! ok {
363376 cobra .CheckErr (fmt .Errorf ("term '%s' is not available for offer '%s'" , roflCommon .Term , offer .ID ))
364377 }
365- return
378+ return term
366379 }
367380
368381 // Take the longest payment period.
@@ -372,108 +385,20 @@ func detectTerm(offer *roflmarket.Offer) (term roflmarket.Term) {
372385 term = t
373386 }
374387 }
375- return
388+ return term
376389}
377390
378391func fetchProviderOffers (ctx context.Context , npa * common.NPASelection , conn connection.Connection , provider types.Address ) (offers []* roflmarket.Offer , err error ) {
379392 offers , err = conn .Runtime (npa .ParaTime ).ROFLMarket .Offers (ctx , client .RoundLatest , provider )
380393 if err != nil {
381394 err = fmt .Errorf ("failed to query provider: %s" , err )
382- return
395+ return offers , err
383396 }
384397 // Order offers, newer first.
385398 sort .Slice (offers , func (i , j int ) bool {
386399 return bytes .Compare (offers [i ].ID [:], offers [j ].ID [:]) > 0
387400 })
388- return
389- }
390-
391- func showProviderOffers (ctx context.Context , npa * common.NPASelection , conn connection.Connection , provider types.Address ) {
392- offers , err := fetchProviderOffers (ctx , npa , conn , provider )
393- cobra .CheckErr (err )
394-
395- fmt .Println ()
396- fmt .Printf ("Offers available from the selected provider:\n " )
397- for idx , offer := range offers {
398- showProviderOffer (ctx , offer )
399- if idx != len (offers )- 1 {
400- fmt .Println ()
401- }
402- }
403- fmt .Println ()
404- }
405-
406- func showProviderOffer (ctx context.Context , offer * roflmarket.Offer ) {
407- name , ok := offer .Metadata [provider .SchedulerMetadataOfferKey ]
408- if ! ok {
409- name = "<unnamed>"
410- }
411-
412- var tee string
413- switch offer .Resources .TEE {
414- case roflmarket .TeeTypeSGX :
415- tee = "sgx"
416- case roflmarket .TeeTypeTDX :
417- tee = "tdx"
418- default :
419- tee = "<unknown>"
420- }
421-
422- fmt .Printf ("- %s [%s]\n " , name , offer .ID )
423- fmt .Printf (" TEE: %s | Memory: %d MiB | vCPUs: %d | Storage: %.2f GiB\n " ,
424- tee ,
425- offer .Resources .Memory ,
426- offer .Resources .CPUCount ,
427- float64 (offer .Resources .Storage )/ 1024. ,
428- )
429- if _ , ok := offer .Metadata [provider .NoteMetadataKey ]; ok {
430- fmt .Printf (" Note: %s\n " , offer .Metadata [provider .NoteMetadataKey ])
431- }
432- if _ , ok := offer .Metadata [provider .DescriptionMetadataKey ]; ok {
433- fmt .Printf (" Description:\n %s\n " , strings .ReplaceAll (offer .Metadata [provider .DescriptionMetadataKey ], "\n " , "\n " ))
434- }
435- if offer .Payment .Native != nil {
436- if len (offer .Payment .Native .Terms ) == 0 {
437- return
438- }
439-
440- // Specify sorting order for terms.
441- terms := []roflmarket.Term {roflmarket .TermHour , roflmarket .TermMonth , roflmarket .TermYear }
442-
443- // Go through provided payment terms and print price.
444- fmt .Printf (" Price: " )
445- var gotPrev bool
446- for _ , term := range terms {
447- price , exists := offer .Payment .Native .Terms [term ]
448- if ! exists {
449- continue
450- }
451-
452- if gotPrev {
453- fmt .Printf (" | " )
454- }
455-
456- bu := types .NewBaseUnits (price , offer .Payment .Native .Denomination )
457- bu .PrettyPrint (ctx , "" , os .Stdout )
458- fmt .Printf ("/%s" , term2str (term ))
459- gotPrev = true
460- }
461- fmt .Println ()
462- }
463- }
464-
465- // Helper to convert roflmarket term into string.
466- func term2str (term roflmarket.Term ) string {
467- switch term {
468- case roflmarket .TermHour :
469- return "hour"
470- case roflmarket .TermMonth :
471- return "month"
472- case roflmarket .TermYear :
473- return "year"
474- default :
475- return "<unknown>"
476- }
401+ return offers , err
477402}
478403
479404func resolveAndMarshalPermissions (npa * common.NPASelection , permissions map [string ][]string ) (string , error ) {
@@ -514,12 +439,12 @@ func init() {
514439 providerFlags .StringVar (& deployOffer , "offer" , "" , "set the provider's offer identifier" )
515440 providerFlags .StringVar (& deployMachine , "machine" , buildRofl .DefaultMachineName , "machine to deploy into" )
516441 providerFlags .BoolVar (& deployForce , "force" , false , "force deployment" )
517- providerFlags .BoolVar (& deployShowOffers , "show-offers" , false , "show all provider offers and quit" )
518442 providerFlags .BoolVar (& deployReplaceMachine , "replace-machine" , false , "rent a new machine if the provided one expired" )
519443
520444 deployCmd .Flags ().AddFlagSet (common .AccountFlag )
521445 deployCmd .Flags ().AddFlagSet (common .RuntimeTxFlags )
522446 deployCmd .Flags ().AddFlagSet (providerFlags )
447+ deployCmd .Flags ().AddFlagSet (roflCommon .ShowOffersFlag )
523448 deployCmd .Flags ().AddFlagSet (roflCommon .DeploymentFlags )
524449 deployCmd .Flags ().AddFlagSet (roflCommon .WipeFlags )
525450 deployCmd .Flags ().AddFlagSet (roflCommon .TermFlags )
0 commit comments