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

mongodb / mongodb-atlas-cli / 17470223062

04 Sep 2025 04:18PM UTC coverage: 63.948% (-0.2%) from 64.109%
17470223062

push

github

web-flow
CLOUDP-340658: enable plugin version verification (#4189)

1 of 6 new or added lines in 2 files covered. (16.67%)

61 existing lines in 3 files now uncovered.

26137 of 40872 relevant lines covered (63.95%)

0.79 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/spf13/cobra"
22
)
23

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

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

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

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

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

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

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

89
        return false
2✔
90
}
91

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

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

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

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

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

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

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

146
        return commands
1✔
147
}
148

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

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

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