feat: add flag to list to skip prepare
Signed-off-by: Viktor Oreshkin <imselfish@stek29.rocks>
This commit is contained in:
		
							parent
							
								
									8b0ad72e77
								
							
						
					
					
						commit
						f3788249e4
					
				|  | @ -32,6 +32,7 @@ func NewListCmd(globalCfg *config.GlobalImpl) *cobra.Command { | |||
| 
 | ||||
| 	f := cmd.Flags() | ||||
| 	f.BoolVar(&listOptions.KeepTempDir, "keep-temp-dir", false, "Keep temporary directory") | ||||
| 	f.BoolVar(&listOptions.WithPreparedCharts, "with-prepared-charts", true, "prepare charts when listing releases") | ||||
| 	f.StringVar(&listOptions.Output, "output", "", "output releases list as a json string") | ||||
| 
 | ||||
| 	return cmd | ||||
|  |  | |||
|  | @ -547,6 +547,49 @@ func (a *App) ListReleases(c ListConfigProvider) error { | |||
| 	var releases []*HelmRelease | ||||
| 
 | ||||
| 	err := a.ForEachState(func(run *Run) (_ bool, errs []error) { | ||||
| 		var stateReleases []*HelmRelease | ||||
| 		var err error | ||||
| 
 | ||||
| 		if c.WithPreparedCharts() { | ||||
| 			err = run.withPreparedCharts("list", state.ChartPrepareOptions{ | ||||
| 				SkipRepos: true, | ||||
| 				SkipDeps:  true, | ||||
| 			}, func() { | ||||
| 				rel, err := a.list(run) | ||||
| 				if err != nil { | ||||
| 					panic(err) | ||||
| 				} | ||||
| 				stateReleases = rel | ||||
| 			}) | ||||
| 		} else { | ||||
| 			stateReleases, err = a.list(run) | ||||
| 		} | ||||
| 
 | ||||
| 		if err != nil { | ||||
| 			errs = append(errs, err) | ||||
| 		} | ||||
| 
 | ||||
| 		releases = append(releases, stateReleases...) | ||||
| 
 | ||||
| 		return | ||||
| 	}, false, SetFilter(true)) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 
 | ||||
| 	if c.Output() == "json" { | ||||
| 		err = FormatAsJson(releases) | ||||
| 	} else { | ||||
| 		err = FormatAsTable(releases) | ||||
| 	} | ||||
| 
 | ||||
| 	return err | ||||
| } | ||||
| 
 | ||||
| func (a *App) list(run *Run) ([]*HelmRelease, error) { | ||||
| 	var releases []*HelmRelease | ||||
| 
 | ||||
| 	for _, r := range run.state.Releases { | ||||
| 		labels := "" | ||||
| 		if r.Labels == nil { | ||||
|  | @ -570,7 +613,7 @@ func (a *App) ListReleases(c ListConfigProvider) error { | |||
| 
 | ||||
| 		enabled, err := state.ConditionEnabled(r, run.state.Values()) | ||||
| 		if err != nil { | ||||
| 				panic(err) | ||||
| 			return nil, err | ||||
| 		} | ||||
| 
 | ||||
| 		installed := r.Installed == nil || *r.Installed | ||||
|  | @ -585,20 +628,7 @@ func (a *App) ListReleases(c ListConfigProvider) error { | |||
| 		}) | ||||
| 	} | ||||
| 
 | ||||
| 		return | ||||
| 	}, false, SetFilter(true)) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 
 | ||||
| 	if c.Output() == "json" { | ||||
| 		err = FormatAsJson(releases) | ||||
| 	} else { | ||||
| 		err = FormatAsTable(releases) | ||||
| 	} | ||||
| 
 | ||||
| 	return err | ||||
| 	return releases, nil | ||||
| } | ||||
| 
 | ||||
