Repository navigation
feat(BREV-9601): Add CLI command to view Brev credit balance #410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package balance | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/brevdev/brev-cli/pkg/cmd/cmderrors" | ||
| "github.com/brevdev/brev-cli/pkg/cmdcontext" | ||
| "github.com/brevdev/brev-cli/pkg/entity" | ||
| breverrors "github.com/brevdev/brev-cli/pkg/errors" | ||
| "github.com/brevdev/brev-cli/pkg/store" | ||
| "github.com/brevdev/brev-cli/pkg/terminal" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type BalanceStore interface { | ||
| GetActiveOrganizationOrDefault() (*entity.Organization, error) | ||
| GetBillingProfile(organizationID string) (*store.BillingProfile, error) | ||
| } | ||
|
|
||
| func NewCmdBalance(t *terminal.Terminal, balanceStore BalanceStore) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Annotations: map[string]string{"organization": ""}, | ||
| Use: "balance", | ||
| DisableFlagsInUseLine: true, | ||
| Short: "Show active organization's credit balance", | ||
| Long: "Print the credit balance for the active organization.", | ||
| Example: ` | ||
| brev balance | ||
| brev balance --org myorg | ||
| `, | ||
| PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
| err := cmdcontext.InvokeParentPersistentPreRun(cmd, args) | ||
| if err != nil { | ||
| return breverrors.WrapAndTrace(err) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| Args: cmderrors.TransformToValidationError(cobra.NoArgs), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| err := RunBalance(t, balanceStore) | ||
| if err != nil { | ||
| return breverrors.WrapAndTrace(err) | ||
| } | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func RunBalance(t *terminal.Terminal, balanceStore BalanceStore) error { | ||
| org, err := balanceStore.GetActiveOrganizationOrDefault() | ||
| if err != nil { | ||
| return breverrors.WrapAndTrace(err) | ||
| } | ||
| if org == nil { | ||
| return fmt.Errorf("no orgs exist") | ||
| } | ||
|
|
||
| profile, err := balanceStore.GetBillingProfile(org.ID) | ||
| if err != nil { | ||
| return breverrors.WrapAndTrace(err) | ||
| } | ||
|
|
||
| if profile == nil || profile.CreditDetails == nil || profile.CreditDetails.RemainingCredits == nil { | ||
| return fmt.Errorf("failed to retrieve credit balance") | ||
| } | ||
|
|
||
| remainingCents := *profile.CreditDetails.RemainingCredits | ||
| dollars := float64(remainingCents) / 100.0 | ||
|
|
||
| t.Vprint(t.Green("✓ Retrieved credit balance\n")) | ||
| t.Vprintf(" Organization: %s\n", org.Name) | ||
| t.Vprintf(" ID: %s\n", org.ID) | ||
| t.Vprintf(" Credits: $%.2f\n", dollars) | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package balance | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/brevdev/brev-cli/pkg/entity" | ||
| "github.com/brevdev/brev-cli/pkg/store" | ||
| "github.com/brevdev/brev-cli/pkg/terminal" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type fakeBalanceStore struct { | ||
| org *entity.Organization | ||
| profile *store.BillingProfile | ||
| } | ||
|
|
||
| func (f fakeBalanceStore) GetActiveOrganizationOrDefault() (*entity.Organization, error) { | ||
| return f.org, nil | ||
| } | ||
|
|
||
| func (f fakeBalanceStore) GetBillingProfile(organizationID string) (*store.BillingProfile, error) { | ||
| return f.profile, nil | ||
| } | ||
|
|
||
| func TestRunBalanceUsesActiveOrgBalance(t *testing.T) { | ||
| org := &entity.Organization{ID: "org-1", Name: "test-org"} | ||
| remaining := int64(500) | ||
| profile := &store.BillingProfile{CreditDetails: &store.CreditDetails{RemainingCredits: &remaining}} | ||
| balanceStore := fakeBalanceStore{org: org, profile: profile} | ||
|
|
||
| err := RunBalance(terminal.New(), balanceStore) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, org, balanceStore.org) | ||
| assert.NotNil(t, balanceStore.profile) | ||
| assert.NotNil(t, balanceStore.profile.CreditDetails) | ||
| assert.Equal(t, remaining, *balanceStore.profile.CreditDetails.RemainingCredits) | ||
| } | ||
|
|
||
| func TestRunBalanceFailsWhenCreditBalanceMissing(t *testing.T) { | ||
| org := &entity.Organization{ID: "org-1", Name: "test-org"} | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| profile *store.BillingProfile | ||
| }{ | ||
| {name: "billing profile missing", profile: nil}, | ||
| {name: "credit details missing", profile: &store.BillingProfile{CreditDetails: nil}}, | ||
| {name: "remaining credits missing", profile: &store.BillingProfile{CreditDetails: &store.CreditDetails{RemainingCredits: nil}}}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| balanceStore := fakeBalanceStore{org: org, profile: tt.profile} | ||
|
|
||
| err := RunBalance(terminal.New(), balanceStore) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "failed to retrieve credit balance") | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this going through brev-deploy? can we go straight to dev-plane?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @patelspratik ,
We have evaluated two approaches for routing the remaining-credits lookup directly to dev-plane, as outlined in this thread.
Thanks.