@@ -16,7 +16,9 @@ package clusters
16
16
17
17
import (
18
18
"context"
19
+ "errors"
19
20
"fmt"
21
+ "net/http"
20
22
21
23
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
22
24
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
@@ -31,6 +33,7 @@ import (
31
33
type WatchOpts struct {
32
34
cli.GlobalOpts
33
35
cli.WatchOpts
36
+ cli.RefresherOpts
34
37
name string
35
38
store store.ClusterDescriber
36
39
}
@@ -45,24 +48,34 @@ func (opts *WatchOpts) initStore(ctx context.Context) func() error {
45
48
}
46
49
}
47
50
48
- func isRetryable (err error ) bool {
49
- atlasErr , ok := atlasClustersPinned .AsError (err )
50
- return ok && atlasErr .GetErrorCode () == "CLUSTER_NOT_FOUND"
51
- }
51
+ func (opts * WatchOpts ) watcher (ctx context.Context ) func () (any , bool , error ) {
52
+ return func () (any , bool , error ) {
53
+ result , err := opts .store .AtlasCluster (opts .ConfigProjectID (), opts .name )
54
+ if err != nil {
55
+ var atlasClustersPinnedErr * atlasClustersPinned.GenericOpenAPIError
52
56
53
- func (opts * WatchOpts ) watcher () (any , bool , error ) {
54
- result , err := opts .store .AtlasCluster (opts .ConfigProjectID (), opts .name )
55
- if err != nil {
56
- return nil , false , err
57
- }
58
- if result .GetStateName () == "UPDATING" {
59
- opts .IsRetryableErr = isRetryable
57
+ if errors .As (err , & atlasClustersPinnedErr ) {
58
+ if * atlasClustersPinnedErr .Model ().Error == http .StatusUnauthorized {
59
+ // Refresh the access token
60
+ // Note: this only updates the config, so we have to re-initialize the store
61
+ if err := opts .RefreshAccessToken (ctx ); err != nil {
62
+ return nil , false , err
63
+ }
64
+
65
+ // Re-initialize store, refreshAccessToken only refreshes the config
66
+ return nil , false , opts .initStore (ctx )()
67
+ }
68
+ }
69
+ }
70
+ if err != nil {
71
+ return nil , false , err
72
+ }
73
+ return nil , result .GetStateName () == "IDLE" , nil
60
74
}
61
- return nil , result .GetStateName () == "IDLE" , nil
62
75
}
63
76
64
- func (opts * WatchOpts ) Run () error {
65
- if _ , err := opts .Watch (opts .watcher ); err != nil {
77
+ func (opts * WatchOpts ) Run (ctx context. Context ) error {
78
+ if _ , err := opts .Watch (opts .watcher ( ctx ) ); err != nil {
66
79
return err
67
80
}
68
81
@@ -93,11 +106,12 @@ You can interrupt the command's polling at any time with CTRL-C.
93
106
opts .ValidateProjectID ,
94
107
opts .initStore (cmd .Context ()),
95
108
opts .InitOutput (cmd .OutOrStdout (), watchTemplate ),
109
+ opts .InitFlow (config .Default ()),
96
110
)
97
111
},
98
- RunE : func (_ * cobra.Command , args []string ) error {
112
+ RunE : func (cmd * cobra.Command , args []string ) error {
99
113
opts .name = args [0 ]
100
- return opts .Run ()
114
+ return opts .Run (cmd . Context () )
101
115
},
102
116
}
103
117
0 commit comments