• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mongodb / mongodb-atlas-cli / 26228926989

21 May 2026 01:27PM UTC coverage: 22.63% (-41.6%) from 64.198%
26228926989

push

github

apix-bot[bot]
Update compliance report for v1.55.0

8987 of 39713 relevant lines covered (22.63%)

0.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.42
/internal/cli/plugin/plugin.go
1
// Copyright 2024 MongoDB Inc
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package plugin
16

17
import (
18
        "errors"
19
        "fmt"
20
        "strings"
21

22
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
23
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/plugin"
24
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/set"
25
        "github.com/spf13/cobra"
26
)
27

28
const (
29
        sourcePluginName = "sourcePluginName"
30
)
31

32
type Opts struct {
33
        plugins          *plugin.ValidatedPlugins
34
        existingCommands []*cobra.Command
35
}
36

37
func (opts *Opts) getValidPlugins() []*plugin.Plugin {
×
38
        return opts.plugins.GetValidPlugins()
×
39
}
×
40

41
func (opts *Opts) getValidAndInvalidPlugins() []*plugin.Plugin {
1✔
42
        return opts.plugins.GetValidAndInvalidPlugins()
1✔
43
}
1✔
44

45
// find a plugin given the input argument of a plugin command
46
// the input arg can be the plugin name, the github values (<repo-owner>/<repo-name>) or the entire github URL of the plugin.
47
func (opts *Opts) findPluginWithArg(arg string) (*plugin.Plugin, error) {
1✔
48
        // try to parse input to github values
1✔
49
        // if parsing fails it will be assumed that the input is the plugin name
1✔
50
        githubValues, err := parseGithubReleaseValues(arg)
1✔
51
        var pluginToUninstall *plugin.Plugin
1✔
52

1✔
53
        if err == nil {
2✔
54
                pluginToUninstall, err = opts.findPluginWithGithubValues(githubValues.owner, githubValues.name)
1✔
55
        } else {
2✔
56
                pluginToUninstall, err = opts.findPluginWithName(arg)
1✔
57
        }
1✔
58

59
        if err != nil {
2✔
60
                return nil, err
1✔
61
        }
1✔
62

63
        return pluginToUninstall, nil
1✔
64
}
65

66
func createExistingCommandsSet(existingCommands []*cobra.Command) set.Set[string] {
1✔
67
        existingCommandsSet := set.NewSet[string]()
1✔
68
        for _, cmd := range existingCommands {
2✔
69
                existingCommandsSet.Add(cmd.Name())
1✔
70
                for _, alias := range cmd.Aliases {
2✔
71
                        existingCommandsSet.Add(alias)
1✔
72
                }
1✔
73
        }
74

75
        return existingCommandsSet
1✔
76
}
77

78
// find a plugin given a github owner and repository name
79
//
80
// also searches through invalid plugins, because this function is also used for uninstall command
81
// throws an error when:
82
// - multiple plugins are found with the same github values
83
// - no plugin is found with the given github values
84
func (opts *Opts) findPluginWithGithubValues(owner string, name string) (*plugin.Plugin, error) {
1✔
85
        var foundPlugin *plugin.Plugin
1✔
86

1✔
87
        for _, p := range opts.getValidAndInvalidPlugins() {
2✔
88
                if p.Github != nil && p.Github.Equals(owner, name) {
2✔
89
                        if foundPlugin != nil {
1✔
90
                                return nil, fmt.Errorf(`found multiple plugins with github values %s/%s`, owner, name)
×
91
                        }
×
92

93
                        foundPlugin = p
1✔
94
                }
95
        }
96

97
        if foundPlugin == nil {
2✔
98
                return nil, fmt.Errorf(`could not find plugin with github values %s/%s`, owner, name)
1✔
99
        }
1✔
100

101
        return foundPlugin, nil
1✔
102
}
103

104
// find a plugin given a plugin name
105
//
106
// also searches through invalid plugins, because this function is also used for uninstall command
107
// throws an error when:
108
// - multiple plugins are found with the same name
109
// - no plugin is found with the given name
110
func (opts *Opts) findPluginWithName(name string) (*plugin.Plugin, error) {
1✔
111
        var foundPlugin *plugin.Plugin
1✔
112

1✔
113
        for _, p := range opts.getValidAndInvalidPlugins() {
2✔
114
                if p.Name == name {
2✔
115
                        if foundPlugin != nil {
2✔
116
                                return nil, fmt.Errorf(`found multiple plugins with name %s`, name)
1✔
117
                        }
1✔
118

119
                        foundPlugin = p
1✔
120
                }
121
        }
122

123
        if foundPlugin == nil {
2✔
124
                return nil, fmt.Errorf(`could not find plugin with name %s`, name)
1✔
125
        }
1✔
126

127
        return foundPlugin, nil
1✔
128
}
129

130
func RegisterCommands(rootCmd *cobra.Command) {
×
131
        plugins := plugin.GetAllPluginsValidated(createExistingCommandsSet(rootCmd.Commands()))
×
132

×
133
        // Get set of first-class plugins that need updating
×
134
        firstClassPluginsNeedingUpdate := getFirstClassPluginsNeedingUpdate(plugins)
×
135

×
136
        for _, p := range plugins.GetValidPlugins() {
×
137
                // Skip installed plugins that are first-class plugins needing update
×
138
                // Their commands will be handled by the first-class plugin mechanism
×
139
                if _, needsUpdate := firstClassPluginsNeedingUpdate[p.Name]; needsUpdate {
×
140
                        continue
×
141
                }
142
                rootCmd.AddCommand(p.GetCobraCommands()...)
×
143
        }
144

145
        rootCmd.AddCommand(getFirstClassPluginCommands(plugins)...)
×
146

×
147
        rootCmd.AddCommand(Builder(plugins, rootCmd.Commands()))
×
148
}
149

150
func validateManifest(manifest *plugin.Manifest) error {
1✔
151
        if valid, errorList := manifest.IsValid(); !valid {
2✔
152
                var manifestErrorLog strings.Builder
1✔
153
                fmt.Fprintf(&manifestErrorLog, "plugin in directory %q could not be loaded due to the following error(s) in the manifest.yaml:\n", manifest.PluginDirectoryPath)
1✔
154
                for _, err := range errorList {
2✔
155
                        fmt.Fprintf(&manifestErrorLog, "\t- %s\n", err.Error())
1✔
156
                }
1✔
157

158
                return errors.New(manifestErrorLog.String())
1✔
159
        }
160
        return nil
1✔
161
}
162

163
func Builder(plugins *plugin.ValidatedPlugins, existingCommands []*cobra.Command) *cobra.Command {
×
164
        const use = "plugin"
×
165
        cmd := &cobra.Command{
×
166
                Use:     use,
×
167
                Aliases: cli.GenerateAliases(use),
×
168
                Short:   "Manage plugins for the AtlasCLI.",
×
169
        }
×
170

×
171
        pluginOpts := &Opts{
×
172
                plugins:          plugins,
×
173
                existingCommands: existingCommands,
×
174
        }
×
175

×
176
        cmd.AddCommand(
×
177
                ListBuilder(pluginOpts),
×
178
                InstallBuilder(pluginOpts),
×
179
                UninstallBuilder(pluginOpts),
×
180
                UpdateBuilder(pluginOpts),
×
181
        )
×
182

×
183
        return cmd
×
184
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc