Skip to content

Commit

Permalink
feat: Add command with feature flag required
Browse files Browse the repository at this point in the history
  • Loading branch information
raulb committed Sep 27, 2021
1 parent 3084d21 commit 7a09af8
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 4 deletions.
56 changes: 53 additions & 3 deletions cmd/meroxa/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (

type Command interface {
// Usage is the one-line usage message.
// Recommended syntax is as follow:
// Recommended syntax is as follows:
// [ ] identifies an optional argument. Arguments that are not enclosed in brackets are required.
// ... indicates that you can specify multiple values for the previous argument.
// | indicates mutually exclusive information. You can use the argument to the left of the separator or the
Expand Down Expand Up @@ -162,6 +162,11 @@ type CommandWithSubCommands interface {
SubCommands() []*cobra.Command
}

type CommandWithFeatureFlag interface {
Command
FeatureFlag() string
}

// BuildCobraCommand takes a Command and builds a *cobra.Command from it. It figures out if the command implements any
// other CommandWith* interfaces and configures the cobra command accordingly.
func BuildCobraCommand(c Command) *cobra.Command {
Expand All @@ -173,16 +178,21 @@ func BuildCobraCommand(c Command) *cobra.Command {
buildCommandWithArgs(cmd, c)
buildCommandWithClient(cmd, c)
buildCommandWithConfig(cmd, c)

// buildCommandWithConfirm needs to go before buildCommandWithExecute to make sure there's a confirmation prompt
// prior to execution.
buildCommandWithConfirm(cmd, c)
buildCommandWithDocs(cmd, c)
buildCommandWithFeatureFlag(cmd, c)
buildCommandWithExecute(cmd, c)

buildCommandWithDocs(cmd, c)
buildCommandWithFlags(cmd, c)
buildCommandWithHidden(cmd, c)
buildCommandWithLogger(cmd, c)
buildCommandWithNoHeaders(cmd, c)
buildCommandWithSubCommands(cmd, c)

// this has to be the last function so it captures all errors from RunE
// this has to be the last function, so it captures all errors from RunE
buildCommandEvent(cmd, c)

return cmd
Expand Down Expand Up @@ -586,3 +596,43 @@ func buildCommandWithSubCommands(cmd *cobra.Command, c Command) {
cmd.AddCommand(sub)
}
}

func hasFeatureFlag(flags []string, f string) bool {
for _, v := range flags {
if v == f {
return true
}
}

return false
}

func buildCommandWithFeatureFlag(cmd *cobra.Command, c Command) {
v, ok := c.(CommandWithFeatureFlag)
if !ok {
return
}

// Considering a command with feature flags no ready to be documented and
// publicly available.
cmd.Hidden = true

old := cmd.RunE
cmd.RunE = func(cmd *cobra.Command, args []string) error {
if old != nil {
err := old(cmd, args)
if err != nil {
return err
}
}

flagRequired := v.FeatureFlag()
userFeatureFlags := global.Config.GetStringSlice(global.UserFeatureFlagsEnv)

if !hasFeatureFlag(userFeatureFlags, flagRequired) {
return fmt.Errorf("feature flag %q required. reach out to [email protected] for more information", flagRequired)
}

return nil
}
}
63 changes: 63 additions & 0 deletions cmd/meroxa/root/environments/environments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright © 2021 Meroxa 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 environments

import (
"context"
"fmt"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/log"
)

type Environments struct {
logger log.Logger
}

var (
_ builder.CommandWithAliases = (*Environments)(nil)
_ builder.CommandWithDocs = (*Environments)(nil)
_ builder.CommandWithExecute = (*Environments)(nil)
_ builder.CommandWithFeatureFlag = (*Environments)(nil)
)

func (*Environments) Usage() string {
return "environments"
}

func (*Environments) Docs() builder.Docs {
return builder.Docs{
Short: "Manage environments on Meroxa",
}
}

func (*Environments) Aliases() []string {
return []string{"env", "environment"}
}

func (*Environments) FeatureFlag() string {
return "environments"
}

func (e *Environments) Logger(logger log.Logger) {
e.logger = logger
}

func (e *Environments) Execute(ctx context.Context) error {
fmt.Println("Welcome to a new world")
return nil
}
4 changes: 3 additions & 1 deletion cmd/meroxa/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/meroxa/cli/cmd/meroxa/root/config"
"github.com/meroxa/cli/cmd/meroxa/root/connectors"
"github.com/meroxa/cli/cmd/meroxa/root/endpoints"
"github.com/meroxa/cli/cmd/meroxa/root/environments"
"github.com/meroxa/cli/cmd/meroxa/root/login"
"github.com/meroxa/cli/cmd/meroxa/root/logout"
"github.com/meroxa/cli/cmd/meroxa/root/open"
Expand Down Expand Up @@ -79,10 +80,11 @@ meroxa resources list --types
cmd.AddCommand(builder.BuildCobraCommand(&api.API{}))
cmd.AddCommand(builder.BuildCobraCommand(&auth.Auth{}))
cmd.AddCommand(builder.BuildCobraCommand(&billing.Billing{}))
cmd.AddCommand(builder.BuildCobraCommand(&config.Config{}))
cmd.AddCommand(builder.BuildCobraCommand(&connectors.Connect{}))
cmd.AddCommand(builder.BuildCobraCommand(&connectors.Connectors{}))
cmd.AddCommand(builder.BuildCobraCommand(&endpoints.Endpoints{}))
cmd.AddCommand(builder.BuildCobraCommand(&config.Config{}))
cmd.AddCommand(builder.BuildCobraCommand(&environments.Environments{}))
cmd.AddCommand(builder.BuildCobraCommand(&login.Login{}))
cmd.AddCommand(builder.BuildCobraCommand(&logout.Logout{}))
cmd.AddCommand(builder.BuildCobraCommand(&open.Open{}))
Expand Down

0 comments on commit 7a09af8

Please sign in to comment.