Skip to content

Commit

Permalink
feat(tool/charts): add repo cmd (#455)
Browse files Browse the repository at this point in the history
* feat(tool/charts): add repo cmd

Adds tk tool charts add-repo for adding a repository using the CLI

* doc: add-repo cmd

* feat(tool/charts): make add-repo idempotent
  • Loading branch information
sh0rez authored Dec 30, 2020
1 parent 31cda44 commit 411b929
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
23 changes: 23 additions & 0 deletions cmd/tk/toolCharts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func chartsCmd() *cli.Command {
cmd.AddCommand(
chartsInitCmd(),
chartsAddCmd(),
chartsAddRepoCmd(),
chartsVendorCmd(),
chartsConfigCmd(),
)
Expand Down Expand Up @@ -63,6 +64,28 @@ func chartsAddCmd() *cli.Command {
return cmd
}

func chartsAddRepoCmd() *cli.Command {
cmd := &cli.Command{
Use: "add-repo [NAME] [URL]",
Short: "Adds a repository to the chartfile",
Args: cli.ArgsExact(2),
}

cmd.Run = func(cmd *cli.Command, args []string) error {
c, err := loadChartfile()
if err != nil {
return err
}

return c.AddRepos(helm.Repo{
Name: args[0],
URL: args[1],
})
}

return cmd
}

func chartsConfigCmd() *cli.Command {
cmd := &cli.Command{
Use: "config",
Expand Down
13 changes: 10 additions & 3 deletions docs/docs/helm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ resource definition, you can still consume Helm resources, as described below.

Helm support is provided using the
[`github.com/grafana/jsonnet-libs/tanka-util`](https://github.com/grafana/jsonnet-libs/tree/master/tanka-util)
library. Install it with:
library. Install it with:

```bash
jb install github.com/grafana/jsonnet-libs/tanka-util
```
Expand Down Expand Up @@ -136,8 +137,14 @@ This will also call `tk tool charts vendor`, so that the `charts/` directory is

**Adding Repositories:**
By default, the `stable` repository is automatically set up for you. If you wish
to add another repository, you currently need modify `chartfile.yaml` directly
to add it:
to add another repository, you can use the `add-repo` command:

```bash
# Add the official Grafana repository
$ tk tool charts add-repo grafana https://grafana.github.io/helm-charts
```

Another way is to modify `chartfile.yaml` directly:

```diff
version: 1
Expand Down
25 changes: 24 additions & 1 deletion pkg/helm/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (c Charts) Vendor() error {

// Add adds every Chart in reqs to the Manifest after validation, and runs
// Vendor afterwards
func (c Charts) Add(reqs []string) error {
func (c *Charts) Add(reqs []string) error {
log.Printf("Adding %v Charts ...", len(reqs))

skip := func(s string, err error) {
Expand Down Expand Up @@ -183,6 +183,29 @@ func (c Charts) Add(reqs []string) error {
return c.Vendor()
}

func (c *Charts) AddRepos(repos ...Repo) error {
added := 0
for _, r := range repos {
if c.Manifest.Repositories.Has(r) {
log.Printf("Skipping %s: Already exists", r.Name)
continue
}

c.Manifest.Repositories = append(c.Manifest.Repositories, r)
}

// write out
if err := write(c.Manifest, c.ManifestFile()); err != nil {
return err
}

if added != len(repos) {
return fmt.Errorf("%v Chart(s) were skipped. Please check above logs for details", len(repos)-added)
}

return nil
}

func InitChartfile(path string) (*Charts, error) {
c := Chartfile{
Version: Version,
Expand Down
15 changes: 14 additions & 1 deletion pkg/helm/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Chartfile struct {
Version uint `json:"version"`

// Repositories to source from
Repositories []Repo `json:"repositories"`
Repositories Repos `json:"repositories"`

// Requires lists Charts expected to be present in the charts folder
Requires Requirements `json:"requires"`
Expand All @@ -43,6 +43,19 @@ type Repo struct {
Password string `json:"password,omitempty"`
}

type Repos []Repo

// Has reports whether 'repo' is already part of the repositories
func (r Repos) Has(repo Repo) bool {
for _, x := range r {
if x == repo {
return true
}
}

return false
}

// Requirement describes a single required Helm Chart.
// Both, Chart and Version are required
type Requirement struct {
Expand Down

0 comments on commit 411b929

Please sign in to comment.