Skip to content

Commit f3fea80

Browse files
committed
improve(*): more beautiful prompt.
1 parent ba52ad6 commit f3fea80

File tree

8 files changed

+96
-38
lines changed

8 files changed

+96
-38
lines changed

cli/command/clean.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ package command
2424

2525
import (
2626
"fmt"
27-
"strings"
2827

2928
"github.com/opencurve/curveadm/cli/cli"
3029
"github.com/opencurve/curveadm/internal/configure/topology"
@@ -41,7 +40,11 @@ var (
4140
$ curveadm clean --only='log,data' # Clean log and data for all services
4241
$ curveadm clean --role=etcd --only=container # Clean container for etcd services`
4342

44-
supportOnlyFlag = map[string]bool{"log": true, "data": true, "container": true}
43+
supportOnlyFlag = map[string]bool{
44+
task.ITEM_LOG: true,
45+
task.ITEM_DATA: true,
46+
task.ITEM_CONTAINER: true,
47+
}
4548
)
4649

4750
type cleanOptions struct {
@@ -105,10 +108,7 @@ func runClean(curveadm *cli.CurveAdm, options cleanOptions) error {
105108
return nil
106109
}
107110

108-
// clean service
109-
curveadm.WriteOut("Warning: clean role=%s host=%s id=%s only=%s now, %s in it will be cleaned.\n",
110-
role, host, id, strings.Join(only, ","), strings.Join(only, ","))
111-
if pass := tui.ConfirmYes("Do you want to continue? [YES/No]: "); !pass {
111+
if pass := tui.ConfirmYes(tui.PromptCleanService(role, host, only)); !pass {
112112
curveadm.WriteOut("Clean canceled\n")
113113
return nil
114114
}

cli/command/cluster/remove.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"fmt"
2727

2828
"github.com/opencurve/curveadm/cli/cli"
29-
"github.com/opencurve/curveadm/internal/tui/common"
29+
tui "github.com/opencurve/curveadm/internal/tui/common"
3030
cliutil "github.com/opencurve/curveadm/internal/utils"
3131
"github.com/opencurve/curveadm/pkg/log"
3232
"github.com/spf13/cobra"
@@ -66,8 +66,7 @@ func runRemove(curveadm *cli.CurveAdm, options removeOptions) error {
6666
}
6767

6868
// TODO(@Wine93): check all service has removed before delete cluster
69-
curveadm.WriteOut("Warning: This operation will remove cluster %s, and all data in it will be cleaned.\n", clusterName)
70-
if pass := common.ConfirmYes("Do you want to continue? [YES/No]: "); !pass {
69+
if pass := tui.ConfirmYes(tui.PromptRemoveCluster(clusterName)); !pass {
7170
curveadm.WriteOut("Remove cluster canceled\n")
7271
return nil
7372
} else if err := curveadm.Storage().DeleteCluster(clusterName); err != nil {

cli/command/config/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func runCommit(curveadm *cli.CurveAdm, options commitOptions) error {
103103

104104
if err := validateTopology(oldData, newData); err != nil {
105105
return err
106-
} else if pass := common.ConfirmYes("Do you want to continue? [YES/No]: "); !pass {
106+
} else if pass := common.ConfirmYes("Do you want to continue?"); !pass {
107107
return nil
108108
} else if err := curveadm.Storage().SetClusterTopology(curveadm.ClusterId(), newData); err != nil {
109109
return err

cli/command/stop.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ func runStop(curveadm *cli.CurveAdm, options stopOptions) error {
7676
return fmt.Errorf("service not found")
7777
}
7878

79-
// stop service
80-
curveadm.WriteOut("Warning: Stop all service now, client IO will be hang!\n")
81-
if pass := tui.ConfirmYes("Do you want to continue? [YES/No]: "); !pass {
79+
if pass := tui.ConfirmYes(tui.PromptStopService()); !pass {
8280
curveadm.WriteOut("Stop canceled\n")
8381
return nil
8482
}

cli/command/support.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,12 @@ import (
3333
task "github.com/opencurve/curveadm/internal/task/task/common"
3434
"github.com/opencurve/curveadm/internal/task/tasks"
3535
"github.com/opencurve/curveadm/internal/tools"
36-
"github.com/opencurve/curveadm/internal/tui/common"
36+
tui "github.com/opencurve/curveadm/internal/tui/common"
3737
"github.com/opencurve/curveadm/internal/utils"
3838
cliutil "github.com/opencurve/curveadm/internal/utils"
3939
"github.com/spf13/cobra"
4040
)
4141

42-
const (
43-
PROMPT = `FYI:
44-
> We have collected logs for troubleshooting,
45-
> and now we will send these logs to the curve center.
46-
> Please don't worry about the data security,
47-
> we guarantee that all logs are encrypted
48-
> and only you have the secret key.
49-
`
50-
)
51-
5242
type supportOptions struct {
5343
}
5444

@@ -142,8 +132,7 @@ func runSupport(curveadm *cli.CurveAdm, options supportOptions) error {
142132
return err
143133
}
144134

145-
curveadm.WriteOut(color.YellowString(PROMPT))
146-
if pass := common.ConfirmYes("Do you want to continue? [YES/No]: "); !pass {
135+
if pass := tui.ConfirmYes(tui.PromptCollectService()); !pass {
147136
return nil
148137
}
149138

internal/configure/format/fc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
)
2828

2929
const (
30-
DEFAULT_SSH_TIMEOUT = 10
30+
DEFAULT_SSH_TIMEOUT = 10 // seconds
3131
)
3232

3333
type (

internal/tui/common/prompt.go

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,90 @@
1616

1717
/*
1818
* Project: CurveAdm
19-
* Created Date: 2021-12-22
19+
* Created Date: 2022-01-14
2020
* Author: Jingli Chen (Wine93)
2121
*/
2222

2323
package common
24+
25+
import (
26+
"bytes"
27+
"fmt"
28+
"strings"
29+
"text/template"
30+
31+
"github.com/fatih/color"
32+
)
33+
34+
const (
35+
PROMPT_REMOVE_CLUSTER = `{{.warning}}
36+
Do you want to continue?`
37+
38+
PROMPT_STOP_SERVICE = `{{.warning}}
39+
Do you want to continue?`
40+
41+
PROMPT_CLEAN_SERVICE = `{{.warning}}
42+
- Service role: {{.role}} ("*" means all roles)
43+
- Service host: {{.host}} ("*" means all hosts)
44+
- Clean items : [{{.items}}]
45+
Do you want to continue?`
46+
47+
PROMPT_COLLECT_SERVICE = `
48+
FYI:
49+
> We have collected logs for troubleshooting,
50+
> and now we will send these logs to the curve center.
51+
> Please don't worry about the data security,
52+
> we guarantee that all logs are encrypted
53+
> and only you have the secret key.
54+
`
55+
56+
DEFAULT_CONFIRM_PROMPT = "Do you want to continue?"
57+
)
58+
59+
type Prompt struct {
60+
tmpl *template.Template
61+
data map[string]interface{}
62+
}
63+
64+
func NewPrompt(text string) *Prompt {
65+
return &Prompt{
66+
tmpl: template.Must(template.New("prompt").Parse(text)),
67+
data: map[string]interface{}{},
68+
}
69+
}
70+
71+
func (p *Prompt) Build() string {
72+
buffer := bytes.NewBufferString("")
73+
err := p.tmpl.Execute(buffer, p.data)
74+
if err != nil {
75+
return ""
76+
}
77+
return buffer.String()
78+
}
79+
80+
func PromptRemoveCluster(clusterName string) string {
81+
prompt := NewPrompt(PROMPT_REMOVE_CLUSTER)
82+
prompt.data["warning"] = fmt.Sprintf("WARNING: cluster '%s' will be removed,\n"+
83+
"and all data in it will be cleaned up", clusterName)
84+
return prompt.Build()
85+
}
86+
87+
func PromptStopService() string {
88+
prompt := NewPrompt(PROMPT_STOP_SERVICE)
89+
prompt.data["warning"] = "WARNING: stop service may cause client IO be hang"
90+
return prompt.Build()
91+
}
92+
93+
func PromptCleanService(role, host string, items []string) string {
94+
prompt := NewPrompt(PROMPT_CLEAN_SERVICE)
95+
prompt.data["warning"] = "WARNING: service items which matched will be cleaned up"
96+
prompt.data["role"] = role
97+
prompt.data["host"] = host
98+
prompt.data["items"] = strings.Join(items, ",")
99+
return prompt.Build()
100+
}
101+
102+
func PromptCollectService() string {
103+
prompt := NewPrompt(color.YellowString(PROMPT_COLLECT_SERVICE) + DEFAULT_CONFIRM_PROMPT)
104+
return prompt.Build()
105+
}

internal/tui/common/tui.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,6 @@ func prompt(prompt string) string {
141141
return strings.TrimSuffix(input, "\n")
142142
}
143143

144-
func ConfirmNo(format string, a ...interface{}) bool {
145-
ans := prompt(fmt.Sprintf(format, a...) + "(default=Y)")
146-
switch strings.TrimSpace(strings.ToLower(ans)) {
147-
case "n", "no":
148-
return true
149-
default:
150-
return false
151-
}
152-
}
153-
154144
func ConfirmYes(format string, a ...interface{}) bool {
155145
ans := prompt(fmt.Sprintf(format, a...) + " [yes/no]: (default=no)")
156146
switch strings.TrimSpace(ans) {

0 commit comments

Comments
 (0)