Skip to content

Commit

Permalink
OS-10 | Use cobra-commander (#4)
Browse files Browse the repository at this point in the history
* OS-10 | Use cobra-commander

* fix lints

* Update README.md

* GH actions

* OS-10 | Use cobra-commander
  • Loading branch information
Hakan Kurtulus authored Jun 5, 2021
1 parent 98c4b48 commit 3f9455c
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 77 deletions.
17 changes: 5 additions & 12 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/omegion/ssh-manager/internal"
"github.com/omegion/ssh-manager/internal/controller"
"github.com/omegion/ssh-manager/internal/provider"
)

Expand Down Expand Up @@ -38,11 +38,11 @@ func setupAddCommand(cmd *cobra.Command) {
}
}

// Add creates SSH key into given provider.
// Add creates Manager key into given provider.
func Add() *cobra.Command {
cmd := &cobra.Command{
Use: "add",
Short: "Add SSH key to given provider.",
Short: "Add Manager key to given provider.",
RunE: func(cmd *cobra.Command, args []string) error {
name, _ := cmd.Flags().GetString("name")
publicKeyFileName, _ := cmd.Flags().GetString("public-key")
Expand Down Expand Up @@ -73,19 +73,12 @@ func Add() *cobra.Command {
},
}

commander := internal.NewCommander()

prv, err := decideProvider(&providerName, &commander)
if err != nil {
return err
}

err = prv.Add(&item)
err = controller.NewManager(&providerName).Add(&item)
if err != nil {
return err
}

log.Infoln(fmt.Sprintf("SSH Keys saved for %s.", name))
log.Infoln(fmt.Sprintf("Manager Keys saved for %s.", name))

return nil
},
Expand Down
31 changes: 6 additions & 25 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/omegion/ssh-manager/internal"
"github.com/omegion/ssh-manager/internal/controller"
"github.com/omegion/ssh-manager/internal/io"
"github.com/omegion/ssh-manager/internal/provider"
)

// setupGetCommand sets default flags.
Expand All @@ -25,32 +24,25 @@ func setupGetCommand(cmd *cobra.Command) {
log.Fatalf("Lethal damage: %s\n\n", err)
}

cmd.Flags().Bool("read-only", false, "Do not write fetched SSH keys")
cmd.Flags().Bool("read-only", false, "Do not write fetched Manager keys")
}

// Get acquires SSH key from given provider.
// Get acquires Manager key from given provider.
func Get() *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: "Get SSH key from given provider.",
Short: "Get Manager key from given provider.",
RunE: func(cmd *cobra.Command, args []string) error {
name, _ := cmd.Flags().GetString("name")
providerName, _ := cmd.Flags().GetString("provider")
readOnly, _ := cmd.Flags().GetBool("read-only")

commander := internal.NewCommander()

prv, err := decideProvider(&providerName, &commander)
if err != nil {
return err
}

item, err := prv.Get(name)
item, err := controller.NewManager(&providerName).Get(name)
if err != nil {
return err
}

log.Infoln(fmt.Sprintf("SSH Keys are fetched for %s.", name))
log.Infoln(fmt.Sprintf("Manager Keys are fetched for %s.", name))

for _, field := range item.Values {
fileName := item.Name
Expand Down Expand Up @@ -79,14 +71,3 @@ func Get() *cobra.Command {

return cmd
}

func decideProvider(name *string, commander *internal.Commander) (provider.APIInterface, error) {
switch *name {
case provider.BitwardenCommand:
return provider.Bitwarden{Commander: *commander}, nil
case provider.OnePasswordCommand:
return provider.OnePassword{Commander: *commander}, nil
default:
return provider.Bitwarden{}, provider.NotFound{Name: name}
}
}
17 changes: 5 additions & 12 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/omegion/ssh-manager/internal"
"github.com/omegion/ssh-manager/internal/controller"
)

// setupListCommand sets default flags.
Expand All @@ -16,27 +16,20 @@ func setupListCommand(cmd *cobra.Command) {
}
}

