@@ -11,7 +11,6 @@ import (
11
11
12
12
"github.com/AlecAivazis/survey/v2"
13
13
"github.com/fatih/color"
14
- isatty "github.com/mattn/go-isatty"
15
14
16
15
"github.com/crowdsecurity/go-cs-lib/slicetools"
17
16
@@ -192,11 +191,6 @@ func (p *ActionPlan) compactDescription() string {
192
191
}
193
192
194
193
func (p * ActionPlan ) Confirm (verbose bool ) (bool , error ) {
195
- // user provided an --interactive flag, but we go with the defaults if it's not a tty
196
- if ! isatty .IsTerminal (os .Stdout .Fd ()) && ! isatty .IsCygwinTerminal (os .Stdout .Fd ()) {
197
- return true , nil
198
- }
199
-
200
194
fmt .Println ("The following actions will be performed:\n " + p .Description (verbose ))
201
195
202
196
var answer bool
@@ -206,9 +200,15 @@ func (p *ActionPlan) Confirm(verbose bool) (bool, error) {
206
200
Default : true ,
207
201
}
208
202
203
+ tty , err := os .OpenFile ("/dev/tty" , os .O_RDWR , 0 )
204
+ if err != nil {
205
+ return prompt .Default , nil
206
+ }
207
+ defer tty .Close ()
208
+
209
209
// in case of EOF, it's likely stdin has been closed in a script or package manager,
210
210
// we can't do anything but go with the default
211
- if err := survey .AskOne (prompt , & answer ); err != nil {
211
+ if err := survey .AskOne (prompt , & answer , survey . WithStdio ( tty , tty , tty ) ); err != nil {
212
212
if errors .Is (err , io .EOF ) {
213
213
return prompt .Default , nil
214
214
}
@@ -221,22 +221,18 @@ func (p *ActionPlan) Confirm(verbose bool) (bool, error) {
221
221
return answer , nil
222
222
}
223
223
224
- func (p * ActionPlan ) Execute (ctx context.Context , interactive bool , dryRun bool , verbose bool ) error {
224
+ func (p * ActionPlan ) Execute (ctx context.Context , interactive bool , dryRun bool , alwaysShowPlan bool , verbosePlan bool ) error {
225
+ // interactive: show action plan, ask for confirm
226
+ // dry-run: show action plan, no prompt, no action
227
+ // alwaysShowPlan: print plan even if interactive and dry-run are false
228
+ // verbosePlan: plan summary is displaying each step in order
225
229
if len (p .commands ) == 0 {
226
- // XXX: show skipped commands, warnings?
227
230
fmt .Println ("Nothing to do." )
228
231
return nil
229
232
}
230
233
231
- if dryRun {
232
- fmt .Println ("Action plan:\n " + p .Description (verbose ))
233
- fmt .Println ("Dry run, no action taken." )
234
-
235
- return nil
236
- }
237
-
238
234
if interactive {
239
- answer , err := p .Confirm (verbose )
235
+ answer , err := p .Confirm (verbosePlan )
240
236
if err != nil {
241
237
return err
242
238
}
@@ -245,6 +241,15 @@ func (p *ActionPlan) Execute(ctx context.Context, interactive bool, dryRun bool,
245
241
fmt .Println ("Operation canceled." )
246
242
return nil
247
243
}
244
+ } else {
245
+ if dryRun || alwaysShowPlan {
246
+ fmt .Println ("Action plan:\n " + p .Description (verbosePlan ))
247
+ }
248
+
249
+ if dryRun {
250
+ fmt .Println ("Dry run, no action taken." )
251
+ return nil
252
+ }
248
253
}
249
254
250
255
for _ , c := range p .commands {
0 commit comments