diff --git a/app/cmd/install.go b/app/cmd/install.go index 8bd5b88..e08a61d 100644 --- a/app/cmd/install.go +++ b/app/cmd/install.go @@ -72,7 +72,7 @@ func addInstallFlags(flags *flag.FlagSet) { func install(cmd *cobra.Command, args []string) { fmt.Println("Installing charts...") - configuration, err := config.LoadConfiguration(cfgFile, cmd) + configuration, err := config.LoadConfiguration(cfgFile, cmd, true) if err != nil { fmt.Printf("Error loading configuration: %s\n", err) os.Exit(1) diff --git a/app/cmd/lint.go b/app/cmd/lint.go index ebb8d2e..625515e 100644 --- a/app/cmd/lint.go +++ b/app/cmd/lint.go @@ -75,7 +75,7 @@ func addLintFlags(flags *flag.FlagSet) { func lint(cmd *cobra.Command, args []string) { fmt.Println("Linting charts...") - configuration, err := config.LoadConfiguration(cfgFile, cmd) + configuration, err := config.LoadConfiguration(cfgFile, cmd, true) if err != nil { fmt.Printf("Error loading configuration: %s\n", err) os.Exit(1) diff --git a/app/cmd/lintAndInstall.go b/app/cmd/lintAndInstall.go index 08c694d..c8fd3d4 100644 --- a/app/cmd/lintAndInstall.go +++ b/app/cmd/lintAndInstall.go @@ -43,7 +43,7 @@ func newLintAndInstallCmd() *cobra.Command { func lintAndInstall(cmd *cobra.Command, args []string) { fmt.Println("Linting and installing charts...") - configuration, err := config.LoadConfiguration(cfgFile, cmd) + configuration, err := config.LoadConfiguration(cfgFile, cmd, true) if err != nil { fmt.Printf("Error loading configuration: %s\n", err) os.Exit(1) diff --git a/app/cmd/listChanged.go b/app/cmd/listChanged.go new file mode 100644 index 0000000..ebe3a11 --- /dev/null +++ b/app/cmd/listChanged.go @@ -0,0 +1,59 @@ +// Copyright The Helm 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/MakeNowJust/heredoc" + "os" + + "github.com/helm/chart-testing/pkg/chart" + "github.com/helm/chart-testing/pkg/config" + "github.com/spf13/cobra" +) + +func newListChangedCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "list-changed", + Aliases: []string{"ls-changed", "lsc"}, + Short: "List changed charts", + Long: heredoc.Doc(` + "List changed charts based on configured charts directories, + "remote, and target branch`), + Run: listChanged, + } + + flags := cmd.Flags() + addCommonFlags(flags) + return cmd +} + +func listChanged(cmd *cobra.Command, args []string) { + configuration, err := config.LoadConfiguration(cfgFile, cmd, false) + if err != nil { + fmt.Printf("Error loading configuration: %s\n", err) + os.Exit(1) + } + + testing := chart.NewTesting(*configuration) + chartDirs, err := testing.ComputeChangedChartDirectories() + if err != nil { + os.Exit(1) + } + + for _, dir := range chartDirs { + fmt.Println(dir) + } +} diff --git a/app/cmd/root.go b/app/cmd/root.go index 455bf68..d7a023c 100644 --- a/app/cmd/root.go +++ b/app/cmd/root.go @@ -44,6 +44,7 @@ func NewRootCmd() *cobra.Command { cmd.AddCommand(newLintCmd()) cmd.AddCommand(newInstallCmd()) cmd.AddCommand(newLintAndInstallCmd()) + cmd.AddCommand(newListChangedCmd()) cmd.AddCommand(newVersionCmd()) cmd.AddCommand(newGenerateDocsCmd()) @@ -58,10 +59,20 @@ func Execute() { } } -func addCommonLintAndInstallFlags(flags *pflag.FlagSet) { +func addCommonFlags(flags *pflag.FlagSet) { flags.StringVar(&cfgFile, "config", "", "Config file") flags.String("remote", "origin", "The name of the Git remote used to identify changed charts") flags.String("target-branch", "master", "The name of the target branch used to identify changed charts") + flags.StringSlice("chart-dirs", []string{"charts"}, heredoc.Doc(` + Directories containing Helm charts. May be specified multiple times + or separate values with commas`)) + flags.StringSlice("excluded-charts", []string{}, heredoc.Doc(` + Charts that should be skipped. May be specified multiple times + or separate values with commas`)) +} + +func addCommonLintAndInstallFlags(flags *pflag.FlagSet) { + addCommonFlags(flags) flags.Bool("all", false, heredoc.Doc(` Process all charts except those explicitly excluded. Disables changed charts detection and version increment checking`)) @@ -69,9 +80,6 @@ func addCommonLintAndInstallFlags(flags *pflag.FlagSet) { Specific charts to test. Disables changed charts detection and version increment checking. May be specified multiple times or separate values with commas`)) - flags.StringSlice("chart-dirs", []string{"charts"}, heredoc.Doc(` - Directories containing Helm charts. May be specified multiple times - or separate values with commas`)) flags.StringSlice("chart-repos", []string{}, heredoc.Doc(` Additional chart repositories for dependency resolutions. Repositories should be formatted as 'name=url' (ex: local=http://127.0.0.1:8879/charts). @@ -81,9 +89,6 @@ func addCommonLintAndInstallFlags(flags *pflag.FlagSet) { specified on a per-repo basis with an equals sign as delimiter (e.g. 'myrepo=--username test --password secret'). May be specified multiple times or separate values with commas`)) - flags.StringSlice("excluded-charts", []string{}, heredoc.Doc(` - Charts that should be skipped. May be specified multiple times - or separate values with commas`)) flags.Bool("debug", false, heredoc.Doc(` Print CLI calls of external tools to stdout (Note: depending on helm-extra-args passed, this may reveal sensitive data)`)) diff --git a/doc/ct.md b/doc/ct.md index 690ca0e..8759f07 100644 --- a/doc/ct.md +++ b/doc/ct.md @@ -23,6 +23,7 @@ in given chart directories. * [ct install](ct_install.md) - Install and test a chart * [ct lint](ct_lint.md) - Lint and validate a chart * [ct lint-and-install](ct_lint-and-install.md) - Lint, install, and test a chart +* [ct list-changed](ct_list-changed.md) - List changed charts * [ct version](ct_version.md) - Print version information -###### Auto generated by spf13/cobra on 18-Jan-2019 +###### Auto generated by spf13/cobra on 31-Jan-2019 diff --git a/doc/ct_install.md b/doc/ct_install.md index b1d8760..b88f9d1 100644 --- a/doc/ct_install.md +++ b/doc/ct_install.md @@ -62,4 +62,4 @@ ct install [flags] * [ct](ct.md) - The Helm chart testing tool -###### Auto generated by spf13/cobra on 18-Jan-2019 +###### Auto generated by spf13/cobra on 31-Jan-2019 diff --git a/doc/ct_lint-and-install.md b/doc/ct_lint-and-install.md index 655e072..7001bdf 100644 --- a/doc/ct_lint-and-install.md +++ b/doc/ct_lint-and-install.md @@ -61,4 +61,4 @@ ct lint-and-install [flags] * [ct](ct.md) - The Helm chart testing tool -###### Auto generated by spf13/cobra on 18-Jan-2019 +###### Auto generated by spf13/cobra on 31-Jan-2019 diff --git a/doc/ct_lint.md b/doc/ct_lint.md index feee273..e5bf868 100644 --- a/doc/ct_lint.md +++ b/doc/ct_lint.md @@ -65,4 +65,4 @@ ct lint [flags] * [ct](ct.md) - The Helm chart testing tool -###### Auto generated by spf13/cobra on 18-Jan-2019 +###### Auto generated by spf13/cobra on 31-Jan-2019 diff --git a/doc/ct_list-changed.md b/doc/ct_list-changed.md new file mode 100644 index 0000000..76d0bad --- /dev/null +++ b/doc/ct_list-changed.md @@ -0,0 +1,31 @@ +## ct list-changed + +List changed charts + +### Synopsis + +"List changed charts based on configured charts directories, +"remote, and target branch + +``` +ct list-changed [flags] +``` + +### Options + +``` + --chart-dirs strings Directories containing Helm charts. May be specified multiple times + or separate values with commas (default [charts]) + --config string Config file + --excluded-charts strings Charts that should be skipped. May be specified multiple times + or separate values with commas + -h, --help help for list-changed + --remote string The name of the Git remote used to identify changed charts (default "origin") + --target-branch string The name of the target branch used to identify changed charts (default "master") +``` + +### SEE ALSO + +* [ct](ct.md) - The Helm chart testing tool + +###### Auto generated by spf13/cobra on 31-Jan-2019 diff --git a/doc/ct_version.md b/doc/ct_version.md index b4e1059..fd908ea 100644 --- a/doc/ct_version.md +++ b/doc/ct_version.md @@ -20,4 +20,4 @@ ct version [flags] * [ct](ct.md) - The Helm chart testing tool -###### Auto generated by spf13/cobra on 18-Jan-2019 +###### Auto generated by spf13/cobra on 31-Jan-2019 diff --git a/pkg/config/config.go b/pkg/config/config.go index ea9e966..bfd2558 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -60,7 +60,7 @@ type Configuration struct { ReleaseLabel string `mapstructure:"release-label"` } -func LoadConfiguration(cfgFile string, cmd *cobra.Command) (*Configuration, error) { +func LoadConfiguration(cfgFile string, cmd *cobra.Command, printConfig bool) (*Configuration, error) { v := viper.New() cmd.Flags().VisitAll(func(flag *flag.Flag) { @@ -92,7 +92,9 @@ func LoadConfiguration(cfgFile string, cmd *cobra.Command) (*Configuration, erro return nil, errors.Wrap(err, "Error loading config file") } } else { - fmt.Println("Using config file: ", v.ConfigFileUsed()) + if printConfig { + fmt.Println("Using config file: ", v.ConfigFileUsed()) + } } cfg := &Configuration{} @@ -134,7 +136,9 @@ func LoadConfiguration(cfgFile string, cmd *cobra.Command) (*Configuration, erro cfg.CheckVersionIncrement = false } - printCfg(cfg) + if printConfig { + printCfg(cfg) + } return cfg, nil } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index fd813b7..2fbdf98 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -30,7 +30,7 @@ func TestUnmarshalJson(t *testing.T) { } func loadAndAssertConfigFromFile(t *testing.T, configFile string) { - cfg, _ := LoadConfiguration(configFile, &cobra.Command{}) + cfg, _ := LoadConfiguration(configFile, &cobra.Command{}, true) require.Equal(t, "origin", cfg.Remote) require.Equal(t, "master", cfg.TargetBranch)