Skip to content

Commit

Permalink
chore: Add --profile flag (#36)
Browse files Browse the repository at this point in the history
* Initial implementation

* fixes docs

* nit: Changes license year
  • Loading branch information
cveticm authored Feb 13, 2025
1 parent 584a2b1 commit 3ac938e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 15 deletions.
12 changes: 0 additions & 12 deletions docs/command/atlas-kubernetes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ Options
-
- false
- help for kubernetes

Inherited Options
-----------------

.. list-table::
:header-rows: 1
:widths: 20 10 10 60

* - Name
- Type
- Required
- Description
* - -P, --profile
- string
- false
Expand Down
6 changes: 6 additions & 0 deletions internal/cli/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

coreConfig "github.com/mongodb/atlas-cli-core/config"

"github.com/mongodb/atlas-cli-plugin-kubernetes/internal/cli"
"github.com/mongodb/atlas-cli-plugin-kubernetes/internal/cli/kubernetes/config"
"github.com/mongodb/atlas-cli-plugin-kubernetes/internal/cli/kubernetes/operator"
"github.com/mongodb/atlas-cli-plugin-kubernetes/internal/flag"
Expand All @@ -32,6 +33,7 @@ import (
func Builder() *cobra.Command {
const use = "kubernetes"
var (
profile string
debugLevel bool
)

Expand All @@ -49,6 +51,9 @@ func Builder() *cobra.Command {
if err != nil {
return fmt.Errorf("failed to load Atlas CLI configuration: %v", err)
}
if err := cli.InitProfile(profile); err != nil {
return fmt.Errorf("Failed to initialise Atlas CLI profile: %v", err)
}

telemetry.TrackPluginCommand(cmd, args)
return nil
Expand All @@ -57,6 +62,7 @@ func Builder() *cobra.Command {

cmd.AddCommand(config.Builder(), operator.Builder())

cmd.PersistentFlags().StringVarP(&profile, flag.Profile, flag.ProfileShort, "", usage.ProfileAtlasCLI)
cmd.PersistentFlags().BoolVarP(&debugLevel, flag.Debug, flag.DebugShort, false, usage.Debug)
_ = cmd.PersistentFlags().MarkHidden(flag.Debug)

Expand Down
41 changes: 41 additions & 0 deletions internal/cli/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2025 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 cli

import (
"errors"
"fmt"

"github.com/mongodb/atlas-cli-core/config"
"github.com/mongodb/atlas-cli-plugin-kubernetes/internal/flag"
)

var errUnsupportedService = errors.New("unsupported service")

func InitProfile(profile string) error {
if profile != "" {
return config.SetName(profile)
} else if profile = config.GetString(flag.Profile); profile != "" {
return config.SetName(profile)
} else if availableProfiles := config.List(); len(availableProfiles) == 1 {
return config.SetName(availableProfiles[0])
}

if !config.IsCloud() {
return fmt.Errorf("%w: %s", errUnsupportedService, config.Service())
}

return nil
}
2 changes: 2 additions & 0 deletions internal/flag/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package flag

const (
Profile = "profile" // Profile flag to use a profile
ProfileShort = "P" // ProfileShort flag to use a profile
OrgID = "orgId" // OrgID flag to use an Organization ID
ProjectID = "projectId" // ProjectID flag to use a project ID
ClusterName = "clusterName" // ClusterName flag
Expand Down
1 change: 1 addition & 0 deletions internal/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package usage

const (
ProfileAtlasCLI = "Name of the profile to use from your configuration file. To learn about profiles for the Atlas CLI, see https://dochub.mongodb.org/core/atlas-cli-save-connection-settings."
ProjectID = "Hexadecimal string that identifies the project to use. This option overrides the settings in the configuration file or environment variable."
OrgID = "Organization ID to use. This option overrides the settings in the configuration file or environment variable."
ExporterClusterName = "One or more comma separated cluster names to import"
Expand Down
1 change: 0 additions & 1 deletion test/e2e/kubernetes_operator_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func TestKubernetesOperatorInstall(t *testing.T) {
req := require.New(t)

cliPath, err := PluginBin()
fmt.Println(cliPath)
req.NoError(err)
const contextPrefix = "kind-"
t.Run("should failed to install old and not supported version of the operator", func(t *testing.T) {
Expand Down
2 changes: 0 additions & 2 deletions tools/docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,5 @@ func mockAtlasBuilder() *cobra.Command {
}

cmd.AddCommand(kubernetes.Builder())
// PersistentFlags used to generate 'Inherited Options'
cmd.PersistentFlags().StringP("profile", "P", "", "Name of the profile to use from your configuration file. To learn about profiles for the Atlas CLI, see https://dochub.mongodb.org/core/atlas-cli-save-connection-settings.")
return cmd
}

0 comments on commit 3ac938e

Please sign in to comment.