Skip to content

Commit

Permalink
Add spinners for Create and Delete operations (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
naresh-kumar-babu authored Feb 12, 2024
1 parent 29c1a91 commit b17a380
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 11 deletions.
14 changes: 11 additions & 3 deletions cmd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/CanastaWiki/Canasta-CLI-Go/internal/mediawiki"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/orchestrators"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/prompt"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/spinner"
)

func NewCmdCreate() *cobra.Command {
Expand Down Expand Up @@ -42,8 +43,10 @@ func NewCmdCreate() *cobra.Command {
if canastaInfo, err = canasta.GeneratePasswords(path, canastaInfo); err != nil {
log.Fatal(err)
}
fmt.Println("Creating Canasta installation '" + canastaInfo.Id + "'...")
if err = createCanasta(canastaInfo, pwd, path, name, domain, yamlPath, orchestrator, override); err != nil {
description := "Creating Canasta installation '" + canastaInfo.Id + "'..."
_, done := spinner.New(description)

if err = createCanasta(canastaInfo, pwd, path, name, domain, yamlPath, orchestrator, override, done); err != nil {
fmt.Print(err.Error(), "\n")
if keepConfig {
log.Fatal(fmt.Errorf("Keeping all the containers and config files\nExiting"))
Expand Down Expand Up @@ -83,7 +86,12 @@ func NewCmdCreate() *cobra.Command {
}

// importCanasta accepts all the keyword arguments and create a installation of the latest Canasta.
func createCanasta(canastaInfo canasta.CanastaVariables, pwd, path, name, domain, yamlPath, orchestrator, override string) error {
func createCanasta(canastaInfo canasta.CanastaVariables, pwd, path, name, domain, yamlPath, orchestrator, override string, done chan struct{}) error {
// Pass a message to the "done" channel indicating the completion of createCanasta function.
// This signals the spinner to stop printing progress, regardless of success or failure.
defer func() {
done <- struct{}{}
}()
if _, err := config.GetDetails(canastaInfo.Id); err == nil {
log.Fatal(fmt.Errorf("Canasta installation with the ID already exist!"))
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/CanastaWiki/Canasta-CLI-Go/internal/canasta"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/config"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/orchestrators"
"github.com/CanastaWiki/Canasta-CLI-Go/internal/spinner"
)

var (
Expand Down Expand Up @@ -41,7 +42,11 @@ func NewCmdCreate() *cobra.Command {
}

func Delete(instance config.Installation) error {
fmt.Println("Deleting Canasta installation '" + instance.Id + "'...")
description := "Deleting Canasta installation '" + instance.Id + "'..."
_, done := spinner.New(description)
defer func() {
done <- struct{}{}
}()
var err error

//Checking Installation existence
Expand Down
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ go 1.18

require (
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
github.com/schollz/progressbar/v3 v3.13.1
github.com/spf13/cobra v1.4.0
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467
golang.org/x/term v0.6.0
gopkg.in/yaml.v2 v2.4.0
)

require (
golang.org/x/sys v0.1.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/rivo/uniseg v0.4.4 // indirect
golang.org/x/sys v0.6.0 // indirect
)

require (
Expand Down
29 changes: 25 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/schollz/progressbar/v3 v3.13.1 h1:o8rySDYiQ59Mwzy2FELeHY5ZARXZTVJC7iHD6PEFUiE=
github.com/schollz/progressbar/v3 v3.13.1/go.mod h1:xvrbki8kfT1fzWzBT/UZd9L6GA+jdL7HAgq2RFnO6fQ=
github.com/sethvargo/go-password v0.2.0 h1:BTDl4CC/gjf/axHMaDQtw507ogrXLci6XRiLc7i/UHI=
github.com/sethvargo/go-password v0.2.0/go.mod h1:Ym4Mr9JXLBycr02MFuVQ/0JHidNetSgbzutTr3zsYXE=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 h1:CBpWXWQpIRjzmkkA+M7q9Fqnwd2mZr3AFqexg8YTfoM=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
49 changes: 49 additions & 0 deletions internal/spinner/spinner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package spinner

import (
"fmt"
"os"
"time"

"github.com/schollz/progressbar/v3"
)

// New returns a new spinner with the following structure.
// ⠏ Description...
// The spinner will keep spinning until a message is passed to the done channel.
func New(description string) (*progressbar.ProgressBar, chan struct{}) {
// Create a spinner to show progress and time taken, to avoid the illusion of the CLI being hung.
spinner := progressbar.NewOptions(-1,
progressbar.OptionEnableColorCodes(true),
progressbar.OptionSetWriter(os.Stdout),
progressbar.OptionSetWidth(10),
progressbar.OptionSetDescription(description),
progressbar.OptionThrottle(65*time.Millisecond),
progressbar.OptionSpinnerType(14),
progressbar.OptionFullWidth(),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionOnCompletion(func() {
fmt.Fprint(os.Stdout, "\n")
}),
)

// done channel tracks whether the process using the spinner completed. It doesn't care about success or failure,
// just the completion.
done := make(chan struct{})

// Create a goroutine to actively listen on the done channel, and update the spinner while the execution
// of the process is ongoing.
go func() {
for {
select {
case <-done:
return
default:
spinner.Add(1)
time.Sleep(5 * time.Millisecond)
}
}
}()

return spinner, done
}

0 comments on commit b17a380

Please sign in to comment.