Skip to content

feat: add fig-autocomplete subcommand #995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Flags are a way to modify the command, also may be called "options". Flags alway

#### Documentation Requirements

f you are adding a brand new command, or updating a command that has no doc annotations, please define the following doc structures for the command. For more information on all command structs, see [Cobra](https://pkg.go.dev/github.com/spf13/cobra#Command).
If you are adding a brand new command, or updating a command that has no doc annotations, please define the following doc structures for the command. For more information on all command structs, see [Cobra](https://pkg.go.dev/github.com/spf13/cobra#Command).
- Add `Use` - (Required) Shows the command and arguments if applicable. Will show up in 'help' output.
- Add `Short` - (Required) Briefly describes the command. Will show up in 'help' output.
- Add `Example` - (Required) Example of how to use the command. Will show up in 'help' output.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/spf13/viper v1.10.1
github.com/stretchr/testify v1.7.0
github.com/tangzero/inflector v1.0.0
github.com/withfig/autocomplete-tools/packages/cobra v1.1.3
go.mongodb.org/atlas v0.15.0
go.mongodb.org/ops-manager v0.37.0
gopkg.in/yaml.v2 v2.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69
github.com/tangzero/inflector v1.0.0 h1:933dvPwRUUOAl98hyeeXuzFix3HwDt5j+45lleu8oh0=
github.com/tangzero/inflector v1.0.0/go.mod h1:OknKjAyDPCDzcWt0yOh2I7hqTukEdyyApcX3/KOhuXc=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/withfig/autocomplete-tools/packages/cobra v1.1.3 h1:toi+jQYY7SG/DRYZxP2U2VkFeCY+6FYLXEr8QR8VXzY=
github.com/withfig/autocomplete-tools/packages/cobra v1.1.3/go.mod h1:RoXh7+7qknOXL65uTzdzE1mPxqcPwS7FLCE9K5GfmKo=
github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyhBc=
github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
32 changes: 32 additions & 0 deletions internal/cli/figautocomplete/fig_autocomplete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2022 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 figautocomplete

import (
"github.com/spf13/cobra"
genFigSpec "github.com/withfig/autocomplete-tools/packages/cobra"
)

const CmdUse = "fig-autocomplete"

func Builder() *cobra.Command {
opts := genFigSpec.Opts{
Use: CmdUse,
}
// command hidden by default
cmd := genFigSpec.NewCmdGenFigSpec(opts)
cmd.Aliases = []string{}
return cmd
}
32 changes: 32 additions & 0 deletions internal/cli/figautocomplete/fig_autocomplete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2022 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.

//go:build unit
// +build unit

package figautocomplete

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBuilder(t *testing.T) {
got := Builder()
a := assert.New(t)
a.Equal(got.Use, "fig-autocomplete")
a.Len(got.Commands(), 0)
a.True(got.HasAvailableFlags())
}
9 changes: 4 additions & 5 deletions internal/cli/opsmanager/clusters/unmanage.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ func UnmanageBuilder() *cobra.Command {
DeleteOpts: cli.NewDeleteOpts("", "Cluster not deleted\""),
}
cmd := &cobra.Command{
Use: "unmanage <name>",
Aliases: []string{"rm"},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This alias name was also aliased in the "delete" subcommand which was shadowing this one, so removing this alias should not be breaking.

Short: "Stop managing a cluster via automation.",
Long: "This commands only removes entries from the automation config but does not actually remove a cluster.",
Args: require.ExactArgs(1),
Use: "unmanage <name>",
Short: "Stop managing a cluster via automation.",
Long: "This commands only removes entries from the automation config but does not actually remove a cluster.",
Args: require.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := opts.PreRunE(opts.ValidateProjectID, opts.initStore(cmd.Context())); err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Builder() *cobra.Command {
const use = "slowOperationThreshold"
cmd := &cobra.Command{
Use: use,
Aliases: []string{use, "slowOT", "sot", "slowMS"},
Aliases: []string{"slowOT", "sot", "slowMS"},
Short: "Enable or disable management of the slow operation threshold for your cluster.",
}
cmd.AddCommand(
Expand Down
6 changes: 6 additions & 0 deletions internal/cli/root/atlas/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/mongodb/mongocli/internal/cli/atlas/serverless"
"github.com/mongodb/mongocli/internal/cli/auth"
"github.com/mongodb/mongocli/internal/cli/events"
"github.com/mongodb/mongocli/internal/cli/figautocomplete"
"github.com/mongodb/mongocli/internal/cli/iam/globalaccesslists"
"github.com/mongodb/mongocli/internal/cli/iam/globalapikeys"
"github.com/mongodb/mongocli/internal/cli/iam/organizations"
Expand Down Expand Up @@ -87,6 +88,10 @@ func Builder(profile *string) *cobra.Command {
config.SetService(config.CloudService)
}

if cmd.Name() == figautocomplete.CmdUse { // figautocomplete command does not require credentials
return nil
}

if cmd.Name() == "quickstart" { // quickstart has its own check
return nil
}
Expand Down Expand Up @@ -158,6 +163,7 @@ func Builder(profile *string) *cobra.Command {
loginCmd,
logoutCmd,
whoCmd,
figautocomplete.Builder(),
)

rootCmd.PersistentFlags().StringVarP(profile, flag.Profile, flag.ProfileShort, "", usage.Profile)
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/root/atlas/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestBuilder(t *testing.T) {
test.CmdValidator(
t,
Builder(&profile),
34,
35,
[]string{},
)
}
3 changes: 3 additions & 0 deletions internal/cli/root/mongocli/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/mongodb/mongocli/internal/cli/auth"
"github.com/mongodb/mongocli/internal/cli/cloudmanager"
cliconfig "github.com/mongodb/mongocli/internal/cli/config"
"github.com/mongodb/mongocli/internal/cli/figautocomplete"
"github.com/mongodb/mongocli/internal/cli/iam"
"github.com/mongodb/mongocli/internal/cli/opsmanager"
"github.com/mongodb/mongocli/internal/config"
Expand Down Expand Up @@ -83,6 +84,7 @@ func Builder(profile *string, argsWithoutProg []string) *cobra.Command {
"-h",
"completion",
"__complete",
"fig-autocomplete",
}
if !hasArgs || search.StringInSlice(shouldIncludeAtlas, argsWithoutProg[0]) {
rootCmd.AddCommand(atlas.Builder())
Expand All @@ -103,6 +105,7 @@ func Builder(profile *string, argsWithoutProg []string) *cobra.Command {
loginCmd,
logoutCmd,
whoCmd,
figautocomplete.Builder(),
)

rootCmd.PersistentFlags().StringVarP(profile, flag.Profile, flag.ProfileShort, "", usage.Profile)
Expand Down
27 changes: 17 additions & 10 deletions internal/cli/root/mongocli/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,70 +31,77 @@ func TestBuilder(t *testing.T) {
}{
{
name: "atlas",
want: 9,
want: 10,
args: args{
argsWithoutProg: []string{"atlas"},
},
},
{
name: "ops-manager",
want: 8,
want: 9,
args: args{
argsWithoutProg: []string{"ops-manager"},
},
},
{
name: "cloud-manager",
want: 8,
want: 9,
args: args{
argsWithoutProg: []string{"cloud-manager"},
},
},
{
name: "ops-manager alias",
want: 8,
want: 9,
args: args{
argsWithoutProg: []string{"om"},
},
},
{
name: "cloud-manager alias",
want: 8,
want: 9,
args: args{
argsWithoutProg: []string{"cm"},
},
},
{
name: "iam",
want: 8,
want: 9,
args: args{
argsWithoutProg: []string{"iam"},
},
},
{
name: "empty",
want: 9,
want: 10,
args: args{
argsWithoutProg: []string{},
},
},
{
name: "autocomplete",
want: 9,
want: 10,
args: args{
argsWithoutProg: []string{"__complete"},
},
},
{
name: "completion",
want: 9,
want: 10,
args: args{
argsWithoutProg: []string{"completion"},
},
},
{
name: "fig-autocompletion",
want: 10,
args: args{
argsWithoutProg: []string{},
},
},
{
name: "--version",
want: 9,
want: 10,
args: args{
argsWithoutProg: []string{"completion"},
},
Expand Down