Skip to content

Commit cbc71f7

Browse files
committed
cmd/rofl: Add providers show command
1 parent 61749d6 commit cbc71f7

18 files changed

+948
-35
lines changed

cmd/rofl/common/flags.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ var (
1919
// TermFlags provide the term and count setting.
2020
TermFlags *flag.FlagSet
2121

22+
// ShowOffersFlag is the flag for showing all provider offers.
23+
ShowOffersFlag *flag.FlagSet
24+
2225
// DeploymentName is the name of the ROFL app deployment.
2326
DeploymentName string
2427

@@ -33,6 +36,9 @@ var (
3336

3437
// TermCount specific the rental base unit multiplier.
3538
TermCount uint64
39+
40+
// ShowOffers controls whether to display all offers for each provider.
41+
ShowOffers bool
3642
)
3743

3844
func init() {
@@ -48,4 +54,7 @@ func init() {
4854
TermFlags = flag.NewFlagSet("", flag.ContinueOnError)
4955
TermFlags.StringVar(&Term, "term", "", "term to pay for in advance [hour, month, year]")
5056
TermFlags.Uint64Var(&TermCount, "term-count", 1, "number of terms to pay for in advance")
57+
58+
ShowOffersFlag = flag.NewFlagSet("", flag.ContinueOnError)
59+
ShowOffersFlag.BoolVar(&ShowOffers, "show-offers", false, "show all offers for each provider")
5160
}

cmd/rofl/common/term.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,43 @@ func ParseMachineTerm(term string) roflmarket.Term {
3131
return 0
3232
}
3333
}
34+
35+
// FormatTerm formats a roflmarket.Term into a human-readable string.
36+
func FormatTerm(term roflmarket.Term) string {
37+
switch term {
38+
case roflmarket.TermHour:
39+
return TermHour
40+
case roflmarket.TermMonth:
41+
return TermMonth
42+
case roflmarket.TermYear:
43+
return TermYear
44+
default:
45+
return fmt.Sprintf("<unknown: %d>", term)
46+
}
47+
}
48+
49+
// FormatTermAdjectival formats a roflmarket.Term into an adjectival form (e.g., "hourly").
50+
func FormatTermAdjectival(term roflmarket.Term) string {
51+
switch term {
52+
case roflmarket.TermHour:
53+
return "hourly"
54+
case roflmarket.TermMonth:
55+
return "monthly"
56+
case roflmarket.TermYear:
57+
return "yearly"
58+
default:
59+
return fmt.Sprintf("term_%d", term)
60+
}
61+
}
62+
63+
// FormatTeeType formats a roflmarket.TeeType into a human-readable string.
64+
func FormatTeeType(tee roflmarket.TeeType) string {
65+
switch tee {
66+
case roflmarket.TeeTypeSGX:
67+
return "sgx"
68+
case roflmarket.TeeTypeTDX:
69+
return "tdx"
70+
default:
71+
return fmt.Sprintf("<unknown: %d>", tee)
72+
}
73+
}

cmd/rofl/deploy.go

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ var (
4040
deployOffer string
4141
deployMachine string
4242
deployForce bool
43-
deployShowOffers bool
4443
deployReplaceMachine bool
4544

4645
deployCmd = &cobra.Command{
@@ -129,7 +128,7 @@ var (
129128

130129
fmt.Printf("Using provider: %s (%s)\n", machine.Provider, providerAddr)
131130

132-
if deployShowOffers {
131+
if roflCommon.ShowOffers {
133132
// Display all offers supported by the provider.
134133
showProviderOffers(ctx, npa, conn, *providerAddr)
135134
return
@@ -203,7 +202,7 @@ var (
203202
}
204203
cobra.CheckErr(totalPrice.Mul(qTermCount))
205204
tp := types.NewBaseUnits(totalPrice, offer.Payment.Native.Denomination)
206-
fmt.Printf("Selected per-%s pricing term, total price is ", term2str(term))
205+
fmt.Printf("Selected per-%s pricing term, total price is ", roflCommon.FormatTerm(term))
207206
tp.PrettyPrint(ctx, "", os.Stdout)
208207
fmt.Println(".")
209208
// Warn the user about the non-refundable rental policy before first renting.
@@ -350,7 +349,7 @@ func pushBundleToOciRepository(orcFilename string, ociRepository string) (string
350349
func detectTerm(offer *roflmarket.Offer) (term roflmarket.Term) {
351350
if offer == nil {
352351
cobra.CheckErr(fmt.Errorf("no offers exist to determine payment term"))
353-
return // Linter complains otherwise.
352+
return term // Linter complains otherwise.
354353
}
355354
if offer.Payment.Native == nil {
356355
cobra.CheckErr(fmt.Errorf("no payment terms available for offer '%s'", offer.ID))
@@ -362,7 +361,7 @@ func detectTerm(offer *roflmarket.Offer) (term roflmarket.Term) {
362361
if _, ok := offer.Payment.Native.Terms[term]; !ok {
363362
cobra.CheckErr(fmt.Errorf("term '%s' is not available for offer '%s'", roflCommon.Term, offer.ID))
364363
}
365-
return
364+
return term
366365
}
367366

368367
// Take the longest payment period.
@@ -372,20 +371,20 @@ func detectTerm(offer *roflmarket.Offer) (term roflmarket.Term) {
372371
term = t
373372
}
374373
}
375-
return
374+
return term
376375
}
377376

378377
func fetchProviderOffers(ctx context.Context, npa *common.NPASelection, conn connection.Connection, provider types.Address) (offers []*roflmarket.Offer, err error) {
379378
offers, err = conn.Runtime(npa.ParaTime).ROFLMarket.Offers(ctx, client.RoundLatest, provider)
380379
if err != nil {
381380
err = fmt.Errorf("failed to query provider: %s", err)
382-
return
381+
return offers, err
383382
}
384383
// Order offers, newer first.
385384
sort.Slice(offers, func(i, j int) bool {
386385
return bytes.Compare(offers[i].ID[:], offers[j].ID[:]) > 0
387386
})
388-
return
387+
return offers, err
389388
}
390389

391390
func showProviderOffers(ctx context.Context, npa *common.NPASelection, conn connection.Connection, provider types.Address) {
@@ -409,19 +408,9 @@ func showProviderOffer(ctx context.Context, offer *roflmarket.Offer) {
409408
name = "<unnamed>"
410409
}
411410

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-
422411
fmt.Printf("- %s [%s]\n", name, offer.ID)
423412
fmt.Printf(" TEE: %s | Memory: %d MiB | vCPUs: %d | Storage: %.2f GiB\n",
424-
tee,
413+
roflCommon.FormatTeeType(offer.Resources.TEE),
425414
offer.Resources.Memory,
426415
offer.Resources.CPUCount,
427416
float64(offer.Resources.Storage)/1024.,
@@ -455,27 +444,13 @@ func showProviderOffer(ctx context.Context, offer *roflmarket.Offer) {
455444

456445
bu := types.NewBaseUnits(price, offer.Payment.Native.Denomination)
457446
bu.PrettyPrint(ctx, "", os.Stdout)
458-
fmt.Printf("/%s", term2str(term))
447+
fmt.Printf("/%s", roflCommon.FormatTerm(term))
459448
gotPrev = true
460449
}
461450
fmt.Println()
462451
}
463452
}
464453

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-
}
477-
}
478-
479454
func resolveAndMarshalPermissions(npa *common.NPASelection, permissions map[string][]string) (string, error) {
480455
perms := make(scheduler.Permissions)
481456
for action, addresses := range permissions {
@@ -514,12 +489,12 @@ func init() {
514489
providerFlags.StringVar(&deployOffer, "offer", "", "set the provider's offer identifier")
515490
providerFlags.StringVar(&deployMachine, "machine", buildRofl.DefaultMachineName, "machine to deploy into")
516491
providerFlags.BoolVar(&deployForce, "force", false, "force deployment")
517-
providerFlags.BoolVar(&deployShowOffers, "show-offers", false, "show all provider offers and quit")
518492
providerFlags.BoolVar(&deployReplaceMachine, "replace-machine", false, "rent a new machine if the provided one expired")
519493

520494
deployCmd.Flags().AddFlagSet(common.AccountFlag)
521495
deployCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
522496
deployCmd.Flags().AddFlagSet(providerFlags)
497+
deployCmd.Flags().AddFlagSet(roflCommon.ShowOffersFlag)
523498
deployCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
524499
deployCmd.Flags().AddFlagSet(roflCommon.WipeFlags)
525500
deployCmd.Flags().AddFlagSet(roflCommon.TermFlags)

0 commit comments

Comments
 (0)