Skip to content

Commit

Permalink
Merge branch 'hidden_env_flags' of https://github.com/meroxa/cli
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-cross committed Jan 18, 2022
2 parents 2c574d6 + 44d29a0 commit 3917e6b
Show file tree
Hide file tree
Showing 76 changed files with 1,279 additions and 81 deletions.
28 changes: 20 additions & 8 deletions cmd/meroxa/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ type CommandWithSubCommands interface {

type CommandWithFeatureFlag interface {
Command
FeatureFlag() string
FeatureFlag() (string, error)
}

// BuildCobraCommand takes a Command and builds a *cobra.Command from it. It figures out if the command implements any
Expand Down Expand Up @@ -666,10 +666,6 @@ func buildCommandWithFeatureFlag(cmd *cobra.Command, c Command) {
return
}

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

old := cmd.PreRunE
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
if old != nil {
Expand All @@ -679,14 +675,30 @@ func buildCommandWithFeatureFlag(cmd *cobra.Command, c Command) {
}
}

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

if !hasFeatureFlag(userFeatureFlags, flagRequired) {
return fmt.Errorf("your account does not have access to the %q feature. "+
"Reach out to [email protected] for more information", flagRequired)
return err
}

return nil
}
}

func CheckFeatureFlag(exec Command, cmd CommandWithFeatureFlag) error {
if global.Config == nil {
c := BuildCobraCommand(exec)
_ = global.PersistentPreRunE(c)
}
flagRequired, err := cmd.FeatureFlag()
if global.Config.Get(global.UserFeatureFlagsEnv) == "" {
return err
}
userFeatureFlags := global.Config.GetStringSlice(global.UserFeatureFlagsEnv)

if !hasFeatureFlag(userFeatureFlags, flagRequired) {
return err
}
return nil
}
7 changes: 5 additions & 2 deletions cmd/meroxa/root/environments/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package environments

