Skip to content

Commit d621f25

Browse files
authored
Merge pull request #61 from RoseSecurity/add-spinner
Add Spinner for User Experience
2 parents cea226d + 22cbd2c commit d621f25

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

cmd/root.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ func TerramaidCmd() *cobra.Command {
4343
return fmt.Errorf("Terraform directory \"%s\" does not exist", options.TFDir)
4444
}
4545

46-
if options.TFDir != "" && !utils.TerraformFilesExist(options.TFDir) {
47-
return fmt.Errorf("Terraform files do not exist in directory \"%s\"", options.TFDir)
46+
if options.TFDir != "" {
47+
exists, err := utils.TerraformFilesExist(options.TFDir)
48+
if err != nil {
49+
return fmt.Errorf("error checking Terraform files in directory \"%s\": %v", options.TFDir, err)
50+
}
51+
if !exists {
52+
return fmt.Errorf("Terraform files do not exist in directory \"%s\"", options.TFDir)
53+
}
4854
}
4955

5056
if options.WorkingDir != "" && !utils.DirExists(options.WorkingDir) {
@@ -68,6 +74,8 @@ func TerramaidCmd() *cobra.Command {
6874
},
6975

7076
RunE: func(cmd *cobra.Command, args []string) error {
77+
sp := utils.NewSpinner("Generating Terramaid Diagrams")
78+
sp.Start()
7179
graph, err := internal.ParseTerraform(options.WorkingDir, options.TFBinary, options.TFPlan)
7280
if err != nil {
7381
return fmt.Errorf("error parsing Terraform: %w", err)
@@ -91,6 +99,7 @@ func TerramaidCmd() *cobra.Command {
9199
return fmt.Errorf("error writing to file: %w", err)
92100
}
93101

102+
sp.Stop()
94103
fmt.Printf("Mermaid diagram successfully written to %s\n", options.Output)
95104

96105
return nil

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ go 1.23
44

55
require (
66
github.com/awalterschulze/gographviz v2.0.3+incompatible
7+
github.com/briandowns/spinner v1.23.1
78
github.com/caarlos0/env/v11 v11.2.2
89
github.com/fatih/color v1.17.0
910
github.com/hashicorp/terraform-exec v0.21.0
11+
github.com/mattn/go-colorable v0.1.13
1012
github.com/spf13/cobra v1.8.1
1113
golang.org/x/text v0.19.0
1214
)
@@ -17,11 +19,11 @@ require (
1719
github.com/hashicorp/go-version v1.7.0 // indirect
1820
github.com/hashicorp/terraform-json v0.22.1 // indirect
1921
github.com/inconshreveable/mousetrap v1.1.0 // indirect
20-
github.com/mattn/go-colorable v0.1.13 // indirect
2122
github.com/mattn/go-isatty v0.0.20 // indirect
2223
github.com/russross/blackfriday/v2 v2.1.0 // indirect
2324
github.com/spf13/pflag v1.0.5 // indirect
2425
github.com/zclconf/go-cty v1.14.4 // indirect
2526
golang.org/x/sys v0.18.0 // indirect
27+
golang.org/x/term v0.1.0 // indirect
2628
gopkg.in/yaml.v3 v3.0.1 // indirect
2729
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew
88
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
99
github.com/awalterschulze/gographviz v2.0.3+incompatible h1:9sVEXJBJLwGX7EQVhLm2elIKCm7P2YHFC8v6096G09E=
1010
github.com/awalterschulze/gographviz v2.0.3+incompatible/go.mod h1:GEV5wmg4YquNw7v1kkyoX9etIk8yVmXj+AkDHuuETHs=
11+
github.com/briandowns/spinner v1.23.1 h1:t5fDPmScwUjozhDj4FA46p5acZWIPXYE30qW2Ptu650=
12+
github.com/briandowns/spinner v1.23.1/go.mod h1:LaZeM4wm2Ywy6vO571mvhQNRcWfRUnXOs0RcKV0wYKM=
1113
github.com/caarlos0/env/v11 v11.2.2 h1:95fApNrUyueipoZN/EhA8mMxiNxrBwDa+oAZrMWl3Kg=
1214
github.com/caarlos0/env/v11 v11.2.2/go.mod h1:JBfcdeQiBoI3Zh1QRAWfe+tpiNTmDtcCj/hHHHMx0vc=
1315
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
@@ -81,6 +83,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
8183
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8284
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
8385
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
86+
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
87+
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
8488
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
8589
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
8690
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=

pkg/utils/utils.go

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
package utils
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
7+
"time"
8+
9+
"github.com/briandowns/spinner"
10+
"github.com/mattn/go-colorable"
11+
)
12+
13+
const (
14+
ColorReset = "\033[0m"
15+
ColorGreen = "\033[32m"
16+
ColorYellow = "\033[33m"
17+
ColorRed = "\033[31m"
18+
ColorBold = "\033[1m"
19+
ColorUnderline = "\033[4m"
620
)
721

22+
type Spinner struct {
23+
s *spinner.Spinner
24+
}
25+
826
// Check if a directory exists
927
func DirExists(dir string) bool {
10-
if _, err := os.Stat(dir); os.IsNotExist(err) {
11-
return false
12-
}
13-
14-
return true
28+
_, err := os.Stat(dir)
29+
return !os.IsNotExist(err)
1530
}
1631

1732
// Check if Terraform files exist in a directory
18-
func TerraformFilesExist(dir string) bool {
33+
func TerraformFilesExist(dir string) (bool, error) {
1934
validExtensions := []string{".tf", ".tf.json", ".tftest.hcl", ".tftest.json", "terraform.tfvars", "terraform.tfvars.json"}
2035

21-
found := false
36+
var found bool
2237
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
2338
if err != nil {
2439
return err
@@ -32,8 +47,28 @@ func TerraformFilesExist(dir string) bool {
3247
return nil
3348
})
3449
if err != nil {
35-
return false
50+
return false, err
3651
}
3752

38-
return found
53+
return found, nil
54+
}
55+
56+
// Initialize a new spinner
57+
func NewSpinner(text string) *Spinner {
58+
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
59+
s.Color("blue")
60+
s.Writer = colorable.NewColorableStdout() // Ensure colors are supported on Windows
61+
s.Suffix = " " + text
62+
return &Spinner{s: s}
63+
}
64+
65+
// Start the spinner
66+
func (sp *Spinner) Start() {
67+
fmt.Printf("%s%s%s ", ColorBold+ColorGreen, sp.s.Suffix, ColorReset)
68+
sp.s.Start()
69+
}
70+
71+
// Stop the spinner
72+
func (sp *Spinner) Stop() {
73+
sp.s.Stop()
3974
}

0 commit comments

Comments
 (0)