// List acquires SSH keys from given provider.
// List acquires Manager keys from given provider.
func List() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List SSH keys from given provider.",
Short: "List Manager keys from given provider.",
RunE: func(cmd *cobra.Command, args []string) error {
providerName, _ := cmd.Flags().GetString("provider")

commander := internal.NewCommander()

prv, err := decideProvider(&providerName, &commander)
if err != nil {
return err
}

items, err := prv.List()
items, err := controller.NewManager(&providerName).List()
if err != nil {
return err
}

log.Infoln("SSH Keys are fetched.")
log.Infoln("Manager Keys are fetched.")

for _, item := range items {
log.Infoln(item.Name)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/omegion/ssh-manager
go 1.16

require (
github.com/golang/mock v1.5.0
github.com/omegion/cobra-commander v0.3.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.3
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4er
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -187,6 +189,7 @@ golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand All @@ -205,6 +208,7 @@ golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -265,7 +269,9 @@ golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=
Expand Down
44 changes: 44 additions & 0 deletions internal/controller/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package controller

import (
"github.com/omegion/ssh-manager/internal"
"github.com/omegion/ssh-manager/internal/provider"
)

// Manager is a controller for SSH providers.
type Manager struct {
Provider provider.Interface
}

// NewManager is a factory for Manager.
func NewManager(providerName *string) *Manager {
return &Manager{Provider: getProviderByName(providerName)}
}

// Add adds item to provider.
func (c Manager) Add(item *provider.Item) error {
return c.Provider.Add(item)
}

// Get gets item from provider.
func (c Manager) Get(name string) (*provider.Item, error) {
return c.Provider.Get(name)
}

// List lists items from provider.
func (c Manager) List() ([]*provider.Item, error) {
return c.Provider.List()
}

func getProviderByName(name *string) provider.Interface {
commander := internal.NewCommander()

switch *name {
case provider.BitwardenCommand:
return provider.Bitwarden{Commander: commander}
case provider.OnePasswordCommand:
return provider.OnePassword{Commander: commander}
default:
return provider.Bitwarden{Commander: commander}
}
}
77 changes: 77 additions & 0 deletions internal/controller/manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package controller

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/omegion/ssh-manager/internal/provider"
"github.com/omegion/ssh-manager/internal/provider/mocks"
)

func TestNewManager(t *testing.T) {
t.Run("bw provider", func(t *testing.T) {
providerName := provider.BitwardenCommand
manager := NewManager(&providerName)

assert.Equal(t, providerName, manager.Provider.GetName())
})

t.Run("op provider", func(t *testing.T) {
providerName := provider.OnePasswordCommand
manager := NewManager(&providerName)

assert.Equal(t, providerName, manager.Provider.GetName())
})

t.Run("unknown provider", func(t *testing.T) {
providerName := "unknown"
manager := NewManager(&providerName)

assert.Equal(t, provider.BitwardenCommand, manager.Provider.GetName())
})
}

func TestManager(t *testing.T) {
ctrl := gomock.NewController(t)
prvMock := mocks.NewMockInterface(ctrl)

expectedItem := provider.Item{}

var expectedItems []*provider.Item

t.Run("add", func(t *testing.T) {
prvMock.EXPECT().Add(&expectedItem).Return(nil)

manager := Manager{Provider: prvMock}

err := manager.Add(&expectedItem)

assert.NoError(t, err)
})

t.Run("get", func(t *testing.T) {
prvMock.EXPECT().Get(gomock.Any()).Return(&expectedItem, nil)

manager := Manager{Provider: prvMock}

item, err := manager.Get("test")

assert.NoError(t, err)
assert.Equal(t, &expectedItem, item)
})

t.Run("get", func(t *testing.T) {
expectedItems = append(expectedItems, &provider.Item{Name: "test"})

prvMock.EXPECT().List().Return(expectedItems, nil)

manager := Manager{Provider: prvMock}

items, err := manager.List()

assert.NoError(t, err)
assert.Len(t, items, 1)
})
}
Loading

0 comments on commit 3f9455c

Please sign in to comment.