Unverified Commit 72c41b84 authored by bryanl's avatar bryanl
Browse files

allow user to override registries locally


Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent a24f4a59
...@@ -668,6 +668,6 @@ ...@@ -668,6 +668,6 @@
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "bd48a9a8d4ee540e42428541efff20b6f32922f3994d0e2ddc40c980ffd02341" inputs-digest = "6a3688925e4c4c6cc43e54ca791b705e438fad130a1c3f686d1cf9390715883a"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1
...@@ -66,11 +66,12 @@ func mockNsWithName(name string) *cmocks.Namespace { ...@@ -66,11 +66,12 @@ func mockNsWithName(name string) *cmocks.Namespace {
return m return m
} }
func mockRegistry(name string) *rmocks.Registry { func mockRegistry(name string, isOverride bool) *rmocks.Registry {
m := &rmocks.Registry{} m := &rmocks.Registry{}
m.On("Name").Return(name) m.On("Name").Return(name)
m.On("Protocol").Return(registry.ProtocolGitHub) m.On("Protocol").Return(registry.ProtocolGitHub)
m.On("URI").Return("github.com/ksonnet/parts/tree/master/incubator") m.On("URI").Return("github.com/ksonnet/parts/tree/master/incubator")
m.On("IsOverride").Return(isOverride)
return m return m
} }
...@@ -43,17 +43,17 @@ func RunPkgList(ksApp app.App) error { ...@@ -43,17 +43,17 @@ func RunPkgList(ksApp app.App) error {
// PkgList lists available registries // PkgList lists available registries
type PkgList struct { type PkgList struct {
app app.App app app.App
rm registry.Manager registryList func(ksApp app.App) ([]registry.Registry, error)
out io.Writer out io.Writer
} }
// NewPkgList creates an instance of PkgList // NewPkgList creates an instance of PkgList
func NewPkgList(ksApp app.App) (*PkgList, error) { func NewPkgList(ksApp app.App) (*PkgList, error) {
rl := &PkgList{ rl := &PkgList{
app: ksApp, app: ksApp,
rm: registry.DefaultManager, registryList: registry.List,
out: os.Stdout, out: os.Stdout,
} }
return rl, nil return rl, nil
...@@ -61,7 +61,7 @@ func NewPkgList(ksApp app.App) (*PkgList, error) { ...@@ -61,7 +61,7 @@ func NewPkgList(ksApp app.App) (*PkgList, error) {
// Run runs the env list action. // Run runs the env list action.
func (pl *PkgList) Run() error { func (pl *PkgList) Run() error {
registries, err := pl.rm.List(pl.app) registries, err := pl.registryList(pl.app)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -22,7 +22,6 @@ import ( ...@@ -22,7 +22,6 @@ import (
"github.com/ksonnet/ksonnet/metadata/app" "github.com/ksonnet/ksonnet/metadata/app"
amocks "github.com/ksonnet/ksonnet/metadata/app/mocks" amocks "github.com/ksonnet/ksonnet/metadata/app/mocks"
"github.com/ksonnet/ksonnet/pkg/registry" "github.com/ksonnet/ksonnet/pkg/registry"
rmocks "github.com/ksonnet/ksonnet/pkg/registry/mocks"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
...@@ -40,9 +39,6 @@ func TestPkgList(t *testing.T) { ...@@ -40,9 +39,6 @@ func TestPkgList(t *testing.T) {
var buf bytes.Buffer var buf bytes.Buffer
a.out = &buf a.out = &buf
rm := &rmocks.Manager{}
a.rm = rm
spec := &registry.Spec{ spec := &registry.Spec{
Libraries: registry.LibraryRefSpecs{ Libraries: registry.LibraryRefSpecs{
"lib1": &registry.LibraryRef{}, "lib1": &registry.LibraryRef{},
...@@ -50,11 +46,13 @@ func TestPkgList(t *testing.T) { ...@@ -50,11 +46,13 @@ func TestPkgList(t *testing.T) {
}, },
} }
incubator := mockRegistry("incubator") incubator := mockRegistry("incubator", false)
incubator.On("FetchRegistrySpec").Return(spec, nil) incubator.On("FetchRegistrySpec").Return(spec, nil)
registries := []registry.Registry{incubator} a.registryList = func(app.App) ([]registry.Registry, error) {
rm.On("List", appMock).Return(registries, nil) registries := []registry.Registry{incubator}
return registries, nil
}
err = a.Run() err = a.Run()
require.NoError(t, err) require.NoError(t, err)
......
...@@ -24,33 +24,34 @@ import ( ...@@ -24,33 +24,34 @@ import (
) )
// RunRegistryAdd runs `registry add` // RunRegistryAdd runs `registry add`
func RunRegistryAdd(ksApp app.App, name, uri, version string) error { func RunRegistryAdd(ksApp app.App, name, uri, version string, isOverride bool) error {
nl, err := NewRegistryAdd(ksApp, name, uri, version) ra, err := NewRegistryAdd(ksApp, name, uri, version, isOverride)
if err != nil { if err != nil {
return err return err
} }
return nl.Run() return ra.Run()
} }
// RegistryAdd lists namespaces. // RegistryAdd lists namespaces.
type RegistryAdd struct { type RegistryAdd struct {
app app.App app app.App
name string name string
uri string uri string
version string version string
isOverride bool
rm registry.Manager registryAdd func(a app.App, name, protocol, uri, version string, isOverride bool) (*registry.Spec, error)
} }
// NewRegistryAdd creates an instance of RegistryAdd. // NewRegistryAdd creates an instance of RegistryAdd.
func NewRegistryAdd(ksApp app.App, name, uri, version string) (*RegistryAdd, error) { func NewRegistryAdd(ksApp app.App, name, uri, version string, isOverride bool) (*RegistryAdd, error) {
ra := &RegistryAdd{ ra := &RegistryAdd{
app: ksApp, app: ksApp,
name: name, name: name,
uri: uri, uri: uri,
version: version, version: version,
rm: registry.DefaultManager, isOverride: isOverride,
registryAdd: registry.Add,
} }
return ra, nil return ra, nil
...@@ -59,7 +60,7 @@ func NewRegistryAdd(ksApp app.App, name, uri, version string) (*RegistryAdd, err ...@@ -59,7 +60,7 @@ func NewRegistryAdd(ksApp app.App, name, uri, version string) (*RegistryAdd, err
// Run lists namespaces. // Run lists namespaces.
func (ra *RegistryAdd) Run() error { func (ra *RegistryAdd) Run() error {
uri, protocol := ra.protocol() uri, protocol := ra.protocol()
_, err := ra.rm.Add(ra.app, ra.name, protocol, uri, ra.version) _, err := ra.registryAdd(ra.app, ra.name, protocol, uri, ra.version, ra.isOverride)
return err return err
} }
......
...@@ -18,9 +18,10 @@ package actions ...@@ -18,9 +18,10 @@ package actions
import ( import (
"testing" "testing"
"github.com/ksonnet/ksonnet/metadata/app"
amocks "github.com/ksonnet/ksonnet/metadata/app/mocks" amocks "github.com/ksonnet/ksonnet/metadata/app/mocks"
"github.com/ksonnet/ksonnet/pkg/registry" "github.com/ksonnet/ksonnet/pkg/registry"
"github.com/ksonnet/ksonnet/pkg/registry/mocks" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
...@@ -34,6 +35,7 @@ func TestRegistryAdd(t *testing.T) { ...@@ -34,6 +35,7 @@ func TestRegistryAdd(t *testing.T) {
version string version string
expectedURI string expectedURI string
protocol string protocol string
isOverride bool
}{ }{
{ {
name: "github", name: "github",
...@@ -41,6 +43,13 @@ func TestRegistryAdd(t *testing.T) { ...@@ -41,6 +43,13 @@ func TestRegistryAdd(t *testing.T) {
expectedURI: "github.com/foo/bar", expectedURI: "github.com/foo/bar",
protocol: registry.ProtocolGitHub, protocol: registry.ProtocolGitHub,
}, },
{
name: "github override",
uri: "github.com/foo/bar",
expectedURI: "github.com/foo/bar",
protocol: registry.ProtocolGitHub,
isOverride: true,
},
{ {
name: "fs", name: "fs",
uri: "/path", uri: "/path",
...@@ -57,12 +66,18 @@ func TestRegistryAdd(t *testing.T) { ...@@ -57,12 +66,18 @@ func TestRegistryAdd(t *testing.T) {
for _, tc := range cases { for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
a, err := NewRegistryAdd(appMock, name, tc.uri, tc.version) a, err := NewRegistryAdd(appMock, name, tc.uri, tc.version, tc.isOverride)
require.NoError(t, err) require.NoError(t, err)
rm := &mocks.Manager{} a.registryAdd = func(a app.App, name, protocol, uri, version string, isOverride bool) (*registry.Spec, error) {
rm.On("Add", appMock, "new", tc.protocol, tc.expectedURI, tc.version).Return(nil, nil) assert.Equal(t, "new", name)
a.rm = rm assert.Equal(t, tc.protocol, protocol)
assert.Equal(t, tc.expectedURI, uri)
assert.Equal(t, tc.version, version)
assert.Equal(t, tc.isOverride, isOverride)
return &registry.Spec{}, nil
}
err = a.Run() err = a.Run()
require.NoError(t, err) require.NoError(t, err)
......
...@@ -18,6 +18,7 @@ package actions ...@@ -18,6 +18,7 @@ package actions
import ( import (
"io" "io"
"os" "os"
"sort"
"github.com/ksonnet/ksonnet/metadata/app" "github.com/ksonnet/ksonnet/metadata/app"
"github.com/ksonnet/ksonnet/pkg/registry" "github.com/ksonnet/ksonnet/pkg/registry"
...@@ -36,17 +37,17 @@ func RunRegistryList(ksApp app.App) error { ...@@ -36,17 +37,17 @@ func RunRegistryList(ksApp app.App) error {
// RegistryList lists available registries // RegistryList lists available registries
type RegistryList struct { type RegistryList struct {
app app.App app app.App
rm registry.Manager registryList func(ksApp app.App) ([]registry.Registry, error)
out io.Writer out io.Writer
} }
// NewRegistryList creates an instance of RegistryList // NewRegistryList creates an instance of RegistryList
func NewRegistryList(ksApp app.App) (*RegistryList, error) { func NewRegistryList(ksApp app.App) (*RegistryList, error) {
rl := &RegistryList{ rl := &RegistryList{
app: ksApp, app: ksApp,
rm: registry.DefaultManager, registryList: registry.List,
out: os.Stdout, out: os.Stdout,
} }
return rl, nil return rl, nil
...@@ -54,24 +55,34 @@ func NewRegistryList(ksApp app.App) (*RegistryList, error) { ...@@ -54,24 +55,34 @@ func NewRegistryList(ksApp app.App) (*RegistryList, error) {
// Run runs the env list action. // Run runs the env list action.
func (rl *RegistryList) Run() error { func (rl *RegistryList) Run() error {
registries, err := rl.rm.List(rl.app) registries, err := rl.registryList(rl.app)
if err != nil { if err != nil {
return err return err
} }
t := table.New(rl.out) t := table.New(rl.out)
t.SetHeader([]string{"name", "protocol", "uri"}) t.SetHeader([]string{"name", "override", "protocol", "uri"})
var rows [][]string var rows [][]string
for _, r := range registries { for _, r := range registries {
override := ""
if r.IsOverride() {
override = "*"
}
rows = append(rows, []string{ rows = append(rows, []string{
r.Name(), r.Name(),
override,
r.Protocol(), r.Protocol(),
r.URI(), r.URI(),
}) })
} }
sort.Slice(rows, func(i, j int) bool {
return rows[i][0] < rows[j][0]
})
t.AppendBulk(rows) t.AppendBulk(rows)
return t.Render() return t.Render()
......
...@@ -19,9 +19,9 @@ import ( ...@@ -19,9 +19,9 @@ import (
"bytes" "bytes"
"testing" "testing"
"github.com/ksonnet/ksonnet/metadata/app"
amocks "github.com/ksonnet/ksonnet/metadata/app/mocks" amocks "github.com/ksonnet/ksonnet/metadata/app/mocks"
"github.com/ksonnet/ksonnet/pkg/registry" "github.com/ksonnet/ksonnet/pkg/registry"
rmocks "github.com/ksonnet/ksonnet/pkg/registry/mocks"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
...@@ -33,13 +33,13 @@ func TestRegistryList(t *testing.T) { ...@@ -33,13 +33,13 @@ func TestRegistryList(t *testing.T) {
var buf bytes.Buffer var buf bytes.Buffer
a.out = &buf a.out = &buf
rm := &rmocks.Manager{} a.registryList = func(app.App) ([]registry.Registry, error) {
a.rm = rm registries := []registry.Registry{
mockRegistry("override", true),
registries := []registry.Registry{ mockRegistry("incubator", false),
mockRegistry("incubator"), }
return registries, nil
} }
rm.On("List", appMock).Return(registries, nil)
err = a.Run() err = a.Run()
require.NoError(t, err) require.NoError(t, err)
......
NAME PROTOCOL URI NAME OVERRIDE PROTOCOL URI
==== ======== === ==== ======== ======== ===
incubator github github.com/ksonnet/parts/tree/master/incubator incubator github github.com/ksonnet/parts/tree/master/incubator
override * github github.com/ksonnet/parts/tree/master/incubator
...@@ -15,14 +15,17 @@ ...@@ -15,14 +15,17 @@
package cmd package cmd
var ( const (
flagEnv = "env" flagEnv = "env"
flagFilename = "filename" flagFilename = "filename"
flagIndex = "index" flagIndex = "index"
flagOutput = "output"
flagNamespace = "namespace" flagNamespace = "namespace"
flagOutput = "output"
flagOverride = "override"
flagVersion = "version"
shortFilename = "f" shortFilename = "f"
shortIndex = "i" shortIndex = "i"
shortOutput = "o" shortOutput = "o"
shortOverride = "o"
) )
...@@ -22,10 +22,6 @@ import ( ...@@ -22,10 +22,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const (
flagRegistryVersion = "version"
)
var regShortDesc = map[string]string{ var regShortDesc = map[string]string{
"list": "List all registries known to the current ksonnet app.", "list": "List all registries known to the current ksonnet app.",
"describe": "Describe a ksonnet registry and the packages it contains", "describe": "Describe a ksonnet registry and the packages it contains",
......
...@@ -18,16 +18,21 @@ package cmd ...@@ -18,16 +18,21 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/spf13/viper"
"github.com/ksonnet/ksonnet/actions" "github.com/ksonnet/ksonnet/actions"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const (
vRegistryAddVersion = "registry-add-version"
vRegistryAddOverride = "registry-add-override"
)
var registryAddCmd = &cobra.Command{ var registryAddCmd = &cobra.Command{
Use: "add <registry-name> <registry-uri>", Use: "add <registry-name> <registry-uri>",
Short: regShortDesc["add"], Short: regShortDesc["add"],
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()
if len(args) != 2 { if len(args) != 2 {
return fmt.Errorf("Command 'registry add' takes two arguments, which is the name and the repository address of the registry to add") return fmt.Errorf("Command 'registry add' takes two arguments, which is the name and the repository address of the registry to add")
} }
...@@ -35,12 +40,10 @@ var registryAddCmd = &cobra.Command{ ...@@ -35,12 +40,10 @@ var registryAddCmd = &cobra.Command{
name := args[0] name := args[0]
uri := args[1] uri := args[1]
version, err := flags.GetString(flagRegistryVersion) version := viper.GetString(vRegistryAddVersion)
if err != nil { isOverride := viper.GetBool(vRegistryAddOverride)
return err
}
return actions.RunRegistryAdd(ka, name, uri, version) return actions.RunRegistryAdd(ka, name, uri, version, isOverride)
}, },
Long: ` Long: `
...@@ -54,12 +57,18 @@ A registry is uniquely identified by its: ...@@ -54,12 +57,18 @@ A registry is uniquely identified by its:
1. Name (e.g. ` + "`incubator`" + `) 1. Name (e.g. ` + "`incubator`" + `)
2. Version (e.g. ` + "`master`" + `) 2. Version (e.g. ` + "`master`" + `)
Currently, only registries supporting the **GitHub protocol** can be added. There are two supported registry protocols: **github** and **fs**.
GitHub registries expect a path in a GitHub repository, and filesystem based
registries expect a path on the local filesystem.
During creation, all registries must specify a unique name and URI where the During creation, all registries must specify a unique name and URI where the
registry lives. Optionally, a version can be provided (e.g. the *Github branch registry lives. Optionally, a version can be provided (e.g. the *Github branch
name*). If a version is not specified, it will default to ` + "`latest`" + `. name*). If a version is not specified, it will default to ` + "`latest`" + `.
Registries can be overridden with ` + "`--override`" + `. Overridden registries
are stored in ` + "`app.override.yaml`" + ` and can be safely ignored using your
SCM configuration.
### Related Commands ### Related Commands
...@@ -79,5 +88,9 @@ ks registry add databases github.com/example/tree/master/reg --version=0.0.1`, ...@@ -79,5 +88,9 @@ ks registry add databases github.com/example/tree/master/reg --version=0.0.1`,
func init() { func init() {
registryCmd.AddCommand(registryAddCmd) registryCmd.AddCommand(registryAddCmd)
registryAddCmd.PersistentFlags().String(flagRegistryVersion, "", "Version of the registry to add") registryAddCmd.Flags().String(flagVersion, "", "Version of the registry to add")
viper.BindPFlag(vRegistryAddVersion, registryAddCmd.Flags().Lookup(flagVersion))
registryAddCmd.Flags().BoolP(flagOverride, shortOverride, false, "Store in override configuration")
viper.BindPFlag(vRegistryAddOverride, registryAddCmd.Flags().Lookup(flagOverride))
} }
...@@ -15,12 +15,18 @@ A registry is uniquely identified by its: ...@@ -15,12 +15,18 @@ A registry is uniquely identified by its:
1. Name (e.g. `incubator`) 1. Name (e.g. `incubator`)
2. Version (e.g. `master`) 2. Version (e.g. `master`)
Currently, only registries supporting the **GitHub protocol** can be added. There are two supported registry protocols: **github** and **fs**.
GitHub registries expect a path in a GitHub repository, and filesystem based
registries expect a path on the local filesystem.
During creation, all registries must specify a unique name and URI where the During creation, all registries must specify a unique name and URI where the
registry lives. Optionally, a version can be provided (e.g. the *Github branch registry lives. Optionally, a version can be provided (e.g. the *Github branch
name*). If a version is not specified, it will default to `latest`. name*). If a version is not specified, it will default to `latest`.
Registries can be overridden with `--override`. Overridden registries
are stored in `app.override.yaml` and can be safely ignored using your
SCM configuration.
### Related Commands ### Related Commands
...@@ -49,6 +55,7 @@ ks registry add databases github.com/example/tree/master/reg --version=0.0.1 ...@@ -49,6 +55,7 @@ ks registry add databases github.com/example/tree/master/reg --version=0.0.1
``` ```
-h, --help help for add -h, --help help for add
-o, --override Store in override configuration
--version string Version of the registry to add --version string Version of the registry to add
``` ```