-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mariah
committed
May 27, 2021
1 parent
c50de48
commit 6362ad6
Showing
6 changed files
with
209 additions
and
53 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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,85 @@ | ||
// Copyright © 2019 The Vultr-cli Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package account | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"github.com/vultr/govultr/v2" | ||
"github.com/vultr/vultr-cli/pkg/cli" | ||
) | ||
|
||
var ( | ||
accountLong = `Retrieve information about your account.` | ||
accountExample = ` | ||
# Full example | ||
vultr-cli account | ||
` | ||
) | ||
|
||
// Interface for account | ||
type AccountInterface interface { | ||
Get() (*govultr.Account, error) | ||
validate(cmd *cobra.Command, args []string) | ||
} | ||
|
||
// Options for account | ||
type Options struct { | ||
Base *cli.Base | ||
} | ||
|
||
// NewAccountOptions returns Options struct | ||
func NewAccountOptions(base *cli.Base) *Options { | ||
return &Options{Base: base} | ||
} | ||
|
||
// NewCmdAccount creates a cobra command for Account | ||
func NewCmdAccount(base *cli.Base) *cobra.Command { | ||
o := NewAccountOptions(base) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "account", | ||
Short: "get account information", | ||
Long: accountLong, | ||
Example: accountExample, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
o.validate(cmd, args) | ||
account, err := o.Get() | ||
|
||
o.Base.Printer.Display(&AccountPrinter{Account: account}, err) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func (o *Options) validate(cmd *cobra.Command, args []string) { | ||
o.Base.Printer.Output = viper.GetString("output") | ||
} | ||
|
||
// Get account information | ||
func (o *Options) Get() (*govultr.Account, error) { | ||
account, err := o.Base.Client.Account.Get(context.Background()) | ||
if err != nil { | ||
fmt.Printf("Error getting account information : %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
return account, nil | ||
} |
This file contains 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,75 @@ | ||
package account | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/vultr/vultr-cli/pkg/cli" | ||
|
||
"github.com/vultr/govultr/v2" | ||
) | ||
|
||
type mockVultrAccount struct { | ||
client *govultr.Client | ||
} | ||
|
||
func (m mockVultrAccount) Get(ctx context.Context) (*govultr.Account, error) { | ||
return &govultr.Account{ | ||
Balance: 10, | ||
PendingCharges: 100, | ||
Name: "John Smith", | ||
Email: "[email protected]", | ||
ACL: []string{"manage_users", "subscriptions", "billing"}, | ||
}, nil | ||
} | ||
|
||
func TestNewAccountOptions(t *testing.T) { | ||
accountOption := NewAccountOptions(&cli.Base{Client: &govultr.Client{Account: mockVultrAccount{nil}}}) | ||
|
||
ref := reflect.TypeOf(accountOption) | ||
if _, ok := ref.MethodByName("Get"); !ok { | ||
t.Errorf("Missing get function") | ||
} | ||
|
||
if _, ok := ref.MethodByName("validate"); ok { | ||
t.Errorf("validate isn't exported shouldn't be accessible") | ||
} | ||
|
||
aInterface := reflect.TypeOf(new(AccountInterface)).Elem() | ||
if !ref.Implements(aInterface) { | ||
t.Errorf("Options does not implement AccountInterface") | ||
} | ||
} | ||
|
||
func TestNewCmdAccount(t *testing.T) { | ||
cmd := NewCmdAccount(&cli.Base{Client: &govultr.Client{Account: mockVultrAccount{nil}}}) | ||
|
||
if cmd.Short != "get account information" { | ||
t.Errorf("invalid short") | ||
} | ||
|
||
if cmd.Use != "account" { | ||
t.Errorf("invalid account") | ||
} | ||
|
||
} | ||
|
||
func TestOptions_Get(t *testing.T) { | ||
a := NewAccountOptions(&cli.Base{Client: &govultr.Client{Account: mockVultrAccount{nil}}}) | ||
|
||
expectedAccount := &govultr.Account{ | ||
Balance: 10, | ||
PendingCharges: 100, | ||
Name: "John Smith", | ||
Email: "[email protected]", | ||
ACL: []string{"manage_users", "subscriptions", "billing"}, | ||
} | ||
|
||
account, _ := a.Get() | ||
|
||
if !reflect.DeepEqual(account, expectedAccount) { | ||
t.Errorf("OSOptions.list returned %v expected %v", account, expectedAccount) | ||
} | ||
|
||
} |
This file contains 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,47 @@ | ||
package account | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/go-yaml/yaml" | ||
"github.com/vultr/govultr/v2" | ||
) | ||
|
||
// AccountPrinter ... | ||
type AccountPrinter struct { | ||
Account *govultr.Account `json:"account"` | ||
} | ||
|
||
// JSON ... | ||
func (s *AccountPrinter) JSON() []byte { | ||
prettyJSON, err := json.MarshalIndent(s, "", " ") | ||
if err != nil { | ||
panic("move this into byte") | ||
} | ||
|
||
return prettyJSON | ||
} | ||
|
||
// Yaml ... | ||
func (s *AccountPrinter) Yaml() []byte { | ||
yam, err := yaml.Marshal(s) | ||
if err != nil { | ||
panic("move this into byte") | ||
} | ||
return yam | ||
} | ||
|
||
// Columns ... | ||
func (a *AccountPrinter) Columns() map[int][]interface{} { | ||
return map[int][]interface{}{0: {"BALANCE", "PENDING CHARGES", "LAST PAYMENT DATE", "LAST PAYMENT AMOUNT", "NAME", "EMAIL", "ACLS"}} | ||
} | ||
|
||
// Data ... | ||
func (a *AccountPrinter) Data() map[int][]interface{} { | ||
return map[int][]interface{}{0: {a.Account.Balance, a.Account.PendingCharges, a.Account.LastPaymentDate, a.Account.LastPaymentAmount, a.Account.Name, a.Account.Email, a.Account.ACL}} | ||
} | ||
|
||
// Paging ... | ||
func (a *AccountPrinter) Paging() map[int][]interface{} { | ||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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