import (
"fmt"

"github.com/spf13/cobra"

"github.com/meroxa/cli/cmd/meroxa/builder"
Expand Down Expand Up @@ -48,8 +50,9 @@ func (*Environments) Aliases() []string {
return []string{"env", "environment"}
}

func (*Environments) FeatureFlag() string {
return "environments"
func (*Environments) FeatureFlag() (string, error) {
return "environments", fmt.Errorf(`no access to the Meroxa self-hosted environments feature.
Sign up for the Beta here: https://share.hsforms.com/1Uq6UYoL8Q6eV5QzSiyIQkAc2sme`)
}

func (e *Environments) Logger(logger log.Logger) {
Expand Down
13 changes: 10 additions & 3 deletions cmd/meroxa/root/pipelines/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/google/uuid"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/cmd/meroxa/root/environments"
"github.com/meroxa/cli/log"
"github.com/meroxa/meroxa-go/pkg/meroxa"
)
Expand Down Expand Up @@ -53,7 +54,7 @@ type Create struct {
flags struct {
Metadata string `long:"metadata" short:"m" usage:"pipeline metadata"`
// TODO: Add support to builder to create flags with an alias (--env | --environment)
Environment string `long:"env" usage:"environment (name or UUID) where pipeline will be created" hidden:"true"`
Environment string `long:"env" usage:"environment (name or UUID) where pipeline will be created"`
}
}

Expand All @@ -74,11 +75,17 @@ func (c *Create) Execute(ctx context.Context) error {
p.Metadata = metadata
}

if c.flags.Environment != "" {
// If the environment specified is not the common environment.
if c.flags.Environment != "" && c.flags.Environment != string(meroxa.EnvironmentTypeCommon) {
err := builder.CheckFeatureFlag(c, &environments.Environments{})
if err != nil {
return err
}

env = c.flags.Environment
p.Environment = &meroxa.EnvironmentIdentifier{}

_, err := uuid.Parse(c.flags.Environment)
_, err = uuid.Parse(c.flags.Environment)

if err == nil {
p.Environment.UUID = c.flags.Environment
Expand Down
89 changes: 79 additions & 10 deletions cmd/meroxa/root/pipelines/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
"reflect"
"testing"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/utils"

"github.com/golang/mock/gomock"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/cmd/meroxa/global"
"github.com/meroxa/cli/log"
"github.com/meroxa/cli/utils"
"github.com/meroxa/meroxa-go/pkg/meroxa"
"github.com/meroxa/meroxa-go/pkg/mock"
)
Expand Down Expand Up @@ -65,8 +65,8 @@ func TestCreatePipelineFlags(t *testing.T) {
shorthand string
hidden bool
}{
{name: "metadata", required: false, shorthand: "m", hidden: false},
{name: "env", required: false, hidden: true},
{name: "metadata", shorthand: "m"},
{name: "env"},
}

c := builder.BuildCobraCommand(&Create{})
Expand Down Expand Up @@ -164,6 +164,16 @@ func TestCreatePipelineWithEnvironmentExecution(t *testing.T) {
pName := "my-pipeline"
env := "my-env"

c := &Create{
client: client,
logger: logger,
}
// Set up feature flags
if global.Config == nil {
build := builder.BuildCobraCommand(c)
_ = global.PersistentPreRunE(build)
}

pi := &meroxa.CreatePipelineInput{
Name: pName,
Environment: &meroxa.EnvironmentIdentifier{Name: env},
Expand All @@ -189,14 +199,22 @@ func TestCreatePipelineWithEnvironmentExecution(t *testing.T) {
).
Return(p, nil)

c := &Create{
client: client,
logger: logger,
}

c.args.Name = pi.Name
c.flags.Environment = pi.Environment.Name

// override feature flags
featureFlags := global.Config.Get(global.UserFeatureFlagsEnv)
startingFlags := ""
if featureFlags != nil {
startingFlags = featureFlags.(string)
}
newFlags := ""
if startingFlags != "" {
newFlags = startingFlags + " "
}
newFlags += "environments"
global.Config.Set(global.UserFeatureFlagsEnv, newFlags)

err := c.Execute(ctx)

if err != nil {
Expand All @@ -222,4 +240,55 @@ Pipeline %q successfully created!
if !reflect.DeepEqual(gotPipeline, *p) {
t.Fatalf("expected \"%v\", got \"%v\"", *p, gotPipeline)
}

// Clear environments feature flags
global.Config.Set(global.UserFeatureFlagsEnv, startingFlags)
}

func TestCreatePipelineWithEnvironmentExecutionWithoutFeatureFlag(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
client := mock.NewMockClient(ctrl)
logger := log.NewTestLogger()
pName := "my-pipeline"
env := "my-env"

c := &Create{
client: client,
logger: logger,
}

pi := &meroxa.CreatePipelineInput{
Name: pName,
Environment: &meroxa.EnvironmentIdentifier{Name: env},
}

p := &meroxa.Pipeline{
ID: 1,
Name: pName,
Environment: &meroxa.EnvironmentIdentifier{
UUID: "2560fbcc-b9ee-461a-a959-fa5656422dc2",
Name: env,
},
State: "healthy",
}

p.Name = pName

c.args.Name = pi.Name
c.flags.Environment = pi.Environment.Name

err := c.Execute(ctx)

if err == nil {
t.Fatalf("unexpected success")
}

gotError := err.Error()
wantError := `no access to the Meroxa self-hosted environments feature.
Sign up for the Beta here: https://share.hsforms.com/1Uq6UYoL8Q6eV5QzSiyIQkAc2sme`

if gotError != wantError {
t.Fatalf("expected error:\n%s\ngot:\n%s", wantError, gotError)
}
}
14 changes: 11 additions & 3 deletions cmd/meroxa/root/resources/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"fmt"

"github.com/google/uuid"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/cmd/meroxa/root/environments"
"github.com/meroxa/cli/log"
"github.com/meroxa/meroxa-go/pkg/meroxa"
)
Expand All @@ -45,7 +47,7 @@ type Create struct {
Metadata string `long:"metadata" short:"m" usage:"resource metadata"`

// TODO: Add support to builder to create flags with an alias (--env | --environment)
Environment string `long:"env" usage:"environment (name or UUID) where resource will be created" hidden:"true"`
Environment string `long:"env" usage:"environment (name or UUID) where resource will be created"`

// credentials
Username string `long:"username" short:"" usage:"username"`
Expand Down Expand Up @@ -121,11 +123,17 @@ func (c *Create) Execute(ctx context.Context) error {
Metadata: nil,
}

if c.flags.Environment != "" {
// If the environment specified is not the common environment.
if c.flags.Environment != "" && c.flags.Environment != string(meroxa.EnvironmentTypeCommon) {
err := builder.CheckFeatureFlag(c, &environments.Environments{})
if err != nil {
return err
}

input.Environment = &meroxa.EnvironmentIdentifier{}
env = c.flags.Environment

_, err := uuid.Parse(c.flags.Environment)
_, err = uuid.Parse(c.flags.Environment)

if err == nil {
input.Environment.UUID = c.flags.Environment
Expand Down
Loading

0 comments on commit 3917e6b

Please sign in to comment.