Commit b2b9912d authored by bryanl's avatar bryanl
Browse files

extract cmd registry list into its own file


Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent 75401f37
......@@ -22,6 +22,7 @@ import (
cmocks "github.com/ksonnet/ksonnet/component/mocks"
"github.com/ksonnet/ksonnet/metadata/app/mocks"
rmocks "github.com/ksonnet/ksonnet/pkg/registry/mocks"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
)
......@@ -63,3 +64,12 @@ func mockNsWithName(name string) *cmocks.Namespace {
m.On("Name").Return(name)
return m
}
func mockRegistry(name string) *rmocks.Registry {
m := &rmocks.Registry{}
m.On("Name").Return(name)
m.On("Protocol").Return("github")
m.On("URI").Return("github.com/ksonnet/parts/tree/master/incubator")
return m
}
// Copyright 2018 The ksonnet 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 actions
import (
"io"
"os"
"github.com/ksonnet/ksonnet/metadata/app"
"github.com/ksonnet/ksonnet/pkg/registry"
"github.com/ksonnet/ksonnet/pkg/util/table"
)
// RunRegistryList runs `env list`
func RunRegistryList(ksApp app.App) error {
rl, err := NewRegistryList(ksApp)
if err != nil {
return err
}
return rl.Run()
}
// RegistryList lists available registries
type RegistryList struct {
app app.App
rm registry.Manager
out io.Writer
}
// NewRegistryList creates an instance of RegistryList
func NewRegistryList(ksApp app.App) (*RegistryList, error) {
rl := &RegistryList{
app: ksApp,
rm: registry.DefaultManager,
out: os.Stdout,
}
return rl, nil
}
// Run runs the env list action.
func (rl *RegistryList) Run() error {
registries, err := rl.rm.Registries(rl.app)
if err != nil {
return err
}
t := table.New(rl.out)
t.SetHeader([]string{"name", "protocol", "uri"})
var rows [][]string
for _, r := range registries {
rows = append(rows, []string{
r.Name(),
r.Protocol(),
r.URI(),
})
}
t.AppendBulk(rows)
return t.Render()
}
// Copyright 2018 The ksonnet 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 actions
import (
"bytes"
"testing"
amocks "github.com/ksonnet/ksonnet/metadata/app/mocks"
"github.com/ksonnet/ksonnet/pkg/registry"
rmocks "github.com/ksonnet/ksonnet/pkg/registry/mocks"
"github.com/stretchr/testify/require"
)
func TestRegistryList(t *testing.T) {
withApp(t, func(appMock *amocks.App) {
a, err := NewRegistryList(appMock)
require.NoError(t, err)
var buf bytes.Buffer
a.out = &buf
rm := &rmocks.Manager{}
a.rm = rm
registries := []registry.Registry{
mockRegistry("incubator"),
}
rm.On("Registries", appMock).Return(registries, nil)
err = a.Run()
require.NoError(t, err)
assertOutput(t, "registry/list/output.txt", buf.String())
})
}
NAME PROTOCOL URI
==== ======== ===
incubator github github.com/ksonnet/parts/tree/master/incubator
......@@ -23,7 +23,6 @@ import (
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
"github.com/ksonnet/ksonnet/pkg/util/table"
"github.com/spf13/cobra"
)
......@@ -74,62 +73,6 @@ described above. (See ` + "`ks prototype --help`" + ` for more information.)
`,
}
var registryListCmd = &cobra.Command{
Use: "list",
Short: regShortDesc["list"],
RunE: func(cmd *cobra.Command, args []string) error {
const (
nameHeader = "NAME"
protocolHeader = "PROTOCOL"
uriHeader = "URI"
)
if len(args) != 0 {
return fmt.Errorf("Command 'registry list' does not take arguments")
}
cwd, err := os.Getwd()
if err != nil {
return err
}
manager, err := metadata.Find(cwd)
if err != nil {
return err
}
app, err := manager.App()
if err != nil {
return err
}
t := table.New(os.Stdout)
t.SetHeader([]string{nameHeader, protocolHeader, uriHeader})
for name, regRef := range app.Registries() {
t.Append([]string{name, regRef.Protocol, regRef.URI})
}
t.Render()
return nil
},
Long: `
The ` + "`list`" + ` command displays all known ksonnet registries in a table. This
table includes the following info:
1. Registry name
2. Protocol (e.g. ` + "`github`" + `)
3. Registry URI
### Related Commands
* ` + "`ks registry describe` " + `— ` + regShortDesc["describe"] + `
### Syntax
`,
}
var registryDescribeCmd = &cobra.Command{
Use: "describe <registry-name>",
Short: regShortDesc["describe"],
......
// Copyright 2018 The ksonnet 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 cmd
import (
"fmt"
"github.com/ksonnet/ksonnet/actions"
"github.com/spf13/cobra"
)
var registryListCmd = &cobra.Command{
Use: "list",
Short: regShortDesc["list"],
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("Command 'registry list' does not take arguments")
}
return actions.RunRegistryList(ka)
},
Long: `
The ` + "`list`" + ` command displays all known ksonnet registries in a table. This
table includes the following info:
1. Registry name
2. Protocol (e.g. ` + "`github`" + `)
3. Registry URI
### Related Commands
* ` + "`ks registry describe` " + `— ` + regShortDesc["describe"] + `
### Syntax
`,
}
// Copyright 2018 The ksonnet 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 registry
import "github.com/ksonnet/ksonnet/metadata/app"
// GitHub is a GitHub based registry.
type GitHub struct {
name string
spec *app.RegistryRefSpec
}
// NewGitHub creates an instance of GitHub.
func NewGitHub(name string, spec *app.RegistryRefSpec) *GitHub {
return &GitHub{
name: name,
spec: spec,
}
}
var _ Registry = (*GitHub)(nil)
// Name is the registry name.
func (g *GitHub) Name() string {
return g.name
}
// Protocol is the registry protocol.
func (g *GitHub) Protocol() string {
return g.spec.Protocol
}
// URI is the registry URI.
func (g *GitHub) URI() string {
return g.spec.URI
}
// Copyright 2018 The ksonnet 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 registry
import (
"testing"
"github.com/ksonnet/ksonnet/metadata/app"
"github.com/stretchr/testify/assert"
)
func TestGithub(t *testing.T) {
spec := &app.RegistryRefSpec{
Protocol: "github",
URI: "github.com/foo/bar",
}
g := NewGitHub("incubator", spec)
assert.Equal(t, "incubator", g.Name())
assert.Equal(t, "github", g.Protocol())
assert.Equal(t, "github.com/foo/bar", g.URI())
}
// Copyright 2018 The ksonnet 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 registry
import "github.com/ksonnet/ksonnet/metadata/app"
var (
// DefaultManager is the default manager for registries.
DefaultManager = &defaultManager{}
)
// Registry is a registry.
type Registry interface {
Name() string
Protocol() string
URI() string
}
// Manager is a manager for registry related actions.
type Manager interface {
// Registries returns a list of alphabetically sorted registries. The
// registries are sorted by name.
Registries(ksApp app.App) ([]Registry, error)
}
type defaultManager struct{}
var _ Manager = (*defaultManager)(nil)
func (dm *defaultManager) Registries(ksApp app.App) ([]Registry, error) {
var registries []Registry
for name, regRef := range ksApp.Registries() {
registries = append(registries, NewGitHub(name, regRef))
}
return registries, nil
}
// Copyright 2018 The ksonnet 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 registry
import (
"testing"
"github.com/ksonnet/ksonnet/metadata/app"
"github.com/ksonnet/ksonnet/metadata/app/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_defaultManager_Registries(t *testing.T) {
dm := &defaultManager{}
specs := app.RegistryRefSpecs{
"incubator": &app.RegistryRefSpec{
Protocol: "github",
URI: "github.com/foo/bar",
},
}
appMock := &mocks.App{}
appMock.On("Registries").Return(specs)
registries, err := dm.Registries(appMock)
require.NoError(t, err)
require.Len(t, registries, 1)
r := registries[0]
assert.Equal(t, "incubator", r.Name())
assert.Equal(t, "github", r.Protocol())
assert.Equal(t, "github.com/foo/bar", r.URI())
}
// Copyright 2018 The ksonnet 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.
// Code generated by mockery v1.0.0
package mocks
import app "github.com/ksonnet/ksonnet/metadata/app"
import mock "github.com/stretchr/testify/mock"
import registry "github.com/ksonnet/ksonnet/pkg/registry"
// Manager is an autogenerated mock type for the Manager type
type Manager struct {
mock.Mock
}
// Registries provides a mock function with given fields: ksApp
func (_m *Manager) Registries(ksApp app.App) ([]registry.Registry, error) {
ret := _m.Called(ksApp)
var r0 []registry.Registry
if rf, ok := ret.Get(0).(func(app.App) []registry.Registry); ok {
r0 = rf(ksApp)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]registry.Registry)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(app.App) error); ok {
r1 = rf(ksApp)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Copyright 2018 The ksonnet 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.
// Code generated by mockery v1.0.0
package mocks
import mock "github.com/stretchr/testify/mock"
// Registry is an autogenerated mock type for the Registry type
type Registry struct {
mock.Mock
}
// Name provides a mock function with given fields:
func (_m *Registry) Name() string {
ret := _m.Called()
var r0 string
if rf, ok := ret.Get(0).(func() string); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(string)
}
return r0
}
// Protocol provides a mock function with given fields:
func (_m *Registry) Protocol() string {
ret := _m.Called()
var r0 string
if rf, ok := ret.Get(0).(func() string); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(string)
}
return r0
}
// URI provides a mock function with given fields:
func (_m *Registry) URI() string {
ret := _m.Called()
var r0 string
if rf, ok := ret.Get(0).(func() string); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(string)
}
return r0
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment