2019-12-30 17:52:00 +00:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-01-17 18:13:53 +00:00
|
|
|
"github.com/pelotech/drone-helm3/internal/env"
|
2019-12-30 21:24:57 +00:00
|
|
|
"strings"
|
2019-12-30 17:52:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AddRepo is an execution step that calls `helm repo add` when executed.
|
|
|
|
type AddRepo struct {
|
2020-01-17 18:13:53 +00:00
|
|
|
*config
|
2020-01-20 23:40:36 +00:00
|
|
|
repo string
|
|
|
|
certs *repoCerts
|
|
|
|
cmd cmd
|
2019-12-30 17:52:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 21:50:04 +00:00
|
|
|
// NewAddRepo creates an AddRepo for the given repo-spec. No validation is performed at this time.
|
2020-01-17 18:13:53 +00:00
|
|
|
func NewAddRepo(cfg env.Config, repo string) *AddRepo {
|
2020-01-16 21:50:04 +00:00
|
|
|
return &AddRepo{
|
2020-01-17 18:13:53 +00:00
|
|
|
config: newConfig(cfg),
|
|
|
|
repo: repo,
|
2020-01-20 23:40:36 +00:00
|
|
|
certs: newRepoCerts(cfg),
|
2020-01-16 21:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:52:00 +00:00
|
|
|
// Execute executes the `helm repo add` command.
|
2020-01-16 23:30:21 +00:00
|
|
|
func (a *AddRepo) Execute() error {
|
2019-12-30 17:52:00 +00:00
|
|
|
return a.cmd.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare gets the AddRepo ready to execute.
|
2020-01-17 18:13:53 +00:00
|
|
|
func (a *AddRepo) Prepare() error {
|
|
|
|
if a.repo == "" {
|
2019-12-30 21:24:57 +00:00
|
|
|
return fmt.Errorf("repo is required")
|
2019-12-30 17:52:00 +00:00
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
split := strings.SplitN(a.repo, "=", 2)
|
2019-12-30 21:24:57 +00:00
|
|
|
if len(split) != 2 {
|
2020-01-17 18:13:53 +00:00
|
|
|
return fmt.Errorf("bad repo spec '%s'", a.repo)
|
2019-12-30 17:52:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 23:40:36 +00:00
|
|
|
if err := a.certs.write(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-30 21:24:57 +00:00
|
|
|
name := split[0]
|
|
|
|
url := split[1]
|
|
|
|
|
2020-01-17 19:12:53 +00:00
|
|
|
args := a.globalFlags()
|
2020-01-20 17:15:35 +00:00
|
|
|
args = append(args, "repo", "add")
|
2020-01-20 23:40:36 +00:00
|
|
|
args = append(args, a.certs.flags()...)
|
2020-01-20 17:15:35 +00:00
|
|
|
args = append(args, name, url)
|
2019-12-30 17:52:00 +00:00
|
|
|
|
|
|
|
a.cmd = command(helmBin, args...)
|
2020-01-17 18:13:53 +00:00
|
|
|
a.cmd.Stdout(a.stdout)
|
|
|
|
a.cmd.Stderr(a.stderr)
|
2019-12-30 17:52:00 +00:00
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
if a.debug {
|
|
|
|
fmt.Fprintf(a.stderr, "Generated command: '%s'\n", a.cmd.String())
|
2019-12-30 17:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|