-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add command with feature flag required
- Loading branch information
Showing
3 changed files
with
119 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 { | ||
|
@@ -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 | ||
|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters