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

mongodb / mongodb-atlas-cli / 17211029452

25 Aug 2025 01:57PM UTC coverage: 63.284%. First build
17211029452

Pull #4160

github

cveticm
disabling plugin test
Pull Request #4160: CLOUDP-339293: Add plugin version check

13 of 20 new or added lines in 3 files covered. (65.0%)

26557 of 41965 relevant lines covered (63.28%)

0.78 hits per line

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

50.79
/internal/cli/plugin/first_class.go
1
// Copyright 2025 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
        "fmt"
19

20
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/plugin"
21
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/validate"
22
        "github.com/spf13/cobra"
23
)
24

25
const (
26
        FirstClassSourceType = "first-class"
27
        sourceType           = "sourceType"
28
)
29

30
// uncomment example plugin to test first class plugin feature.
31
var FirstClassPlugins = []*FirstClassPlugin{
32
        // {
33
        //         Name: "atlas-cli-first-class-plugin-example",
34
        //         Github: &Github{
35
        //                 Owner: "stefan4h",
36
        //                 Name: "atlas-cli-first-class-plugin-example",
37
        //         },
38
        //         Commands: []*Command{
39
        //                 {
40
        //                         Name: "first-class",
41
        //                         Description: "Root command of the Atlas CLI first class plugin example",
42
        //                 },
43
        //         },
44
        // },
45
        {
46
                Name: "atlas-cli-plugin-kubernetes",
47
                Github: &Github{
48
                        Owner: "mongodb",
49
                        Name:  "atlas-cli-plugin-kubernetes",
50
                },
51
                Commands: []*Command{
52
                        {
53
                                Name:        "kubernetes",
54
                                Description: "Manage Kubernetes resources.",
55
                        },
56
                },
57
        },
58
}
59

60
type Command struct {
61
        Name        string
62
        Description string
63
}
64

65
type Github struct {
66
        Owner string
67
        Name  string
68
}
69

70
type FirstClassPlugin struct {
71
        Name     string
72
        Github   *Github
73
        Commands []*Command
74
}
75

76
func IsFirstClassPluginCmd(cmd *cobra.Command) bool {
1✔
77
        if cmdSourceType, ok := cmd.Annotations[sourceType]; ok && cmdSourceType == FirstClassSourceType {
2✔
78
                return true
1✔
79
        }
1✔
80
        return false
1✔
81
}
82

83
func (fcp *FirstClassPlugin) isAlreadyInstalled(plugins *plugin.ValidatedPlugins) bool {
2✔
84
        for _, p := range plugins.GetValidPlugins() {
4✔
85
                if p.Name == fcp.Name {
3✔
86
                        return true
1✔
87
                }
1✔
88
        }
89

90
        return false
2✔
91
}
92

93
func (fcp *FirstClassPlugin) runFirstClassPluginCommand(cmd *cobra.Command, args []string, plugins *plugin.ValidatedPlugins) error {
×
94
        installOpts := &InstallOpts{
×
95
                Opts: Opts{
×
96
                        plugins: plugins,
×
97
                },
×
98
                ghClient: NewAuthenticatedGithubClient(),
×
99
        }
×
100
        installOpts.githubAsset = &GithubAsset{
×
101
                owner: fcp.Github.Owner,
×
102
                name:  fcp.Github.Name,
×
103
        }
×
104
        installOpts.Print("Installing first class plugin " + fcp.Name)
×
105

×
106
        // check if plugin already exists, if not, install it
×
107
        if err := installOpts.checkForDuplicatePlugins(); err != nil {
×
108
                return fmt.Errorf("first class plugin %s is already installed, should not install again", fcp.Name)
×
109
        }
×
110
        if err := installOpts.Run(cmd.Context()); err != nil {
×
111
                return fmt.Errorf("failed to install first class plugin %s: %w", fcp.Name, err)
×
112
        }
×
113

114
        // find and run installed plugin
115
        installedPlugin, err := plugin.GetPluginWithName(fcp.Name, nil, false)
×
116
        if err != nil {
×
117
                return err
×
118
        }
×
119
        // validate plugin version is compatible
NEW
120
        if err := validate.PluginVersion(installedPlugin.Name, installedPlugin.Version); err != nil {
×
NEW
121
                return err
×
NEW
122
        }
×
123

124
        return installedPlugin.Run(cmd, args)
×
125
}
126

127
func (fcp *FirstClassPlugin) getCommands(plugins *plugin.ValidatedPlugins) []*cobra.Command {
1✔
128
        commands := make([]*cobra.Command, 0, len(fcp.Commands))
1✔
129

1✔
130
        // for every command listed in the first class plugin, create a cobra command that installs the plugin
1✔
131
        for _, firstClassPluginCommand := range fcp.Commands {
2✔
132
                cmd := &cobra.Command{
1✔
133
                        Use:   firstClassPluginCommand.Name,
1✔
134
                        Short: firstClassPluginCommand.Description,
1✔
135
                        Annotations: map[string]string{
1✔
136
                                sourceType: FirstClassSourceType,
1✔
137
                        },
1✔
138
                        RunE: func(cmd *cobra.Command, args []string) error {
1✔
139
                                return fcp.runFirstClassPluginCommand(cmd, args, plugins)
×
140
                        },
×
141
                        DisableFlagParsing: true,
142
                }
143

144
                commands = append(commands, cmd)
1✔
145
        }
146

147
        return commands
1✔
148
}
149

150
func getFirstClassPluginCommands(plugins *plugin.ValidatedPlugins) []*cobra.Command {
1✔
151
        var commands []*cobra.Command
1✔
152
        // create cobra commands to install first class plugins when their commands are run
1✔
153
        for _, firstClassPlugin := range FirstClassPlugins {
2✔
154
                if firstClassPlugin.isAlreadyInstalled(plugins) {
1✔
155
                        continue
×
156
                }
157

158
                commands = append(commands, firstClassPlugin.getCommands(plugins)...)
1✔
159
        }
160

161
        return commands
1✔
162
}
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