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
1616
1717import (
1818 "github.com/mongodb/mongocli/internal/description"
19+ "github.com/mongodb/mongocli/internal/validate"
1920 "github.com/spf13/cobra"
2021)
2122
2223func AtlasBuilder () * cobra.Command {
2324 cmd := & cobra.Command {
2425 Use : "atlas" ,
2526 Short : description .Atlas ,
27+ PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
28+ return validate .Credentials ()
29+ },
2630 }
2731 cmd .AddCommand (AtlasClustersBuilder ())
2832 cmd .AddCommand (AtlasDBUsersBuilder ())
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ package cli
1616
1717import (
1818 "github.com/mongodb/mongocli/internal/description"
19+ "github.com/mongodb/mongocli/internal/validate"
1920 "github.com/spf13/cobra"
2021)
2122
@@ -24,6 +25,9 @@ func CloudManagerBuilder() *cobra.Command {
2425 Use : "cloud-manager" ,
2526 Aliases : []string {"cm" },
2627 Short : description .CloudManager ,
28+ PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
29+ return validate .Credentials ()
30+ },
2731 }
2832
2933 cmd .AddCommand (OpsManagerClustersBuilder ())
Original file line number Diff line number Diff line change @@ -16,12 +16,16 @@ package cli
1616
1717import (
1818 "github.com/mongodb/mongocli/internal/description"
19+ "github.com/mongodb/mongocli/internal/validate"
1920 "github.com/spf13/cobra"
2021)
2122
2223func IAMBuilder () * cobra.Command {
2324 cmd := & cobra.Command {
24- Use : "iam" ,
25+ Use : "iam" ,
26+ PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
27+ return validate .Credentials ()
28+ },
2529 Short : description .IAM ,
2630 }
2731 cmd .AddCommand (IAMProjectsBuilder ())
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ package cli
1616
1717import (
1818 "github.com/mongodb/mongocli/internal/description"
19+ "github.com/mongodb/mongocli/internal/validate"
1920 "github.com/spf13/cobra"
2021)
2122
@@ -24,6 +25,9 @@ func OpsManagerBuilder() *cobra.Command {
2425 Use : "ops-manager" ,
2526 Aliases : []string {"om" },
2627 Short : description .OpsManager ,
28+ PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
29+ return validate .Credentials ()
30+ },
2731 }
2832
2933 cmd .AddCommand (OpsManagerClustersBuilder ())
Original file line number Diff line number Diff line change @@ -16,9 +16,12 @@ package validate
1616
1717import (
1818 "encoding/hex"
19+ "errors"
1920 "fmt"
2021 "net/url"
2122 "strings"
23+
24+ "github.com/mongodb/mongocli/internal/config"
2225)
2326
2427func URL (val interface {}) error {
@@ -45,3 +48,10 @@ func ObjectID(s string) error {
4548 }
4649 return nil
4750}
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 1414
1515package validate
1616
17- import "testing"
17+ import (
18+ "os"
19+ "testing"
20+
21+ "github.com/spf13/viper"
22+ )
1823
1924func TestURL (t * testing.T ) {
2025 t .Run ("Valid URL" , func (t * testing.T ) {
@@ -57,3 +62,24 @@ func TestObjectID(t *testing.T) {
5762 }
5863 })
5964}
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