| func (a *App) within(dir string, do func() error) error { | ||||
|  |  | |||
|  | @ -17,7 +17,7 @@ import ( | |||
| 	"github.com/helmfile/helmfile/pkg/testutil" | ||||
| ) | ||||
| 
 | ||||
| func TestListWithEnvironment(t *testing.T) { | ||||
| func testListWithConfig(t *testing.T, cfg configImpl) { | ||||
| 	type testcase struct { | ||||
| 		environment string | ||||
| 		ns          string | ||||
|  | @ -26,7 +26,7 @@ func TestListWithEnvironment(t *testing.T) { | |||
| 		expected    string | ||||
| 	} | ||||
| 
 | ||||
| 	check := func(t *testing.T, tc testcase) { | ||||
| 	check := func(t *testing.T, tc testcase, cfg configImpl) { | ||||
| 		t.Helper() | ||||
| 
 | ||||
| 		bs := &bytes.Buffer{} | ||||
|  | @ -164,7 +164,7 @@ releases: | |||
| 
 | ||||
| 			var listErr error | ||||
| 			out := testutil.CaptureStdout(func() { | ||||
| 				listErr = app.ListReleases(configImpl{}) | ||||
| 				listErr = app.ListReleases(cfg) | ||||
| 			}) | ||||
| 
 | ||||
| 			var gotErr string | ||||
|  | @ -197,14 +197,14 @@ cache                      	my-app     	true   	true     	app:test	bitnami/redis | |||
| database                   	my-app     	true   	true     	        	bitnami/postgres	11.6.22 | ||||
| global                     	kube-system	true   	true     	        	incubator/raw   	        | ||||
| `, | ||||
| 		}) | ||||
| 		}, cfg) | ||||
| 	}) | ||||
| 
 | ||||
| 	t.Run("fail on unknown environment", func(t *testing.T) { | ||||
| 		check(t, testcase{ | ||||
| 			environment: "staging", | ||||
| 			error:       `err: no releases found that matches specified selector() and environment(staging), in any helmfile`, | ||||
| 		}) | ||||
| 		}, cfg) | ||||
| 	}) | ||||
| 
 | ||||
| 	t.Run("list releases matching selector and environment", func(t *testing.T) { | ||||
|  | @ -215,7 +215,7 @@ global                     	kube-system	true   	true     	        	incubator/raw | |||
| external-secrets	default  	true   	true     	app:test,chart:raw,name:external-secrets,namespace:default	incubator/raw	        | ||||
| my-release      	default  	true   	true     	app:test,chart:raw,name:my-release,namespace:default      	incubator/raw	        | ||||
| `, | ||||
| 		}) | ||||
| 		}, cfg) | ||||
| 	}) | ||||
| 
 | ||||
| 	t.Run("filters releases for environment used in one file only", func(t *testing.T) { | ||||
|  | @ -225,7 +225,7 @@ my-release      	default  	true   	true     	app:test,chart:raw,name:my-release, | |||
| cache   	my-app   	true   	true     	app:test	bitnami/redis   	17.0.7  | ||||
| database	my-app   	true   	true     	        	bitnami/postgres	11.6.22 | ||||
| `, | ||||
| 		}) | ||||
| 		}, cfg) | ||||
| 	}) | ||||
| 
 | ||||
| 	t.Run("filters releases for environment used in multiple files", func(t *testing.T) { | ||||
|  | @ -243,6 +243,15 @@ test3                      	           	true   	true     	        	incubator/raw | |||
| cache                      	my-app     	true   	true     	app:test	bitnami/redis   	17.0.7  | ||||
| database                   	my-app     	true   	true     	        	bitnami/postgres	11.6.22 | ||||
| `, | ||||
| 		}) | ||||
| 		}, cfg) | ||||
| 	}) | ||||
| } | ||||
| 
 | ||||
| func TestListWithEnvironment(t *testing.T) { | ||||
| 	t.Run("with prepared charts", func(t *testing.T) { | ||||
| 		testListWithConfig(t, configImpl{withPreparedCharts: true}) | ||||
| 	}) | ||||
| 	t.Run("without prepared charts", func(t *testing.T) { | ||||
| 		testListWithConfig(t, configImpl{withPreparedCharts: false}) | ||||
| 	}) | ||||
| } | ||||
|  |  | |||
|  | @ -2220,6 +2220,7 @@ type configImpl struct { | |||
| 	skipNeeds              bool | ||||
| 	includeNeeds           bool | ||||
| 	includeTransitiveNeeds bool | ||||
| 	withPreparedCharts     bool | ||||
| } | ||||
| 
 | ||||
| func (c configImpl) Selectors() []string { | ||||
|  | @ -2294,6 +2295,10 @@ func (c configImpl) Output() string { | |||
| 	return c.output | ||||
| } | ||||
| 
 | ||||
| func (c configImpl) WithPreparedCharts() bool { | ||||
| 	return c.withPreparedCharts | ||||
| } | ||||
| 
 | ||||
| type applyConfig struct { | ||||
| 	args                   string | ||||
| 	values                 []string | ||||
|  |  | |||
|  | @ -236,6 +236,7 @@ type interactive interface { | |||
| 
 | ||||
| type ListConfigProvider interface { | ||||
| 	Output() string | ||||
| 	WithPreparedCharts() bool | ||||
| } | ||||
| 
 | ||||
| type CacheConfigProvider interface{} | ||||
|  |  | |||
|  | @ -6,6 +6,8 @@ type ListOptions struct { | |||
| 	Output string | ||||
| 	// KeepTempDir is the keep temp dir flag
 | ||||
| 	KeepTempDir bool | ||||
| 	// WithPreparedCharts makes list call `withPreparedCharts` when listing
 | ||||
| 	WithPreparedCharts bool | ||||
| } | ||||
| 
 | ||||
| // NewListOptions creates a new Apply
 | ||||
|  | @ -36,3 +38,8 @@ func (c *ListImpl) Args() string { | |||
| func (c *ListImpl) Output() string { | ||||
| 	return c.ListOptions.Output | ||||
| } | ||||
| 
 | ||||
| // WithPreparedCharts returns withPreparedCharts flag
 | ||||
| func (c *ListImpl) WithPreparedCharts() bool { | ||||
| 	return c.ListOptions.WithPreparedCharts | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue