Skip to content

Commit fccf5b6

Browse files
committed
alpha: consistent show state
1 parent 41d7974 commit fccf5b6

File tree

2 files changed

+78
-63
lines changed

2 files changed

+78
-63
lines changed

.golangci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ linters:
6565
- ineffassign
6666
- misspell
6767
- nakedret
68-
- nestif
6968
- noctx
7069
- nolintlint
7170
- revive
@@ -92,3 +91,4 @@ linters:
9291
# - gosec
9392
# - lll
9493
# - maligned
94+
# - nestif

pkg/actions/deploy.go

Lines changed: 77 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,23 @@ func (d *Deploy) checkIfDNSAreUsed(stateApps map[string]*apiv1.AppState) error {
312312
return nil
313313
}
314314

315-
func (d *Deploy) multilockPlanAndApply(ctx context.Context, state *types.StateData, partialLock, verify bool, yamlContext *client.YAMLContext) (stateDiff *statediff.Diff, shouldSave bool, acquiredLocks map[string]string, dur time.Duration, err error) {
315+
type planAndApplyResults struct {
316+
stateDiff *statediff.Diff
317+
empty, canceled bool
318+
acquiredLocks map[string]string
319+
missingLocks []string
320+
dur time.Duration
321+
}
322+
323+
func (r *planAndApplyResults) shouldSave() bool {
324+
return !r.canceled && (!r.empty || !r.stateDiff.IsEmpty())
325+
}
326+
327+
func (d *Deploy) multilockPlanAndApply(ctx context.Context, state *types.StateData, partialLock, verify bool, yamlContext *client.YAMLContext) (ret planAndApplyResults, err error) {
316328
var (
317-
locks []string
318-
missingLocks []string
319-
stateBefore *types.StateData
320-
empty, canceled bool
329+
locks []string
330+
missingLocks []string
331+
stateBefore *types.StateData
321332
)
322333

323334
// Acquire necessary acquiredLocks.
@@ -331,19 +342,19 @@ func (d *Deploy) multilockPlanAndApply(ctx context.Context, state *types.StateDa
331342
statePlugin := d.cfg.State.Plugin()
332343

333344
for {
334-
acquiredLocks, err = statePlugin.Client().AcquireLocks(ctx, d.cfg.State.Other, locks, d.opts.LockWait, yamlContext)
345+
ret.acquiredLocks, err = statePlugin.Client().AcquireLocks(ctx, d.cfg.State.Other, locks, d.opts.LockWait, yamlContext)
335346
if err != nil {
336-
return nil, false, nil, dur, err
347+
return ret, err
337348
}
338349

339-
d.log.Debugf("Acquired locks: %s\n", acquiredLocks)
350+
d.log.Debugf("Acquired locks: %s\n", ret.acquiredLocks)
340351

341352
// Build apps.
342353
if first && !d.opts.SkipBuild {
343354
err = d.buildApps(ctx, state.Apps)
344355
if err != nil {
345-
_ = releaseLocks(d.cfg, acquiredLocks)
346-
return nil, false, nil, dur, err
356+
_ = releaseLocks(d.cfg, ret.acquiredLocks)
357+
return ret, err
347358
}
348359

349360
first = false
@@ -354,14 +365,14 @@ func (d *Deploy) multilockPlanAndApply(ctx context.Context, state *types.StateDa
354365

355366
err = d.preChecks(state)
356367
if err != nil {
357-
_ = releaseLocks(d.cfg, acquiredLocks)
358-
return nil, false, nil, dur, err
368+
_ = releaseLocks(d.cfg, ret.acquiredLocks)
369+
return ret, err
359370
}
360371

361-
empty, canceled, dur, missingLocks, err = d.planAndApply(ctx, verify, state, acquiredLocks, true)
372+
ret.empty, ret.canceled, ret.dur, ret.missingLocks, err = d.planAndApply(ctx, verify, state, ret.acquiredLocks, true)
362373
if err != nil {
363-
_ = releaseLocks(d.cfg, acquiredLocks)
364-
acquiredLocks = nil
374+
_ = releaseLocks(d.cfg, ret.acquiredLocks)
375+
ret.acquiredLocks = nil
365376

366377
// Proceed to save partial work if needed.
367378
break
@@ -373,31 +384,31 @@ func (d *Deploy) multilockPlanAndApply(ctx context.Context, state *types.StateDa
373384

374385
locks = append(locks, missingLocks...)
375386

376-
err = releaseLocks(d.cfg, acquiredLocks)
387+
err = releaseLocks(d.cfg, ret.acquiredLocks)
377388
if err != nil {
378-
return nil, false, nil, dur, err
389+
return ret, err
379390
}
380391

381392
state, _, err = getState(ctx, d.cfg.State, false, d.opts.LockWait, false, yamlContext)
382393
if err != nil {
383-
return nil, false, nil, dur, err
394+
return ret, err
384395
}
385396
}
386397

387398
stateAfter := state
388399

389-
stateDiff, diffErr := statediff.New(stateBefore, stateAfter)
400+
var diffErr error
401+
402+
ret.stateDiff, diffErr = statediff.New(stateBefore, stateAfter)
390403
if diffErr != nil {
391-
return nil, false, nil, dur, merry.Errorf("computing state diff failed: %w", err)
404+
return ret, merry.Errorf("computing state diff failed: %w", err)
392405
}
393406

394-
shouldSave = !canceled && (!empty || !stateDiff.IsEmpty()) && !d.opts.SkipDiff
395-
396-
if !stateDiff.IsEmpty() {
397-
d.log.Debugf("State Diff (to apply: %t)\n%s\n", shouldSave, stateDiff)
407+
if !ret.stateDiff.IsEmpty() {
408+
d.log.Debugf("State Diff (to apply: %t)\n%s\n", ret.shouldSave, ret.stateDiff)
398409
}
399410

400-
return stateDiff, shouldSave, acquiredLocks, dur, err
411+
return ret, err
401412
}
402413

403414
func (d *Deploy) multilockRun(ctx context.Context) error {
@@ -422,24 +433,24 @@ func (d *Deploy) multilockRun(ctx context.Context) error {
422433
}
423434

424435
// Acquire locks and plan+apply.
425-
stateDiff, shouldSave, acquiredLocks, dur, err := d.multilockPlanAndApply(ctx, state, partialLock, verify, yamlContext)
436+
res, err := d.multilockPlanAndApply(ctx, state, partialLock, verify, yamlContext)
426437
if err != nil {
427438
return err
428439
}
429440

430-
if shouldSave {
441+
if res.shouldSave() && !d.opts.SkipDiff {
431442
d.log.Debugln("Saving state.")
432443

433444
var stateErr error
434445

435446
state, stateRes, stateErr = getState(context.Background(), d.cfg.State, true, stateLockWait, false, yamlContext)
436447
if stateErr != nil {
437-
_ = releaseLocks(d.cfg, acquiredLocks)
448+
_ = releaseLocks(d.cfg, res.acquiredLocks)
438449
return stateErr
439450
}
440451

441452
// These are "less important" errors.
442-
if e := stateDiff.Apply(state); err == nil {
453+
if e := res.stateDiff.Apply(state); err == nil {
443454
err = e
444455
}
445456

@@ -450,21 +461,21 @@ func (d *Deploy) multilockRun(ctx context.Context) error {
450461
if e := releaseStateLock(d.cfg, stateRes.LockInfo); err == nil {
451462
err = e
452463
}
453-
}
454464

455-
if shouldSave && err == nil {
456-
d.log.Printf("All changes applied in %s.\n", dur.Truncate(timeTruncate))
465+
if err == nil {
466+
d.log.Printf("All changes applied in %s.\n", res.dur.Truncate(timeTruncate))
467+
}
457468
}
458469

459470
// Release locks.
460-
if releaseErr := releaseLocks(d.cfg, acquiredLocks); err == nil {
471+
if releaseErr := releaseLocks(d.cfg, res.acquiredLocks); err == nil {
461472
err = releaseErr
462473
}
463474

464475
switch {
465476
case err != nil:
466477
return err
467-
case !shouldSave:
478+
case res.canceled:
468479
return nil
469480
}
470481

@@ -622,6 +633,35 @@ func (d *Deploy) prepareAppSSLMap(appStates map[string]*apiv1.AppState) (map[str
622633
return sslMap, nil
623634
}
624635

636+
func (d *Deploy) showAppState(appState *apiv1.AppState, appNameStyle, appURLStyle *pterm.Style) {
637+
app := appState.App
638+
639+
var appInfo []string
640+
641+
if app.Url != "" {
642+
appInfo = append(appInfo, pterm.Gray("URL"), appURLStyle.Sprint(app.Url))
643+
} else if appState.Dns != nil {
644+
var privateURL string
645+
646+
switch {
647+
case appState.Dns.InternalUrl != "":
648+
privateURL = appState.Dns.InternalUrl
649+
case appState.Dns.InternalIp != "":
650+
privateURL = appState.Dns.InternalIp
651+
default:
652+
return
653+
}
654+
655+
appInfo = append(appInfo, pterm.Gray("private"), appURLStyle.Sprint(privateURL))
656+
}
657+
658+
d.log.Printf("%s (%s)\n %s\n", appNameStyle.Sprint(app.Name), app.Type, strings.Join(appInfo, " "))
659+
660+
if appState.Dns.CloudUrl != "" {
661+
d.log.Printf(" %s %s\n", pterm.Gray("cloud URL"), appURLStyle.Sprint(appState.Dns.CloudUrl))
662+
}
663+
}
664+
625665
func (d *Deploy) showAppStates(appStates map[string]*apiv1.AppState, appNameStyle *pterm.Style) (allReady bool) {
626666
appURLStyle := pterm.NewStyle(pterm.FgGreen, pterm.Underscore)
627667
appFailingStyle := pterm.NewStyle(pterm.FgRed, pterm.Bold)
@@ -655,38 +695,13 @@ func (d *Deploy) showAppStates(appStates map[string]*apiv1.AppState, appNameStyl
655695
d.log.Section().Println("App Status")
656696

657697
for _, appState := range readyApps {
658-
app := appState.App
659-
660-
var appInfo []string
661-
662-
if app.Url != "" {
663-
appInfo = append(appInfo, pterm.Gray("URL"), appURLStyle.Sprint(app.Url))
664-
} else if appState.Dns != nil {
665-
var privateURL string
666-
667-
switch {
668-
case appState.Dns.InternalUrl != "":
669-
privateURL = appState.Dns.InternalUrl
670-
case appState.Dns.InternalIp != "":
671-
privateURL = appState.Dns.InternalIp
672-
default:
673-
continue
674-
}
675-
676-
appInfo = append(appInfo, pterm.Gray("private"), appURLStyle.Sprint(privateURL))
677-
}
678-
679-
d.log.Printf("%s (%s)\n %s\n", appNameStyle.Sprint(app.Name), app.Type, strings.Join(appInfo, " "))
680-
681-
if appState.Dns.CloudUrl != "" {
682-
fmt.Printf(" %s %s\n", pterm.Gray("cloud URL"), appURLStyle.Sprint(appState.Dns.CloudUrl))
683-
}
698+
d.showAppState(appState, appNameStyle, appURLStyle)
684699
}
685700

686701
for _, appState := range unreadyApps {
687-
app := appState.App
702+
d.showAppState(appState, appNameStyle, appURLStyle)
688703

689-
d.log.Printf("%s (%s) %s %s\n", appNameStyle.Sprint(app.Name), app.Type, pterm.Gray("==>"), appFailingStyle.Sprint("FAILING"))
704+
d.log.Printf(" %s %s\n", pterm.Gray("status"), appFailingStyle.Sprint("FAILING!"))
690705
d.log.Errorln(appState.Deployment.Message)
691706
}
692707
}

0 commit comments

Comments
 (0)