| 213 | } |
| 214 | |
| 215 | func (p *ActionPlan) Execute(ctx context.Context, interactive bool, dryRun bool, alwaysShowPlan bool, verbose bool) error { |
| 216 | // interactive: show action plan, ask for confirm |
| 217 | // dry-run: show action plan, no prompt, no action |
| 218 | // alwaysShowPlan: print plan even if interactive and dry-run are false |
| 219 | // verbosePlan: plan summary is displaying each step in order |
| 220 | if len(p.commands) == 0 { |
| 221 | fmt.Fprintln(os.Stdout, "Nothing to install or remove.") |
| 222 | return nil |
| 223 | } |
| 224 | |
| 225 | if interactive && !dryRun { |
| 226 | answer, err := p.Confirm(dryRun) |
| 227 | if err != nil { |
| 228 | return err |
| 229 | } |
| 230 | |
| 231 | if !answer { |
| 232 | return ErrUserCanceled |
| 233 | } |
| 234 | } else { |
| 235 | if dryRun || alwaysShowPlan { |
| 236 | fmt.Fprintln(os.Stdout, "Action plan:\n"+p.Description(verbose)) |
| 237 | } |
| 238 | |
| 239 | if dryRun { |
| 240 | fmt.Fprintln(os.Stdout, "Dry run, no action taken.") |
| 241 | return nil |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | for _, c := range p.commands { |
| 246 | if err := c.Run(ctx, p); err != nil { |
| 247 | return err |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return nil |
| 252 | } |