-
Notifications
You must be signed in to change notification settings - Fork 153
/
cmd.go
83 lines (67 loc) · 2.71 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package app
// Provides support for removing an application from gitops management.
import (
"context"
"fmt"
"os"
"strings"
"github.com/weaveworks/weave-gitops/cmd/internal"
"github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/services"
"github.com/weaveworks/weave-gitops/pkg/services/auth"
"k8s.io/apimachinery/pkg/types"
"github.com/lithammer/dedent"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/weaveworks/weave-gitops/cmd/gitops/version"
"github.com/weaveworks/weave-gitops/pkg/services/app"
)
var params app.RemoveParams
var Cmd = &cobra.Command{
Use: "app <app name>",
Short: "Delete an app from a gitops cluster",
Long: strings.TrimSpace(dedent.Dedent(`
Deletes an application from a gitops cluster so it will no longer be managed via GitOps
`)),
Example: `
# Delete application from gitops control via immediate commit
gitops delete app podinfo
`,
Args: cobra.MinimumNArgs(1),
RunE: runCmd,
SilenceUsage: true,
SilenceErrors: true,
PostRun: func(cmd *cobra.Command, args []string) {
version.CheckVersion(version.CheckpointParamsWithFlags(version.CheckpointParams(), cmd))
},
}
func init() {
Cmd.Flags().BoolVar(¶ms.DryRun, "dry-run", false, "If set, 'gitops delete app' will not make any changes to the system; it will just display the actions that would have been taken")
Cmd.Flags().BoolVar(¶ms.AutoMerge, "auto-merge", false, "If set, 'gitops delete app' will merge changes automatically to the config repository")
}
func runCmd(cmd *cobra.Command, args []string) error {
ctx := context.Background()
params.Name = args[0]
params.Namespace, _ = cmd.Parent().Flags().GetString("namespace")
log := internal.NewCLILogger(os.Stdout)
factory := services.NewFactory(flux.New(osys.New(), &runner.CLIRunner{}), log)
appService, err := factory.GetAppService(ctx)
if err != nil {
return fmt.Errorf("failed to create app service: %w", err)
}
appContent, err := appService.Get(types.NamespacedName{Name: params.Name, Namespace: params.Namespace})
if err != nil {
return fmt.Errorf("unable to get application for %s %w", params.Name, err)
}
providerClient := internal.NewGitProviderClient(os.Stdout, os.LookupEnv, auth.NewAuthCLIHandler, log)
gitClient, gitProvider, err := factory.GetGitClients(ctx, providerClient, services.NewGitConfigParamsFromApp(appContent, params.DryRun))
if err != nil {
return fmt.Errorf("failed to get git clients: %w", err)
}
if err := appService.Remove(gitClient, gitProvider, params); err != nil {
return errors.Wrapf(err, "failed to remove the app %s", params.Name)
}
return nil
}