/
set.go
107 lines (97 loc) · 3.38 KB
/
set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2020 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 config
import (
"fmt"
"strings"
"github.com/mongodb/mongodb-atlas-cli/mongocli/v2/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/mongocli/v2/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/mongocli/v2/internal/config"
"github.com/mongodb/mongodb-atlas-cli/mongocli/v2/internal/search"
"github.com/mongodb/mongodb-atlas-cli/mongocli/v2/internal/validate"
"github.com/spf13/cobra"
)
type SetOpts struct {
cli.GlobalOpts
prop string
val string
store config.SetSaver
}
func (opts *SetOpts) Run() error {
if strings.HasSuffix(opts.prop, "_url") {
if err := validate.URL(opts.val); err != nil {
return err
}
} else if strings.HasSuffix(opts.prop, "_id") {
if err := validate.ObjectID(opts.val); err != nil {
return err
}
}
var value interface{}
value = opts.val
if search.StringInSlice(config.BooleanProperties(), opts.prop) {
value = config.IsTrue(opts.val)
}
if search.StringInSlice(config.GlobalProperties(), opts.prop) {
opts.store.SetGlobal(opts.prop, value)
} else {
opts.store.Set(opts.prop, value)
}
if err := opts.store.Save(); err != nil {
return err
}
fmt.Printf("Updated property '%s'\n", opts.prop)
return nil
}
func mongoCLIExample() string {
if config.BinName() == config.MongoCLI {
return fmt.Sprintf(` Set the Ops Manager base URL in the profile myProfile to http://localhost:30700/:
%s config set ops_manager_url http://localhost:30700/ -P myProfile
`, config.BinName())
}
return ""
}
func SetBuilder() *cobra.Command {
const argsN = 2
cmd := &cobra.Command{
Use: "set <propertyName> <value>",
Short: "Configure specific properties of a profile.",
Args: func(cmd *cobra.Command, args []string) error {
if err := require.ExactArgs(argsN)(cmd, args); err != nil {
return err
}
if !search.StringInSlice(cmd.ValidArgs, args[0]) {
return fmt.Errorf("invalid property: %q", args[0])
}
return nil
},
Example: fmt.Sprintf(` %s
Set the organization ID in the default profile to 5dd5aaef7a3e5a6c5bd12de4:
%s config set org_id 5dd5aaef7a3e5a6c5bd12de4`, mongoCLIExample(), config.BinName()),
Annotations: map[string]string{
"propertyNameDesc": "Property to set in the profile. Valid values for Atlas CLI and MongoDB CLI are project_id, org_id, service, public_api_key, private_api_key, output, mongosh_path, skip_update_check, telemetry_enabled, access_token, and refresh_token. Additionally, values that are only valid for MongoDB CLI include ops_manager_url base_url, ops_manager_ca_certificate, and ops_manager_skip_verify.",
"valueDesc": "Value for the property to set in the profile.",
},
ValidArgs: config.Properties(),
RunE: func(_ *cobra.Command, args []string) error {
opts := &SetOpts{
store: config.Default(),
prop: args[0],
val: args[1],
}
return opts.Run()
},
}
return cmd
}