1
0
mirror of synced 2026-02-06 19:05:55 +00:00

Add a simple spinner when running flux diff kustomization

If implemented, users will see a spinner run while the diff is on-going.

Signed-off-by: Soule BA <soule@weave.works>
This commit is contained in:
Soule BA
2022-02-11 17:42:48 +01:00
parent de5f00016b
commit 01f910e257
3 changed files with 46 additions and 2 deletions

View File

@@ -26,6 +26,7 @@ import (
"path/filepath"
"sort"
"strings"
"time"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2"
"github.com/fluxcd/pkg/ssa"
@@ -35,6 +36,7 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/homeport/dyff/pkg/dyff"
"github.com/lucasb-eyer/go-colorful"
"github.com/theckman/yacspin"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling"
@@ -53,6 +55,21 @@ func (b *Builder) Manager() (*ssa.ResourceManager, error) {
}
func (b *Builder) Diff() (string, bool, error) {
// Add a spiner
cfg := yacspin.Config{
Frequency: 100 * time.Millisecond,
CharSet: yacspin.CharSets[59],
Suffix: "Kustomization diffing...",
SuffixAutoColon: true,
Message: "running dry-run",
StopCharacter: "✓",
StopColors: []string{"fgGreen"},
}
spinner, err := yacspin.New(cfg)
if err != nil {
return "", false, fmt.Errorf("failed to create spinner: %w", err)
}
output := strings.Builder{}
createdOrDrifted := false
res, err := b.Build()
@@ -77,6 +94,11 @@ func (b *Builder) Diff() (string, bool, error) {
return "", createdOrDrifted, err
}
err = spinner.Start()
if err != nil {
return "", false, fmt.Errorf("failed to start spinner: %w", err)
}
var diffErrs error
// create an inventory of objects to be reconciled
newInventory := newInventory()
@@ -123,6 +145,8 @@ func (b *Builder) Diff() (string, bool, error) {
addObjectsToInventory(newInventory, change)
}
spinner.Message("processing inventory")
if b.kustomization.Spec.Prune && diffErrs == nil {
oldStatus := b.kustomization.Status.DeepCopy()
if oldStatus.Inventory != nil {
@@ -136,6 +160,11 @@ func (b *Builder) Diff() (string, bool, error) {
}
}
err = spinner.Stop()
if err != nil {
return "", createdOrDrifted, fmt.Errorf("failed to stop spinner: %w", err)
}
return output.String(), createdOrDrifted, diffErrs
}