Skip to content
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

refactor cmd package into seperate packages per-service #49

Merged
merged 2 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/ukfast
cover.out
dist/
__debug_bin
210 changes: 0 additions & 210 deletions cmd/account.go

This file was deleted.

22 changes: 22 additions & 0 deletions cmd/account/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package account

import (
"github.com/spf13/cobra"
"github.com/ukfast/cli/internal/pkg/factory"
)

func AccountRootCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "account",
Short: "Commands relating to Account service",
}

// Child root commands
cmd.AddCommand(accountContactRootCmd(f))
cmd.AddCommand(accountDetailsRootCmd(f))
cmd.AddCommand(accountCreditRootCmd(f))
cmd.AddCommand(accountInvoiceRootCmd(f))
cmd.AddCommand(accountInvoiceQueryRootCmd(f))

return cmd
}
26 changes: 13 additions & 13 deletions cmd/account_contact.go → cmd/account/account_contact.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
package cmd
package account

import (
"errors"
"fmt"
"strconv"

"github.com/spf13/cobra"
"github.com/ukfast/cli/internal/pkg/factory"
"github.com/ukfast/cli/internal/pkg/helper"
"github.com/ukfast/cli/internal/pkg/output"
"github.com/ukfast/sdk-go/pkg/service/account"
)

func accountContactRootCmd() *cobra.Command {
func accountContactRootCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "contact",
Short: "sub-commands relating to contacts",
}

// Child commands
cmd.AddCommand(accountContactListCmd())
cmd.AddCommand(accountContactShowCmd())
cmd.AddCommand(accountContactListCmd(f))
cmd.AddCommand(accountContactShowCmd(f))

return cmd
}

func accountContactListCmd() *cobra.Command {
func accountContactListCmd(f factory.ClientFactory) *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "Lists contacts",
Long: "This command lists contacts",
Example: "ukfast account contact list",
RunE: func(cmd *cobra.Command, args []string) error {
return accountContactList(getClient().AccountService(), cmd, args)
return accountContactList(f.NewClient().AccountService(), cmd, args)
},
}
}
Expand All @@ -47,11 +48,10 @@ func accountContactList(service account.AccountService, cmd *cobra.Command, args
return fmt.Errorf("Error retrieving contacts: %s", err)
}

outputAccountContacts(contacts)
return nil
return output.CommandOutput(cmd, OutputAccountContactsProvider(contacts))
}

func accountContactShowCmd() *cobra.Command {
func accountContactShowCmd(f factory.ClientFactory) *cobra.Command {
return &cobra.Command{
Use: "show <contact: id>...",
Short: "Shows a contact",
Expand All @@ -64,13 +64,13 @@ func accountContactShowCmd() *cobra.Command {

return nil
},
Run: func(cmd *cobra.Command, args []string) {
accountContactShow(getClient().AccountService(), cmd, args)
RunE: func(cmd *cobra.Command, args []string) error {
return accountContactShow(f.NewClient().AccountService(), cmd, args)
},
}
}

func accountContactShow(service account.AccountService, cmd *cobra.Command, args []string) {
func accountContactShow(service account.AccountService, cmd *cobra.Command, args []string) error {
var contacts []account.Contact
for _, arg := range args {
contactID, err := strconv.Atoi(arg)
Expand All @@ -88,5 +88,5 @@ func accountContactShow(service account.AccountService, cmd *cobra.Command, args
contacts = append(contacts, contact)
}

outputAccountContacts(contacts)
return output.CommandOutput(cmd, OutputAccountContactsProvider(contacts))
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package account

import (
"errors"
Expand All @@ -7,6 +7,7 @@ import (
gomock "github.com/golang/mock/gomock"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/ukfast/cli/internal/pkg/clierrors"
"github.com/ukfast/cli/test/mocks"
"github.com/ukfast/cli/test/test_output"
"github.com/ukfast/sdk-go/pkg/service/account"
Expand All @@ -24,21 +25,21 @@ func Test_accountContactList(t *testing.T) {
accountContactList(service, &cobra.Command{}, []string{})
})

t.Run("MalformedFlag_OutputsFatal", func(t *testing.T) {
t.Run("MalformedFlag_ReturnsError", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

service := mocks.NewMockAccountService(mockCtrl)
cmd := accountContactListCmd()
cmd := accountContactListCmd(nil)
cmd.Flags().StringArray("filter", []string{"invalidfilter"}, "")

err := accountContactList(service, cmd, []string{})

assert.NotNil(t, err)
assert.Equal(t, "Missing value for filtering", err.Error())
assert.IsType(t, &clierrors.ErrInvalidFlagValue{}, err)
})

t.Run("GetContactsError_OutputsFatal", func(t *testing.T) {
t.Run("GetContactsError_ReturnsError", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

Expand All @@ -55,13 +56,13 @@ func Test_accountContactList(t *testing.T) {

func Test_accountContactShowCmd_Args(t *testing.T) {
t.Run("ValidArgs_NoError", func(t *testing.T) {
err := accountContactShowCmd().Args(nil, []string{"123"})
err := accountContactShowCmd(nil).Args(nil, []string{"123"})

assert.Nil(t, err)
})

t.Run("InvalidArgs_Error", func(t *testing.T) {
err := accountContactShowCmd().Args(nil, []string{})
err := accountContactShowCmd(nil).Args(nil, []string{})

assert.NotNil(t, err)
assert.Equal(t, "Missing contact", err.Error())
Expand Down
Loading