Skip to content

Commit

Permalink
rewrite accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariah committed May 27, 2021
1 parent c50de48 commit 6362ad6
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 53 deletions.
40 changes: 0 additions & 40 deletions cmd/account.go

This file was deleted.

85 changes: 85 additions & 0 deletions cmd/account/account.go
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
}
75 changes: 75 additions & 0 deletions cmd/account/account_test.go
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)
}

}
47 changes: 47 additions & 0 deletions cmd/account/printer.go
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
}
12 changes: 0 additions & 12 deletions cmd/printer/account.go

This file was deleted.

3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"os"

"github.com/vultr/vultr-cli/cmd/account"
"github.com/vultr/vultr-cli/cmd/operatingSystems"
"github.com/vultr/vultr-cli/cmd/sshkeys"

Expand Down Expand Up @@ -66,7 +67,7 @@ func init() {
viper.BindPFlag("output", rootCmd.PersistentFlags().Lookup("output"))

rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.AddCommand(accountCmd)
rootCmd.AddCommand(account.NewCmdAccount(base))
rootCmd.AddCommand(applications.NewCmdApplications(base))
rootCmd.AddCommand(Backups())
rootCmd.AddCommand(BareMetal())
Expand Down

0 comments on commit 6362ad6

Please sign in to comment.