-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
11 changed files
with
277 additions
and
77 deletions.
There are no files selected for viewing
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
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
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
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
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
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,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} | ||
} | ||
} |
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,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) | ||
}) | ||
} |
Oops, something went wrong.