This is an automated email from the ASF dual-hosted git repository.

zhongxjian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git


The following commit(s) were added to refs/heads/master by this push:
     new c0edeca7 [operator] Update progress information logic (#517)
c0edeca7 is described below

commit c0edeca7875e466e2a96299783d09e23ad6d303b
Author: mfordjody <[email protected]>
AuthorDate: Mon Dec 2 15:09:23 2024 +0800

    [operator] Update progress information logic (#517)
---
 go.mod                                 |  8 ++--
 go.sum                                 |  4 ++
 operator/pkg/util/progress/progress.go | 85 ++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 3 deletions(-)

diff --git a/go.mod b/go.mod
index 74b59586..65f13727 100644
--- a/go.mod
+++ b/go.mod
@@ -28,6 +28,7 @@ require (
        github.com/bakito/go-log-logr-adapter v0.0.2
        github.com/buildpacks/imgutil v0.0.0-20240605145725-186f89b2d168
        github.com/buildpacks/pack v0.34.2
+       github.com/cheggaaa/pb/v3 v3.1.5
        github.com/containers/image/v5 v5.27.0
        github.com/containers/storage v1.48.0
        github.com/distribution/reference v0.6.0
@@ -41,6 +42,7 @@ require (
        github.com/envoyproxy/go-control-plane v0.12.0
        github.com/envoyproxy/protoc-gen-validate v1.0.4
        github.com/evanphx/json-patch/v5 v5.9.0
+       github.com/fatih/color v1.16.0
        github.com/fullstorydev/grpcurl v1.9.1
        github.com/gin-gonic/gin v1.9.1
        github.com/go-co-op/gocron v1.9.0
@@ -58,6 +60,7 @@ require (
        github.com/google/uuid v1.6.0
        github.com/google/yamlfmt v0.9.0
        github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
+       github.com/hashicorp/go-multierror v1.1.1
        github.com/heroku/color v0.0.6
        github.com/hoisie/mustache v0.0.0-20160804235033-6375acf62c69
        github.com/jhump/protoreflect v1.16.0
@@ -100,6 +103,7 @@ require (
        k8s.io/apiextensions-apiserver v0.30.3
        k8s.io/apimachinery v0.30.3
        k8s.io/client-go v0.30.3
+       k8s.io/klog/v2 v2.120.1
        k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340
        k8s.io/kubectl v0.29.2
        k8s.io/utils v0.0.0-20230726121419-3b25d923346b
@@ -132,6 +136,7 @@ require (
        github.com/ProtonMail/go-crypto v1.0.0 // indirect
        github.com/RageCage64/multilinediff v0.2.0 // indirect
        github.com/RoaringBitmap/roaring v1.2.3 // indirect
+       github.com/VividCortex/ewma v1.2.0 // indirect
        github.com/Workiva/go-datastructures v1.0.52 // indirect
        github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5 // 
indirect
        github.com/agext/levenshtein v1.2.3 // indirect
@@ -196,7 +201,6 @@ require (
        github.com/emirpasic/gods v1.18.1 // indirect
        github.com/evanphx/json-patch v5.7.0+incompatible // indirect
        github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // 
indirect
-       github.com/fatih/color v1.16.0 // indirect
        github.com/felixge/httpsnoop v1.0.4 // indirect
        github.com/fsnotify/fsnotify v1.7.0 // indirect
        github.com/gabriel-vasile/mimetype v1.4.2 // indirect
@@ -234,7 +238,6 @@ require (
        github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // 
indirect
        github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 // indirect
        github.com/hashicorp/errwrap v1.1.0 // indirect
-       github.com/hashicorp/go-multierror v1.1.1 // indirect
        github.com/hashicorp/golang-lru v1.0.2 // indirect
        github.com/hashicorp/hcl v1.0.0 // indirect
        github.com/hashicorp/vault/sdk v0.7.0 // indirect
@@ -375,7 +378,6 @@ require (
        k8s.io/apiserver v0.30.3 // indirect
        k8s.io/cli-runtime v0.30.3 // indirect
        k8s.io/component-base v0.30.3 // indirect
-       k8s.io/klog/v2 v2.120.1 // indirect
        oras.land/oras-go v1.2.5 // indirect
        sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
        sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // 
indirect
diff --git a/go.sum b/go.sum
index 27760de5..f475f1a8 100644
--- a/go.sum
+++ b/go.sum
@@ -114,6 +114,8 @@ github.com/Shopify/sarama v1.30.0/go.mod 
h1:zujlQQx1kzHsh4jfV1USnptCQrHAEZ2Hk8fT
 github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod 
h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
 github.com/Shopify/toxiproxy/v2 v2.1.6-0.20210914104332-15ea381dcdae/go.mod 
h1:/cvHQkZ1fst0EmZnA5dFtiQdWCNCFYzb+uE2vqVgvx0=
 github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod 
h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
+github.com/VividCortex/ewma v1.2.0 
h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
+github.com/VividCortex/ewma v1.2.0/go.mod 
h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
 github.com/VividCortex/gohistogram v1.0.0/go.mod 
h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
 github.com/Workiva/go-datastructures v1.0.52 
h1:PLSK6pwn8mYdaoaCZEMsXBpBotr4HHn9abU0yMQt0NI=
 github.com/Workiva/go-datastructures v1.0.52/go.mod 
h1:Z+F2Rca0qCsVYDS8z7bAGm8f3UkzuWYS/oBZz5a7VVA=
@@ -261,6 +263,8 @@ github.com/cespare/xxhash/v2 v2.3.0 
h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
 github.com/cespare/xxhash/v2 v2.3.0/go.mod 
h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
 github.com/chai2010/gettext-go v1.0.2 
h1:1Lwwip6Q2QGsAdl/ZKPCwTe9fe0CjlUbqj5bFNSjIRk=
 github.com/chai2010/gettext-go v1.0.2/go.mod 
h1:y+wnP2cHYaVj19NZhYKAwEMH2CI1gNHeQQ+5AjwawxA=
+github.com/cheggaaa/pb/v3 v3.1.5 
h1:QuuUzeM2WsAqG2gMqtzaWithDJv0i+i6UlnwSCI4QLk=
+github.com/cheggaaa/pb/v3 v3.1.5/go.mod 
h1:CrxkeghYTXi1lQBEI7jSn+3svI3cuc19haAj6jM60XI=
 github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod 
h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
 github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 
h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
 github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod 
h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
diff --git a/operator/pkg/util/progress/progress.go 
b/operator/pkg/util/progress/progress.go
index 56a1dca2..c23f0191 100644
--- a/operator/pkg/util/progress/progress.go
+++ b/operator/pkg/util/progress/progress.go
@@ -1,5 +1,13 @@
 package progress
 
+import (
+       "fmt"
+       "github.com/apache/dubbo-kubernetes/operator/pkg/component"
+       "github.com/cheggaaa/pb/v3"
+       "io"
+       "sync"
+)
+
 type InstallState int
 
 const (
@@ -10,3 +18,80 @@ const (
 )
 
 const inProgress = `{{ yellow (cycle . "-" "-" " ") }}`
+
+type ManifestInfo struct {
+       report   func()
+       err      string
+       waiting  []string
+       finished bool
+       mu       sync.Mutex
+}
+
+type Info struct {
+       components map[string]*ManifestInfo
+       state      InstallState
+       bar        *pb.ProgressBar
+       mu         sync.Mutex
+       template   string
+}
+
+func NewInfo() *Info {
+       return &Info{
+               components: map[string]*ManifestInfo{},
+               bar:        createBar(),
+       }
+}
+
+func (info *Info) reportProgress(componentName string) func() {
+       return func() {
+               compName := component.Name(componentName)
+               cliName := component.UserFacingCompName(compName)
+               info.mu.Lock()
+               defer info.mu.Unlock()
+               comp := info.components[componentName]
+               comp.mu.Lock()
+               finished := comp.finished
+               compErr := comp.err
+               comp.mu.Unlock()
+               successIcon := "🎉"
+               if icon, found := component.Icons[compName]; found {
+                       successIcon = icon
+               }
+               if finished || compErr != "" {
+                       if finished {
+                               info.SetMessage(fmt.Sprintf(`{{ green "✔" }} %s 
install Completed %s`, cliName, successIcon), true)
+                       } else {
+                               info.SetMessage(fmt.Sprintf(`{{ read "✘" }} %s 
encountered an error: %s`, cliName, compErr), true)
+                       }
+                       delete(info.components, componentName)
+                       info.bar = createBar()
+                       return
+               }
+       }
+}
+
+func (info *Info) SetMessage(status string, finish bool) {
+       if !info.bar.GetBool(pb.Terminal) && status == info.template {
+               return
+       }
+       info.template = status
+       info.bar.SetTemplateString(info.template)
+       if finish {
+               info.bar.Finish()
+       }
+       info.bar.Write()
+}
+
+func createBar() *pb.ProgressBar {
+       var testWriter *io.Writer
+       bar := pb.New(0)
+       bar.Set(pb.Static, true)
+       if testWriter != nil {
+               bar.SetWriter(*testWriter)
+       }
+       bar.Start()
+       if !bar.GetBool(pb.Terminal) {
+               bar.Set(pb.ReturnSymbol, "\n")
+       }
+       return bar
+}

Reply via email to