Skip to content

Commit 32f9b10

Browse files
authored
Replace survey with huh (#112)
* Replace configuration override thing with huh and add huh as a dependency * Convert cmd/configure all the way over to huh * Convert cmd/diagnostics and remove survey as it is no longer needed --------- Co-authored-by: Garbg <[email protected]>
1 parent 103fca3 commit 32f9b10

4 files changed

Lines changed: 154 additions & 84 deletions

File tree

cmd/configure.go

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ import (
1111
"regexp"
1212
"time"
1313

14-
"github.com/AlecAivazis/survey/v2"
15-
"github.com/AlecAivazis/survey/v2/terminal"
14+
"github.com/charmbracelet/huh"
1615
"github.com/goccy/go-json"
17-
"github.com/spf13/cobra"
18-
1916
"github.com/pelican-dev/wings/config"
17+
"github.com/spf13/cobra"
2018
)
2119

2220
var configureArgs struct {
@@ -53,62 +51,56 @@ func configureCmdRun(cmd *cobra.Command, args []string) {
5351
}
5452

5553
if _, err := os.Stat(configureArgs.ConfigPath); err == nil && !configureArgs.Override {
56-
survey.AskOne(&survey.Confirm{Message: "Override existing configuration file"}, &configureArgs.Override)
54+
huh.NewConfirm().
55+
Title("Override existing configuration file?").
56+
Value(&configureArgs.Override).
57+
Run()
5758
if !configureArgs.Override {
5859
fmt.Println("Aborting process; a configuration file already exists for this node.")
5960
os.Exit(1)
6061
}
6162
} else if err != nil && !os.IsNotExist(err) {
6263
panic(err)
6364
}
64-
65-
var questions []*survey.Question
65+
var fields []huh.Field
6666
if configureArgs.PanelURL == "" {
67-
questions = append(questions, &survey.Question{
68-
Name: "PanelURL",
69-
Prompt: &survey.Input{Message: "Panel URL: "},
70-
Validate: func(ans interface{}) error {
71-
if str, ok := ans.(string); ok {
72-
_, err := url.ParseRequestURI(str)
73-
return err
74-
}
75-
return nil
76-
},
77-
})
67+
fields = append(fields, huh.NewInput().
68+
Title("Panel URL: ").
69+
Validate(func(str string) error {
70+
_, err := url.ParseRequestURI(str)
71+
return err
72+
}).
73+
Value(&configureArgs.PanelURL),
74+
)
7875
}
7976

8077
if configureArgs.Token == "" {
81-
questions = append(questions, &survey.Question{
82-
Name: "Token",
83-
Prompt: &survey.Input{Message: "API Token: "},
84-
Validate: func(ans interface{}) error {
85-
if str, ok := ans.(string); ok {
86-
if len(str) == 0 {
87-
return fmt.Errorf("please provide a valid authentication token")
88-
}
78+
fields = append(fields, huh.NewInput().
79+
Title("API Token: ").
80+
Validate(func(str string) error {
81+
if len(str) == 0 {
82+
return fmt.Errorf("please provide a valid authentication token")
8983
}
9084
return nil
91-
},
92-
})
85+
}).
86+
Value(&configureArgs.Token),
87+
)
9388
}
9489

9590
if configureArgs.Node == "" {
96-
questions = append(questions, &survey.Question{
97-
Name: "Node",
98-
Prompt: &survey.Input{Message: "Node ID: "},
99-
Validate: func(ans interface{}) error {
100-
if str, ok := ans.(string); ok {
101-
if !nodeIdRegex.Match([]byte(str)) {
102-
return fmt.Errorf("please provide a valid authentication token")
103-
}
91+
fields = append(fields, huh.NewInput().
92+
Title("Node ID: ").
93+
Validate(func(str string) error {
94+
if !nodeIdRegex.Match([]byte(str)) {
95+
return fmt.Errorf("please providde a valid node ID")
10496
}
10597
return nil
106-
},
107-
})
98+
}).
99+
Value(&configureArgs.Node),
100+
)
108101
}
109-
110-
if err := survey.Ask(questions, &configureArgs); err != nil {
111-
if err == terminal.InterruptErr {
102+
if err := huh.NewForm(huh.NewGroup(fields...)).Run(); err != nil {
103+
if err == huh.ErrUserAborted {
112104
return
113105
}
114106

@@ -154,7 +146,7 @@ func configureCmdRun(cmd *cobra.Command, args []string) {
154146
if err := json.Unmarshal(b, cfg); err != nil {
155147
panic(err)
156148
}
157-
149+
158150
// Manually specify the Panel URL as it won't be decoded from JSON.
159151
cfg.PanelLocation = configureArgs.PanelURL
160152

cmd/diagnostics.go

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ import (
1515
"strings"
1616
"time"
1717

18-
"github.com/AlecAivazis/survey/v2"
19-
"github.com/AlecAivazis/survey/v2/terminal"
2018
"github.com/apex/log"
19+
"github.com/charmbracelet/huh"
2120
"github.com/docker/docker/api/types"
21+
dockerSystem "github.com/docker/docker/api/types/system" // Alias the correct system package
2222
"github.com/docker/docker/pkg/parsers/kernel"
2323
"github.com/docker/docker/pkg/parsers/operatingsystem"
2424
"github.com/goccy/go-json"
2525
"github.com/spf13/cobra"
26-
dockerSystem "github.com/docker/docker/api/types/system" // Alias the correct system package
2726

2827
"github.com/pelican-dev/wings/config"
2928
"github.com/pelican-dev/wings/environment"
@@ -69,26 +68,30 @@ func newDiagnosticsCommand() *cobra.Command {
6968
// - running docker containers
7069
// - logs
7170
func diagnosticsCmdRun(*cobra.Command, []string) {
72-
questions := []*survey.Question{
73-
{
74-
Name: "IncludeEndpoints",
75-
Prompt: &survey.Confirm{Message: "Do you want to include endpoints (i.e. the FQDN/IP of your panel)?", Default: false},
76-
},
77-
{
78-
Name: "IncludeLogs",
79-
Prompt: &survey.Confirm{Message: "Do you want to include the latest logs?", Default: true},
80-
},
81-
{
82-
Name: "ReviewBeforeUpload",
83-
Prompt: &survey.Confirm{
84-
Message: "Do you want to review the collected data before uploading to " + diagnosticsArgs.HastebinURL + "?",
85-
Help: "The data, especially the logs, might contain sensitive information, so you should review it. You will be asked again if you want to upload.",
86-
Default: true,
87-
},
88-
},
71+
// To set default to true
72+
defaultTrueConfirmAccessor := func() huh.Accessor[bool] {
73+
accessor := huh.EmbeddedAccessor[bool]{}
74+
accessor.Set(true)
75+
return &accessor
8976
}
90-
if err := survey.Ask(questions, &diagnosticsArgs); err != nil {
91-
if err == terminal.InterruptErr {
77+
form := huh.NewForm(
78+
huh.NewGroup(
79+
huh.NewConfirm().
80+
Title("Do you want to include endpoints (i.e. the FQDN/IP of your panel)?").
81+
Value(&diagnosticsArgs.IncludeEndpoints),
82+
huh.NewConfirm().
83+
Title("Do you want to include the latest logs?").
84+
Accessor(defaultTrueConfirmAccessor()).
85+
Value(&diagnosticsArgs.IncludeLogs),
86+
huh.NewConfirm().
87+
Title("Do you want to review the collected data before uploading to "+diagnosticsArgs.HastebinURL+"?").
88+
Description("The data, especially the logs, might contain sensitive information, so you should review it. You will be asked again if you want to upload.").
89+
Accessor(defaultTrueConfirmAccessor()).
90+
Value(&diagnosticsArgs.ReviewBeforeUpload),
91+
),
92+
)
93+
if err := form.Run(); err != nil {
94+
if err == huh.ErrUserAborted {
9295
return
9396
}
9497
panic(err)
@@ -199,7 +202,10 @@ func diagnosticsCmdRun(*cobra.Command, []string) {
199202

200203
upload := !diagnosticsArgs.ReviewBeforeUpload
201204
if !upload {
202-
survey.AskOne(&survey.Confirm{Message: "Upload to " + diagnosticsArgs.HastebinURL + "?", Default: false}, &upload)
205+
huh.NewConfirm().
206+
Title("Upload to " + diagnosticsArgs.HastebinURL + "?").
207+
Value(&upload).
208+
Run()
203209
}
204210
if upload {
205211
u, err := uploadToHastebin(diagnosticsArgs.HastebinURL, output.String())

go.mod

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.23.0
44

55
require (
66
emperror.dev/errors v0.8.1
7-
github.com/AlecAivazis/survey/v2 v2.3.7
87
github.com/Jeffail/gabs/v2 v2.7.0
98
github.com/NYTimes/logrotate v1.0.0
109
github.com/acobaugh/osrelease v0.1.0
@@ -51,6 +50,30 @@ require (
5150
gorm.io/gorm v1.30.0
5251
)
5352

53+
require (
54+
github.com/atotto/clipboard v0.1.4 // indirect
55+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
56+
github.com/catppuccin/go v0.3.0 // indirect
57+
github.com/charmbracelet/bubbles v0.21.0 // indirect
58+
github.com/charmbracelet/bubbletea v1.3.4 // indirect
59+
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect
60+
github.com/charmbracelet/lipgloss v1.1.0 // indirect
61+
github.com/charmbracelet/x/ansi v0.8.0 // indirect
62+
github.com/charmbracelet/x/cellbuf v0.0.13 // indirect
63+
github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0 // indirect
64+
github.com/charmbracelet/x/term v0.2.1 // indirect
65+
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
66+
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
67+
github.com/mattn/go-localereader v0.0.1 // indirect
68+
github.com/mattn/go-runewidth v0.0.16 // indirect
69+
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
70+
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
71+
github.com/muesli/cancelreader v0.2.2 // indirect
72+
github.com/muesli/termenv v0.16.0 // indirect
73+
github.com/rivo/uniseg v0.4.7 // indirect
74+
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
75+
)
76+
5477
require (
5578
github.com/Microsoft/go-winio v0.6.1 // indirect
5679
github.com/Microsoft/hcsshim v0.12.2 // indirect
@@ -61,6 +84,7 @@ require (
6184
github.com/bodgit/windows v1.0.1 // indirect
6285
github.com/bytedance/sonic v1.11.6 // indirect
6386
github.com/bytedance/sonic/loader v0.1.1 // indirect
87+
github.com/charmbracelet/huh v0.7.0
6488
github.com/cloudwego/base64x v0.1.4 // indirect
6589
github.com/cloudwego/iasm v0.2.0 // indirect
6690
github.com/containerd/errdefs v0.3.0 // indirect
@@ -90,14 +114,12 @@ require (
90114
github.com/jinzhu/now v1.1.5 // indirect
91115
github.com/jonboulle/clockwork v0.5.0 // indirect
92116
github.com/json-iterator/go v1.1.12 // indirect
93-
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
94117
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
95118
github.com/kr/fs v0.1.0 // indirect
96119
github.com/leodido/go-urn v1.4.0 // indirect
97120
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
98121
github.com/magefile/mage v1.15.0 // indirect
99122
github.com/mattn/go-isatty v0.0.20 // indirect
100-
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
101123
github.com/minio/minlz v1.0.0 // indirect
102124
github.com/moby/docker-image-spec v1.3.1 // indirect
103125
github.com/moby/sys/atomicwriter v0.1.0 // indirect
@@ -139,7 +161,6 @@ require (
139161
golang.org/x/arch v0.8.0 // indirect
140162
golang.org/x/mod v0.18.0 // indirect
141163
golang.org/x/net v0.39.0 // indirect
142-
golang.org/x/term v0.32.0 // indirect
143164
golang.org/x/text v0.25.0 // indirect
144165
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect
145166
golang.org/x/tools v0.22.0 // indirect
@@ -151,4 +172,4 @@ require (
151172
modernc.org/mathutil v1.6.0 // indirect
152173
modernc.org/memory v1.8.0 // indirect
153174
modernc.org/sqlite v1.29.6 // indirect
154-
)
175+
)

0 commit comments

Comments
 (0)