New ๐ŸŒŠ wave for @helm

Commit e3b407b7

e3b407b7ff00Merged

Dmitriy Zhilyaev <[email protected]> ยท 2 years ago ยท 3 files ยท ~3 modified

Description

fix: refactoring

Signed-off-by: Dmitriy Zhilyaev [email protected]

Details

Commit
e3b407b7ff00
Parent
21bee7d16ff8

pkg/action/build.gomodifiedView file

@@ -32,57 +32,23 @@
3232 skipUnchanged bool
3333 }
3434
35-func (i *Build) CleanRemote(src, dest string) {
36- srcPlandir := filepath.Join(src, i.plandir)
37- destPlandir := filepath.Join(dest, i.plandir)
38-
39- err := os.RemoveAll(destPlandir)
40- if err != nil {
41- log.WithError(err).Error("failed to clean plandir")
42- }
43-
44- err = helper.MoveFile(srcPlandir, destPlandir)
45- if err != nil {
46- log.WithError(err).Error("failed to move plandir")
47- }
48-}
49-
5035 // Run is the main function for 'build' CLI command.
5136 //
5237 //nolint:funlen,gocognit,cyclop
5338 func (i *Build) Run(ctx context.Context) (err error) {
54- wd, err := os.Getwd()
55- if err != nil {
56- return fmt.Errorf("failed to get current directory: %w", err)
57- }
58-
59- // TODO: mv to func
39+ // Download Remote source
6040 if i.remoteSource != "" {
61- remoteSource, err := url.Parse(i.remoteSource)
41+ downloadPath, err := i.downloadRemoteSrc(ctx)
6242 if err != nil {
63- return fmt.Errorf("failed to parse remote source: %w", err)
43+ return err
6444 }
6545
66- downloadPath := cache.Default.GetRemoteSourcePath(remoteSource)
67- err = getter.Get(
68- downloadPath,
69- i.remoteSource,
70- getter.WithContext(ctx),
71- getter.WithDetectors(getter.Detectors),
72- getter.WithGetters(getter.Getters),
73- getter.WithDecompressors(getter.Decompressors),
74- )
46+ wd, err := os.Getwd()
7547 if err != nil {
76- return fmt.Errorf("failed to download remote source: %w", err)
48+ return fmt.Errorf("failed to get current directory: %w", err)
7749 }
7850
79- // we need to move plandir back to where it should be
80- defer i.CleanRemote(downloadPath, wd)
81-
82- err = os.Chdir(downloadPath)
83- if err != nil {
84- return fmt.Errorf("failed to chdir to downloaded remote source: %w", err)
85- }
51+ defer i.cleanRemoteSrc(downloadPath, wd)
8652 }
8753
8854 if i.autoYml {
@@ -123,31 +89,6 @@
12389 }
12490
12591 log.Info("๐Ÿ— Planfile is ready!")
126-
127- return nil
128-}
129-
130-func (i *Build) diffing(ctx context.Context, p *plan.Plan) error {
131- switch i.diffMode {
132- case DiffModeLocal:
133- oldPlan := plan.New(i.plandir)
134- if oldPlan.IsExist() {
135- log.Info("๐Ÿ†š Diff with previous local plan")
136- if err := oldPlan.Import(ctx); err != nil {
137- return err
138- }
139-
140- p.DiffPlan(oldPlan, i.diff.Options)
141- }
142-
143- case DiffModeLive:
144- log.Info("๐Ÿ†š Diff manifests in the kubernetes cluster")
145- p.DiffLive(ctx, i.diff.Options, i.diff.ThreeWayMerge)
146- case DiffModeNone:
147- log.Info("๐Ÿ†š Skip diffing")
148- default:
149- log.Warnf("๐Ÿ†šโ”Unknown %q diff mode, skipping", i.diffMode)
150- }
15192
15293 return nil
15394 }
@@ -227,3 +168,70 @@
227168
228169 return m
229170 }
171+
172+func (i *Build) cleanRemoteSrc(src, dest string) {
173+ srcPlandir := filepath.Join(src, i.plandir)
174+ destPlandir := filepath.Join(dest, i.plandir)
175+
176+ err := os.RemoveAll(destPlandir)
177+ if err != nil {
178+ log.WithError(err).Error("failed to clean plandir")
179+ }
180+
181+ err = helper.MoveFile(srcPlandir, destPlandir)
182+ if err != nil {
183+ log.WithError(err).Error("failed to move plandir")
184+ }
185+}
186+
187+func (i *Build) downloadRemoteSrc(ctx context.Context) (path string, err error) {
188+ src, err := url.Parse(i.remoteSource)
189+ if err != nil {
190+ return "", fmt.Errorf("failed to parse remote source: %w", err)
191+ }
192+
193+ path = cache.Default.GetRemoteSourcePath(src)
194+ err = getter.Get(
195+ path,
196+ i.remoteSource,
197+ getter.WithContext(ctx),
198+ getter.WithDetectors(getter.Detectors),
199+ getter.WithGetters(getter.Getters),
200+ getter.WithDecompressors(getter.Decompressors),
201+ )
202+ if err != nil {
203+ return "", fmt.Errorf("failed to download remote source: %w", err)
204+ }
205+
206+ err = os.Chdir(path)
207+ if err != nil {
208+ return "", fmt.Errorf("failed to chdir to downloaded remote source: %w", err)
209+ }
210+
211+ return
212+}
213+
214+func (i *Build) diffing(ctx context.Context, p *plan.Plan) error {
215+ switch i.diffMode {
216+ case DiffModeLocal:
217+ oldPlan := plan.New(i.plandir)
218+ if oldPlan.IsExist() {
219+ log.Info("๐Ÿ†š Diff with previous local plan")
220+ if err := oldPlan.Import(ctx); err != nil {
221+ return err
222+ }
223+
224+ p.DiffPlan(oldPlan, i.diff.Options)
225+ }
226+
227+ case DiffModeLive:
228+ log.Info("๐Ÿ†š Diff manifests in the kubernetes cluster")
229+ p.DiffLive(ctx, i.diff.Options, i.diff.ThreeWayMerge)
230+ case DiffModeNone:
231+ log.Info("๐Ÿ†š Skip diffing")
232+ default:
233+ log.Warnf("๐Ÿ†šโ”Unknown %q diff mode, skipping", i.diffMode)
234+ }
235+
236+ return nil
237+}