File tree Expand file tree Collapse file tree 6 files changed +54
-2
lines changed Expand file tree Collapse file tree 6 files changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -16,13 +16,17 @@ package cli
16
16
17
17
import (
18
18
"github.com/mongodb/mongocli/internal/description"
19
+ "github.com/mongodb/mongocli/internal/validate"
19
20
"github.com/spf13/cobra"
20
21
)
21
22
22
23
func AtlasBuilder () * cobra.Command {
23
24
cmd := & cobra.Command {
24
25
Use : "atlas" ,
25
26
Short : description .Atlas ,
27
+ PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
28
+ return validate .Credentials ()
29
+ },
26
30
}
27
31
cmd .AddCommand (AtlasClustersBuilder ())
28
32
cmd .AddCommand (AtlasDBUsersBuilder ())
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ package cli
16
16
17
17
import (
18
18
"github.com/mongodb/mongocli/internal/description"
19
+ "github.com/mongodb/mongocli/internal/validate"
19
20
"github.com/spf13/cobra"
20
21
)
21
22
@@ -24,6 +25,9 @@ func CloudManagerBuilder() *cobra.Command {
24
25
Use : "cloud-manager" ,
25
26
Aliases : []string {"cm" },
26
27
Short : description .CloudManager ,
28
+ PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
29
+ return validate .Credentials ()
30
+ },
27
31
}
28
32
29
33
cmd .AddCommand (OpsManagerClustersBuilder ())
Original file line number Diff line number Diff line change @@ -16,12 +16,16 @@ package cli
16
16
17
17
import (
18
18
"github.com/mongodb/mongocli/internal/description"
19
+ "github.com/mongodb/mongocli/internal/validate"
19
20
"github.com/spf13/cobra"
20
21
)
21
22
22
23
func IAMBuilder () * cobra.Command {
23
24
cmd := & cobra.Command {
24
- Use : "iam" ,
25
+ Use : "iam" ,
26
+ PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
27
+ return validate .Credentials ()
28
+ },
25
29
Short : description .IAM ,
26
30
}
27
31
cmd .AddCommand (IAMProjectsBuilder ())
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ package cli
16
16
17
17
import (
18
18
"github.com/mongodb/mongocli/internal/description"
19
+ "github.com/mongodb/mongocli/internal/validate"
19
20
"github.com/spf13/cobra"
20
21
)
21
22
@@ -24,6 +25,9 @@ func OpsManagerBuilder() *cobra.Command {
24
25
Use : "ops-manager" ,
25
26
Aliases : []string {"om" },
26
27
Short : description .OpsManager ,
28
+ PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
29
+ return validate .Credentials ()
30
+ },
27
31
}
28
32
29
33
cmd .AddCommand (OpsManagerClustersBuilder ())
Original file line number Diff line number Diff line change @@ -16,9 +16,12 @@ package validate
16
16
17
17
import (
18
18
"encoding/hex"
19
+ "errors"
19
20
"fmt"
20
21
"net/url"
21
22
"strings"
23
+
24
+ "github.com/mongodb/mongocli/internal/config"
22
25
)
23
26
24
27
func URL (val interface {}) error {
@@ -45,3 +48,10 @@ func ObjectID(s string) error {
45
48
}
46
49
return nil
47
50
}
51
+
52
+ func Credentials () error {
53
+ if config .PrivateAPIKey () == "" || config .PublicAPIKey () == "" {
54
+ return errors .New ("missing credentials" )
55
+ }
56
+ return nil
57
+ }
Original file line number Diff line number Diff line change 14
14
15
15
package validate
16
16
17
- import "testing"
17
+ import (
18
+ "os"
19
+ "testing"
20
+
21
+ "github.com/spf13/viper"
22
+ )
18
23
19
24
func TestURL (t * testing.T ) {
20
25
t .Run ("Valid URL" , func (t * testing.T ) {
@@ -57,3 +62,24 @@ func TestObjectID(t *testing.T) {
57
62
}
58
63
})
59
64
}
65
+
66
+ func TestCredentials (t * testing.T ) {
67
+ t .Run ("no credentials" , func (t * testing.T ) {
68
+ err := Credentials ()
69
+ if err == nil {
70
+ t .Fatal ("Credentials() expected an error\n " )
71
+ }
72
+ })
73
+ t .Run ("with credentials" , func (t * testing.T ) {
74
+ // this function depends on the global config (globals are bad I know)
75
+ // the easiest way we have to test it is via ENV vars
76
+ viper .AutomaticEnv ()
77
+ _ = os .Setenv ("PUBLIC_API_KEY" , "test" )
78
+ _ = os .Setenv ("PRIVATE_API_KEY" , "test" )
79
+
80
+ err := Credentials ()
81
+ if err != nil {
82
+ t .Fatalf ("Credentials() unexpected error %v\n " , err )
83
+ }
84
+ })
85
+ }
You can’t perform that action at this time.
0 commit comments