Skip to content

Commit 9dc7f63

Browse files
committed
cmd/rofl: Move term and termCount params to roflCommon
1 parent 850aae0 commit 9dc7f63

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

cmd/rofl/common/flags.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ var (
1616
// NoUpdateFlag is the flag for disabling the rofl.yaml manifest file update.
1717
NoUpdateFlag *flag.FlagSet
1818

19+
// TermFlags provide the term and count setting.
20+
TermFlags *flag.FlagSet
21+
1922
// DeploymentName is the name of the ROFL app deployment.
2023
DeploymentName string
2124

@@ -24,6 +27,12 @@ var (
2427

2528
// NoUpdate disables updating the rofl.yaml manifest file.
2629
NoUpdate bool
30+
31+
// Term specifies the rental base unit.
32+
Term string
33+
34+
// TermCount specific the rental base unit multiplier.
35+
TermCount uint64
2736
)
2837

2938
func init() {
@@ -35,4 +44,8 @@ func init() {
3544

3645
NoUpdateFlag = flag.NewFlagSet("", flag.ContinueOnError)
3746
NoUpdateFlag.BoolVar(&NoUpdate, "no-update-manifest", false, "do not update the manifest")
47+
48+
TermFlags = flag.NewFlagSet("", flag.ContinueOnError)
49+
TermFlags.StringVar(&Term, "term", "", "term to pay for in advance [hour, month, year]")
50+
TermFlags.Uint64Var(&TermCount, "term-count", 1, "number of terms to pay for in advance")
3851
}

cmd/rofl/deploy.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ var (
3838
deployProvider string
3939
deployOffer string
4040
deployMachine string
41-
deployTerm string
42-
deployTermCount uint64
4341
deployForce bool
4442
deployShowOffers bool
4543
deployReplaceMachine bool
@@ -175,12 +173,12 @@ var (
175173
showProviderOffer(ctx, offer)
176174

177175
term := detectTerm(offer)
178-
if deployTermCount < 1 {
176+
if roflCommon.TermCount < 1 {
179177
return nil, nil, fmt.Errorf("number of terms must be at least 1")
180178
}
181179

182180
// Calculate total price.
183-
qTermCount := quantity.NewFromUint64(deployTermCount)
181+
qTermCount := quantity.NewFromUint64(roflCommon.TermCount)
184182
totalPrice, ok := offer.Payment.Native.Terms[term]
185183
if !ok {
186184
cobra.CheckErr("internal error: previously selected term not found in offer terms")
@@ -197,7 +195,7 @@ var (
197195
Offer: offer.ID,
198196
Deployment: &machineDeployment,
199197
Term: term,
200-
TermCount: deployTermCount,
198+
TermCount: roflCommon.TermCount,
201199
})
202200

203201
var sigTx, meta any
@@ -334,11 +332,11 @@ func detectTerm(offer *roflmarket.Offer) (term roflmarket.Term) {
334332
cobra.CheckErr(fmt.Errorf("no payment terms available for offer '%s'", offer.ID))
335333
}
336334

337-
if deployTerm != "" {
335+
if roflCommon.Term != "" {
338336
// Custom deploy term.
339-
term = roflCommon.ParseMachineTerm(deployTerm)
337+
term = roflCommon.ParseMachineTerm(roflCommon.Term)
340338
if _, ok := offer.Payment.Native.Terms[term]; !ok {
341-
cobra.CheckErr(fmt.Errorf("term '%s' is not available for offer '%s'", deployTerm, offer.ID))
339+
cobra.CheckErr(fmt.Errorf("term '%s' is not available for offer '%s'", roflCommon.Term, offer.ID))
342340
}
343341
return
344342
}
@@ -454,8 +452,6 @@ func init() {
454452
providerFlags.StringVar(&deployProvider, "provider", "", "set the provider address")
455453
providerFlags.StringVar(&deployOffer, "offer", "", "set the provider's offer identifier")
456454
providerFlags.StringVar(&deployMachine, "machine", buildRofl.DefaultMachineName, "machine to deploy into")
457-
providerFlags.StringVar(&deployTerm, "term", "", "term to pay for in advance")
458-
providerFlags.Uint64Var(&deployTermCount, "term-count", 1, "number of terms to pay for in advance")
459455
providerFlags.BoolVar(&deployForce, "force", false, "force deployment")
460456
providerFlags.BoolVar(&deployShowOffers, "show-offers", false, "show all provider offers and quit")
461457
providerFlags.BoolVar(&deployReplaceMachine, "replace-machine", false, "rent a new machine if the provided one expired")
@@ -464,4 +460,5 @@ func init() {
464460
deployCmd.Flags().AddFlagSet(providerFlags)
465461
deployCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
466462
deployCmd.Flags().AddFlagSet(roflCommon.WipeFlags)
463+
deployCmd.Flags().AddFlagSet(roflCommon.TermFlags)
467464
}

cmd/rofl/machine/mgmt.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/spf13/cobra"
9-
flag "github.com/spf13/pflag"
109

1110
"github.com/oasisprotocol/oasis-core/go/common/cbor"
1211
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
@@ -22,9 +21,6 @@ import (
2221
)
2322

2423
var (
25-
topUpTerm string
26-
topUpTermCount uint64
27-
2824
showCmd = &cobra.Command{
2925
Use: "show [<machine-name>]",
3026
Short: "Show information about a machine",
@@ -262,13 +258,16 @@ var (
262258
// Resolve provider address.
263259
providerAddr, _, err := common.ResolveLocalAccountOrAddress(npa.Network, machine.Provider)
264260
if err != nil {
265-
cobra.CheckErr(fmt.Sprintf("Invalid provider address: %s", err))
261+
cobra.CheckErr(fmt.Sprintf("invalid provider address: %s", err))
266262
}
267263

268264
// Parse machine payment term.
269-
term := roflCommon.ParseMachineTerm(topUpTerm)
270-
if topUpTermCount < 1 {
271-
cobra.CheckErr("Number of terms must be at least 1.")
265+
if roflCommon.Term == "" {
266+
cobra.CheckErr("no term period specified. Use --term to specify it")
267+
}
268+
term := roflCommon.ParseMachineTerm(roflCommon.Term)
269+
if roflCommon.TermCount < 1 {
270+
cobra.CheckErr("number of terms must be at least 1.")
272271
}
273272

274273
// When not in offline mode, connect to the given network endpoint.
@@ -281,14 +280,14 @@ var (
281280

282281
fmt.Printf("Using provider: %s (%s)\n", machine.Provider, providerAddr)
283282
fmt.Printf("Top-up machine: %s [%s]\n", machineName, machine.ID)
284-
fmt.Printf("Top-up term: %d x %s\n", topUpTermCount, topUpTerm)
283+
fmt.Printf("Top-up term: %d x %s\n", roflCommon.TermCount, roflCommon.Term)
285284

286285
// Prepare transaction.
287286
tx := roflmarket.NewInstanceTopUpTx(nil, &roflmarket.InstanceTopUp{
288287
Provider: *providerAddr,
289288
ID: machineID,
290289
Term: term,
291-
TermCount: topUpTermCount,
290+
TermCount: roflCommon.TermCount,
292291
})
293292

294293
acc := common.LoadAccount(cliConfig.Global(), npa.AccountName)
@@ -402,10 +401,6 @@ func showCommandArgs[V any](npa *common.NPASelection, raw []byte, args V) {
402401
}
403402

404403
func init() {
405-
topUpFlags := flag.NewFlagSet("", flag.ContinueOnError)
406-
topUpFlags.StringVar(&topUpTerm, "term", roflCommon.TermMonth, "term to pay for in advance")
407-
topUpFlags.Uint64Var(&topUpTermCount, "term-count", 1, "number of terms to pay for in advance")
408-
409404
showCmd.Flags().AddFlagSet(common.SelectorFlags)
410405
showCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
411406

@@ -426,5 +421,5 @@ func init() {
426421
topUpCmd.Flags().AddFlagSet(common.SelectorFlags)
427422
topUpCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
428423
topUpCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
429-
topUpCmd.Flags().AddFlagSet(topUpFlags)
424+
topUpCmd.Flags().AddFlagSet(roflCommon.TermFlags)
430425
}

0 commit comments

Comments
 (0)