/
connect_opts.go
116 lines (99 loc) · 3.46 KB
/
connect_opts.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
108
109
110
111
112
113
114
115
116
// Copyright 2023 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 options
import (
"context"
"errors"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/compass"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/log"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mongosh"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/telemetry"
)
var (
ConnectionStringTypeStandard = "standard"
connectionStringTypePrivate = "private"
connectionStringTypeOptions = []string{ConnectionStringTypeStandard, connectionStringTypePrivate}
errConnectionStringTypeNotImplemented = errors.New("connection string type not implemented")
errNetworkPeeringConnectionNotConfigured = errors.New("network peering connection is not configured for this deployment")
promptConnectionStringType = "What type of connection string type would you like to use?"
)
type ConnectOpts struct {
cli.OutputOpts
DeploymentOpts
ConnectWith string
ConnectToAtlasOpts
}
func (opts *ConnectOpts) Connect(ctx context.Context) error {
if _, err := opts.SelectDeployments(ctx, opts.ConfigProjectID()); err != nil {
return err
}
if err := opts.askConnectWith(); err != nil {
return err
}
if opts.IsAtlasDeploymentType() {
if err := opts.validateAndPromptAtlasOpts(); err != nil {
return err
}
return opts.connectToAtlas()
}
return opts.connectToLocal(ctx)
}
func (opts *ConnectOpts) askConnectWith() error {
if opts.ConnectWith == "" {
var err error
if opts.ConnectWith, err = opts.DeploymentOpts.PromptConnectWith(); err != nil {
return err
}
}
return ValidateConnectWith(opts.ConnectWith)
}
func (opts *ConnectOpts) connectToDeployment(connectionString string) error {
switch opts.ConnectWith {
case ConnectWithConnectionString:
return opts.Print(connectionString)
case CompassConnect:
if !compass.Detect() {
return ErrCompassNotInstalled
}
if _, err := log.Warningln("Launching MongoDB Compass..."); err != nil {
return err
}
return compass.Run(opts.DeploymentOpts.DBUsername, opts.DeploymentOpts.DBUserPassword, connectionString)
case MongoshConnect:
if !mongosh.Detect() {
return ErrMongoshNotInstalled
}
return mongosh.Run(opts.DeploymentOpts.DBUsername, opts.DeploymentOpts.DBUserPassword, connectionString)
}
return nil
}
func (opts *ConnectOpts) promptDBUsername() error {
p := &survey.Input{
Message: "Username for authenticating to MongoDB deployment",
}
return telemetry.TrackAskOne(p, &opts.DeploymentOpts.DBUsername)
}
func (opts *ConnectOpts) promptDBUserPassword() error {
if !opts.IsTerminalInput() {
_, err := fmt.Fscanln(opts.InReader, &opts.DeploymentOpts.DBUserPassword)
return err
}
p := &survey.Password{
Message: "Password for authenticating to MongoDB deployment",
}
return telemetry.TrackAskOne(p, &opts.DeploymentOpts.DBUserPassword)
}