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

mongodb / mongodb-atlas-cli / 16670272475

01 Aug 2025 08:27AM UTC coverage: 57.953% (-7.1%) from 65.017%
16670272475

Pull #4071

github

fmenezes
lint
Pull Request #4071: chore: remove unit tag from tests

23613 of 40745 relevant lines covered (57.95%)

2.74 hits per line

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

85.0
/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 {
1✔
83
        for _, p := range plugins.GetValidPlugins() {
2✔
84
                if p.Name == fcp.Name {
1✔
85
                        return true
×
86
                }
×
87
        }
88

89
        return false
1✔
90
}
91

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

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

113
        // find and run installed plugin
114
        installedPlugin, err := plugin.GetPluginWithName(fcp.Name, nil, false)
1✔
115
        if err != nil {
1✔
116
                return err
×
117
        }
×
118

119
        return installedPlugin.Run(cmd, args)
1✔
120
}
121

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

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

139
                commands = append(commands, cmd)
1✔
140
        }
141

142
        return commands
1✔
143
}
144

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

153
                commands = append(commands, firstClassPlugin.getCommands(plugins)...)
1✔
154
        }
155

156
        return commands
1✔
157
}
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