Skip to content

Commit 4dfc42f

Browse files
authored
Merge pull request #22 from b4b4r07/feature/ask
Add feature to ask if it's ok to run command
2 parents dfbfd89 + a33a12a commit 4dfc42f

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

cmd/install.go

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ func newInstallCmd() *cobra.Command {
7272
pkgs = given
7373
}
7474

75+
yes, _ := c.askRunCommand(*c, getNameInPackages(pkgs))
76+
if !yes {
77+
fmt.Println("Cancelled")
78+
return nil
79+
}
80+
7581
c.Env.AskWhen(map[string]bool{
7682
"GITHUB_TOKEN": config.HasGitHubReleaseBlock(pkgs),
7783
"AFX_SUDO_PASSWORD": config.HasSudoInCommandBuildSteps(pkgs),

cmd/meta.go

+41
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"log"
88
"os"
99
"path/filepath"
10+
"sort"
1011
"strings"
1112

13+
"github.com/AlecAivazis/survey/v2"
1214
"github.com/b4b4r07/afx/pkg/config"
1315
"github.com/b4b4r07/afx/pkg/env"
1416
"github.com/b4b4r07/afx/pkg/errors"
@@ -191,6 +193,45 @@ func (m *meta) prompt() (config.Package, error) {
191193
return nil, errors.New("pkg not found")
192194
}
193195

196+
func (m *meta) askRunCommand(op interface{}, pkgs []string) (bool, error) {
197+
var do string
198+
switch op.(type) {
199+
case installCmd:
200+
do = "install"
201+
case uninstallCmd:
202+
do = "uninstall"
203+
case updateCmd:
204+
do = "update"
205+
default:
206+
return false, errors.New("unsupported command type")
207+
}
208+
209+
length := 3
210+
target := strings.Join(pkgs, ", ")
211+
if len(pkgs) > length {
212+
target = fmt.Sprintf("%s, ... (%d packages)", strings.Join(pkgs[:length], ", "), len(pkgs))
213+
}
214+
215+
yes := false
216+
confirm := survey.Confirm{
217+
Message: fmt.Sprintf("OK to %s these packages? %s", do, color.YellowString(target)),
218+
}
219+
220+
if len(pkgs) > length {
221+
helpMessage := "\n"
222+
sort.Strings(pkgs)
223+
for _, pkg := range pkgs {
224+
helpMessage += fmt.Sprintf("- %s\n", pkg)
225+
}
226+
confirm.Help = helpMessage
227+
}
228+
229+
if err := survey.AskOne(&confirm, &yes); err != nil {
230+
return false, errors.Wrap(err, "failed to catch answer from console")
231+
}
232+
return yes, nil
233+
}
234+
194235
func getNameInPackages(pkgs []config.Package) []string {
195236
var keys []string
196237
for _, pkg := range pkgs {

cmd/uninstall.go

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ func newUninstallCmd() *cobra.Command {
6969
resources = given
7070
}
7171

72+
yes, _ := c.askRunCommand(*c, getNameInResources(resources))
73+
if !yes {
74+
fmt.Println("Cancelled")
75+
return nil
76+
}
77+
7278
return c.run(resources)
7379
},
7480
PostRunE: func(cmd *cobra.Command, args []string) error {

cmd/update.go

+6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ func newUpdateCmd() *cobra.Command {
7272
pkgs = given
7373
}
7474

75+
yes, _ := c.askRunCommand(*c, getNameInPackages(pkgs))
76+
if !yes {
77+
fmt.Println("Cancelled")
78+
return nil
79+
}
80+
7581
c.Env.AskWhen(map[string]bool{
7682
"GITHUB_TOKEN": config.HasGitHubReleaseBlock(pkgs),
7783
"AFX_SUDO_PASSWORD": config.HasSudoInCommandBuildSteps(pkgs),

0 commit comments

Comments
 (0)