@@ -18,21 +18,22 @@ import (
1818 "context"
1919 "errors"
2020 "runtime"
21+ "slices"
2122
2223 "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
2324 "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/log"
2425 "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
2526 atlasv2 "go.mongodb.org/atlas-sdk/v20240805001/admin"
2627)
2728
28- func (opts * DeploymentOpts ) SelectDeployments (ctx context.Context , projectID string ) (Deployment , error ) {
29+ func (opts * DeploymentOpts ) listDeployments (ctx context.Context , projectID string ) ([] Deployment , error ) {
2930 var atlasDeployments , localDeployments []Deployment
3031 var atlasErr , localErr error
3132
3233 if opts .IsAtlasDeploymentType () || opts .NoDeploymentTypeSet () {
3334 if atlasDeployments , atlasErr = opts .AtlasDeployments (projectID ); atlasErr != nil {
3435 if opts .IsAtlasDeploymentType () {
35- return Deployment {} , atlasErr
36+ return nil , atlasErr
3637 }
3738 if ! isUnauthenticatedErr (atlasErr ) {
3839 _ , _ = log .Warningf ("Warning: failed to retrieve Atlas deployments because %q\n " , atlasErr .Error ())
@@ -43,29 +44,37 @@ func (opts *DeploymentOpts) SelectDeployments(ctx context.Context, projectID str
4344 if opts .IsLocalDeploymentType () || opts .NoDeploymentTypeSet () {
4445 if localErr = opts .LocalDeploymentPreRun (ctx ); localErr != nil {
4546 if opts .IsLocalDeploymentType () {
46- return Deployment {} , localErr
47+ return nil , localErr
4748 }
4849 _ , _ = log .Debugf ("Warning: failed to retrieve Local deployments because %q\n " , localErr .Error ())
4950 }
5051
5152 localDeployments , localErr = opts .GetLocalDeployments (ctx )
5253 if localErr != nil {
5354 if opts .IsLocalDeploymentType () {
54- return Deployment {} , localErr
55+ return nil , localErr
5556 }
5657 _ , _ = log .Warningf ("Warning: failed to retrieve Local deployments because %q\n " , localErr .Error ())
5758 }
5859 }
5960
6061 if atlasErr != nil && localErr != nil {
61- return Deployment {} , errors .New ("failed to retrieve atlas and local deployments" )
62+ return nil , errors .New ("failed to retrieve atlas and local deployments" )
6263 }
6364
64- if opts .DeploymentName == "" {
65- return opts .Select (append (localDeployments , atlasDeployments ... ))
65+ return append (localDeployments , atlasDeployments ... ), nil
66+ }
67+
68+ func (opts * DeploymentOpts ) SelectDeployments (ctx context.Context , projectID string , states ... string ) (Deployment , error ) {
69+ deployments , err := opts .listDeployments (ctx , projectID )
70+ if err != nil {
71+ return Deployment {}, errors .New ("failed to retrieve atlas and local deployments" )
6672 }
6773
68- return opts .findDeploymentByName (localDeployments , atlasDeployments )
74+ deployments = opts .filterDeploymentByName (deployments ... )
75+ deployments = opts .filterDeploymentByState (deployments , states ... )
76+
77+ return opts .Select (deployments )
6978}
7079
7180func isUnauthenticatedErr (err error ) bool {
@@ -77,21 +86,36 @@ func isUnauthenticatedErr(err error) bool {
7786 return ok && target .GetReason () == "Unauthorized"
7887}
7988
80- func (opts * DeploymentOpts ) findDeploymentByName (localDeployments []Deployment , atlasDeployments []Deployment ) (Deployment , error ) {
81- deployments := make ([]Deployment , 0 )
82- for _ , d := range localDeployments {
89+ func (opts * DeploymentOpts ) filterDeploymentByName (deployments ... Deployment ) []Deployment {
90+ if opts .DeploymentName == "" {
91+ return deployments
92+ }
93+
94+ filteredDeployments := []Deployment {}
95+
96+ for _ , d := range deployments {
8397 if d .Name == opts .DeploymentName {
84- deployments = append (deployments , d )
98+ filteredDeployments = append (filteredDeployments , d )
8599 }
86100 }
87101
88- for _ , d := range atlasDeployments {
89- if d .Name == opts .DeploymentName {
90- deployments = append (deployments , d )
102+ return filteredDeployments
103+ }
104+
105+ func (* DeploymentOpts ) filterDeploymentByState (deployments []Deployment , states ... string ) []Deployment {
106+ if len (states ) == 0 {
107+ return deployments
108+ }
109+
110+ filteredDeployments := []Deployment {}
111+
112+ for _ , d := range deployments {
113+ if slices .Contains (states , d .StateName ) {
114+ filteredDeployments = append (filteredDeployments , d )
91115 }
92116 }
93117
94- return opts . Select ( deployments )
118+ return filteredDeployments
95119}
96120
97121func (opts * DeploymentOpts ) AtlasDeployments (projectID string ) ([]Deployment , error ) {
0 commit comments