|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/deislabs/duffle/pkg/action" |
| 7 | + "github.com/deislabs/duffle/pkg/claim" |
| 8 | + "github.com/deislabs/duffle/pkg/credentials" |
| 9 | + "github.com/deislabs/duffle/pkg/utils/crud" |
| 10 | + "github.com/docker/cli/cli" |
| 11 | + "github.com/docker/cli/cli/command" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +type uninstallOptions struct { |
| 16 | + targetContext string |
| 17 | + credentialsets []string |
| 18 | +} |
| 19 | + |
| 20 | +func uninstallCmd(dockerCli command.Cli) *cobra.Command { |
| 21 | + var opts uninstallOptions |
| 22 | + |
| 23 | + cmd := &cobra.Command{ |
| 24 | + Use: "uninstall <installation-name>", |
| 25 | + Short: "Uninstall an application", |
| 26 | + Args: cli.ExactArgs(1), |
| 27 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 28 | + return runUninstall(dockerCli, args[0], opts) |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + cmd.Flags().StringVar(&opts.targetContext, "target-context", "", "Context on which to uninstall the application") |
| 33 | + cmd.Flags().StringArrayVarP(&opts.credentialsets, "credential-set", "c", []string{}, "Use a duffle credentialset (either a YAML file, or a credential set present in the duffle credential store)") |
| 34 | + |
| 35 | + return cmd |
| 36 | +} |
| 37 | + |
| 38 | +func runUninstall(dockerCli command.Cli, claimName string, opts uninstallOptions) error { |
| 39 | + muteDockerCli(dockerCli) |
| 40 | + h := duffleHome() |
| 41 | + |
| 42 | + claimStore := claim.NewClaimStore(crud.NewFileSystemStore(h.Claims(), "json")) |
| 43 | + c, err := claimStore.Read(claimName) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + targetContext := getTargetContext(opts.targetContext) |
| 48 | + |
| 49 | + driverImpl, err := prepareDriver(dockerCli) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + creds, err := prepareCredentialSet(targetContext, dockerCli.ContextStore(), c.Bundle, opts.credentialsets) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + if err := credentials.Validate(creds, c.Bundle.Credentials); err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + uninst := &action.Uninstall{ |
| 61 | + Driver: driverImpl, |
| 62 | + } |
| 63 | + err = uninst.Run(&c, creds, dockerCli.Out()) |
| 64 | + if err == nil { |
| 65 | + return claimStore.Delete(claimName) |
| 66 | + } |
| 67 | + if err2 := claimStore.Store(c); err2 != nil { |
| 68 | + fmt.Fprintf(dockerCli.Err(), "failed to update claim: %s\n", err2) |
| 69 | + } |
| 70 | + return err |
| 71 | +} |
0 commit comments