Skip to content
Snippets Groups Projects
Unverified Commit 29e3cd45 authored by Alex Clemmer's avatar Alex Clemmer Committed by GitHub
Browse files

Merge pull request #228 from jessicayuen/registry-add

Implement command `ks registry add`
parents 1c366df6 4e0b163e
No related branches found
No related tags found
No related merge requests found
......@@ -6,19 +6,28 @@ import (
"strings"
"github.com/ksonnet/ksonnet/metadata"
"github.com/ksonnet/ksonnet/pkg/kubecfg"
"github.com/ksonnet/ksonnet/utils"
"github.com/spf13/cobra"
)
const (
flagRegistryVersion = "version"
)
var regShortDesc = map[string]string{
"list": "List all registries known to the current ksonnet app.",
"describe": "Describe a ksonnet registry and the packages it contains",
"add": "Add a registry to the current ksonnet app",
}
func init() {
RootCmd.AddCommand(registryCmd)
registryCmd.AddCommand(registryListCmd)
registryCmd.AddCommand(registryDescribeCmd)
registryCmd.AddCommand(registryAddCmd)
registryAddCmd.PersistentFlags().String(flagRegistryVersion, "", "Version of the registry to add")
}
var registryCmd = &cobra.Command{
......@@ -182,3 +191,55 @@ by ` + "`<registry-name>`" + `. Specifically, it displays the following:
### Syntax
`,
}
var registryAddCmd = &cobra.Command{
Use: "add <registry-name> <registry-uri>",
Short: regShortDesc["add"],
RunE: func(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()
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")
}
name := args[0]
uri := args[1]
version, err := flags.GetString(flagRegistryVersion)
if err != nil {
return err
}
// TODO allow protocol to be specified by flag once there is greater
// support for other protocol types.
return kubecfg.NewRegistryAddCmd(name, "github", uri, version).Run()
},
Long: `
The ` + "`add`" + ` command allows custom registries to be added to your ksonnet app.
A registry is uniquely identified by its:
1. Name
2. Version
Currently, only registries supporting the GitHub protocol can be added.
All registries must specify a unique name and URI where the registry lives.
Optionally, a version can be provided. If a version is not specified, it will
default to ` + "`latest`" + `.
### Related Commands
* ` + "`ks registry list` " + `— ` + regShortDesc["list"] + `
### Syntax
`,
Example: `# Add a registry with the name 'databases' at the uri 'github.com/example'
ks registry add databases github.com/example
# Add a registry with the name 'databases' at the uri 'github.com/example' and
# the version 0.0.1
ks registry add databases github.com/example --version=0.0.1`,
}
......@@ -35,6 +35,7 @@ ks registry
### SEE ALSO
* [ks](ks.md) - Configure your application to deploy to a Kubernetes cluster
* [ks registry add](ks_registry_add.md) - Add a registry to the current ksonnet app
* [ks registry describe](ks_registry_describe.md) - Describe a ksonnet registry and the packages it contains
* [ks registry list](ks_registry_list.md) - List all registries known to the current ksonnet app.
## ks registry add
Add a registry to the current ksonnet app
### Synopsis
The `add` command allows custom registries to be added to your ksonnet app.
A registry is uniquely identified by its:
1. Name
2. Version
Currently, only registries supporting the GitHub protocol can be added.
All registries must specify a unique name and URI where the registry lives.
Optionally, a version can be provided. If a version is not specified, it will
default to `latest`.
### Related Commands
* `ks registry list` — List all registries known to the current ksonnet app.
### Syntax
```
ks registry add <registry-name> <registry-uri>
```
### Examples
```
# Add a registry with the name 'databases' at the uri 'github.com/example'
ks registry add databases github.com/example
# Add a registry with the name 'databases' at the uri 'github.com/example' and
# the version 0.0.1
ks registry add databases github.com/example --version=0.0.1
```
### Options
```
--version string Version of the registry to add
```
### Options inherited from parent commands
```
-v, --verbose count[=-1] Increase verbosity. May be given multiple times.
```
### SEE ALSO
* [ks registry](ks_registry.md) - Manage registries for current project
......@@ -351,33 +351,31 @@ func (m *manager) getOrCacheRegistry(gh registry.Manager) (*registry.Spec, error
registrySpecFile := m.registryPath(gh)
registrySpec, exists, err := m.registrySpecFromFile(registrySpecFile)
if !exists {
return nil, fmt.Errorf("Registry '%s' does not exist", gh.MakeRegistryRefSpec().Name)
} else if err != nil {
return nil, err
}
// If failed, use the protocol to try to retrieve app specification.
registrySpec, err = gh.FetchRegistrySpec()
if err != nil {
return nil, err
}
// If failed, use the protocol to try to retrieve app specification.
registrySpec, err = gh.FetchRegistrySpec()
if err != nil {
return nil, err
}
registrySpecBytes, err := registrySpec.Marshal()
if err != nil {
return nil, err
}
registrySpecBytes, err := registrySpec.Marshal()
if err != nil {
return nil, err
}
// NOTE: We call mkdir after getting the registry spec, since a
// network call might fail and leave this half-initialized empty
// directory.
registrySpecDir := appendToAbsPath(m.registriesPath, gh.RegistrySpecDir())
err = m.appFS.MkdirAll(string(registrySpecDir), defaultFolderPermissions)
if err != nil {
return nil, err
}
// NOTE: We call mkdir after getting the registry spec, since a
// network call might fail and leave this half-initialized empty
// directory.
registrySpecDir := appendToAbsPath(m.registriesPath, gh.RegistrySpecDir())
err = m.appFS.MkdirAll(string(registrySpecDir), defaultFolderPermissions)
if err != nil {
return nil, err
}
err = afero.WriteFile(m.appFS, string(registrySpecFile), registrySpecBytes, defaultFilePermissions)
if err != nil {
err = afero.WriteFile(m.appFS, string(registrySpecFile), registrySpecBytes, defaultFilePermissions)
if err != nil {
return nil, err
}
} else if err != nil {
return nil, err
}
......
// Copyright 2017 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 kubecfg
// RegistryAddCmd contains the metadata needed to create a registry.
type RegistryAddCmd struct {
name string
protocol string
uri string
version string
}
// NewRegistryAddCmd initializes a RegistryAddCmd.
func NewRegistryAddCmd(name, protocol, uri, version string) *RegistryAddCmd {
if version == "" {
version = "latest"
}
return &RegistryAddCmd{name: name, protocol: protocol, uri: uri, version: version}
}
// Run adds the registry to the ksonnet project.
func (c *RegistryAddCmd) Run() error {
manager, err := manager()
if err != nil {
return err
}
_, err = manager.AddRegistry(c.name, c.protocol, c.uri, c.version)
return err
}
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