/
list.go
107 lines (94 loc) · 3.25 KB
/
list.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2023 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package deployments
import (
"context"
"errors"
"fmt"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/atlas/deployments/options"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/podman"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/cobra"
)
type ListOpts struct {
cli.OutputOpts
cli.GlobalOpts
options.DeploymentOpts
}
const listTemplate = `NAME TYPE MDB VER STATE
{{range valueOrEmptySlice .}}{{.Name}} {{.Type}} {{.MongoDBVersion}} {{.StateName}}
{{end}}`
const errAtlas = "failed to retrieve Atlas deployments with: %s"
func (opts *ListOpts) Run(ctx context.Context) error {
if err := opts.LocalDeploymentPreRun(ctx); err != nil {
return err
}
var mdbContainers []options.Deployment
if opts.IsLocalDeploymentType() || opts.NoDeploymentTypeSet() {
var err error
mdbContainers, err = opts.GetLocalDeployments(ctx)
if err != nil && !errors.Is(err, podman.ErrPodmanNotFound) {
return err
}
}
var atlasClusters []options.Deployment
var atlasErr error
if opts.IsAtlasDeploymentType() || opts.NoDeploymentTypeSet() {
if opts.IsCliAuthenticated() && cli.TokenRefreshed {
atlasClusters, atlasErr = opts.AtlasDeployments(opts.ProjectID)
}
}
if err := opts.Print(append(atlasClusters, mdbContainers...)); err != nil {
return err
}
if atlasErr != nil {
return fmt.Errorf(errAtlas, atlasErr.Error())
}
return nil
}
// ListBuilder atlas deployments list.
func ListBuilder() *cobra.Command {
opts := &ListOpts{}
cmd := &cobra.Command{
Use: "list",
Short: "Return all deployments.",
Aliases: []string{"ls"},
Args: require.NoArgs,
GroupID: "all",
Annotations: map[string]string{
"output": listTemplate,
},
PreRunE: func(cmd *cobra.Command, _ []string) error {
return opts.PreRunE(
opts.InitStore(cmd.Context(), cmd.OutOrStdout()),
opts.InitOutput(cmd.OutOrStdout(), listTemplate))
},
RunE: func(cmd *cobra.Command, _ []string) error {
return opts.Run(cmd.Context())
},
PostRunE: func(_ *cobra.Command, _ []string) error {
return opts.PostRun()
},
}
cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)
cmd.Flags().StringVar(&opts.DeploymentType, flag.TypeFlag, "", usage.DeploymentType)
_ = cmd.RegisterFlagCompletionFunc(flag.TypeFlag, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return options.DeploymentTypeOptions, cobra.ShellCompDirectiveDefault
})
_ = cmd.Flags().MarkHidden(flag.TypeFlag)
return cmd
}