Skip to content
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

Add secrets command to CLI #847

Merged
merged 5 commits into from
Nov 2, 2023
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
9 changes: 8 additions & 1 deletion cmd/meroxa/root/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func addTurbineHeaders(c addHeader, lang ir.Lang, version string) {
c.AddHeader("Meroxa-CLI-App-Version", version)
}

func (a Applications) RetrieveApplicationID(ctx context.Context, client global.BasicClient, nameOrID, path string) (*Applications, error) {
func RetrieveApplicationByNameOrID(ctx context.Context, client global.BasicClient, nameOrID, path string) (*Applications, error) {
var getPath string
apps := Applications{}
if path != "" {
Expand Down Expand Up @@ -209,5 +209,12 @@ func (a Applications) RetrieveApplicationID(ctx context.Context, client global.B
} else {
return nil, fmt.Errorf("supply either ID/Name argument or --path flag")
}

if apps.TotalItems == 0 {
return nil, fmt.Errorf("no applications found")
} else if apps.TotalItems > 1 {
return nil, fmt.Errorf("multiple applications found")
}

return &apps, nil
}
13 changes: 11 additions & 2 deletions cmd/meroxa/root/apps/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,20 @@ func (d *Deploy) Execute(ctx context.Context) error {
if err != nil {
return err
}
if _, err = d.client.CollectionRequest(ctx, "POST", collectionName, "", input, nil); err != nil {

response, err := d.client.CollectionRequest(ctx, "POST", collectionName, "", input, nil)
if err != nil {
return err
}

apps := &Application{}
err = json.NewDecoder(response.Body).Decode(&apps)
if err != nil {
fmt.Println(err)
return err
}

dashboardURL := fmt.Sprintf("%s/apps/%s/detail", global.GetMeroxaAPIURL(), input.ID)
dashboardURL := fmt.Sprintf("%s/apps/%s/detail", global.GetMeroxaAPIURL(), apps.ID)
output := fmt.Sprintf("Application %q successfully deployed!\n\n ✨ To view your application, visit %s",
d.appName, dashboardURL)

Expand Down
8 changes: 4 additions & 4 deletions cmd/meroxa/root/apps/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ type Describe struct {
turbineCLI turbine.CLI
lang ir.Lang
args struct {
idOrName string
nameOrUUID string
}
flags struct {
Path string `long:"path" usage:"Path to the app directory (default is local directory)"`
}
}

func (d *Describe) Usage() string {
return "describe [IDorName] [--path pwd]"
return "describe [nameOrUUID] [--path pwd]"
}

func (d *Describe) Flags() []builder.Flag {
Expand Down Expand Up @@ -96,7 +96,7 @@ func (d *Describe) Execute(ctx context.Context) error {
}
addTurbineHeaders(d.client, d.lang, turbineVersion)

apps, err = apps.RetrieveApplicationID(ctx, d.client, d.args.idOrName, d.flags.Path)
apps, err = RetrieveApplicationByNameOrID(ctx, d.client, d.args.nameOrUUID, d.flags.Path)
if err != nil {
return err
}
Expand All @@ -121,7 +121,7 @@ func (d *Describe) Logger(logger log.Logger) {

func (d *Describe) ParseArgs(args []string) error {
if len(args) > 0 {
d.args.idOrName = args[0]
d.args.nameOrUUID = args[0]
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions cmd/meroxa/root/apps/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func TestDescribeApplicationArgs(t *testing.T) {
t.Fatalf("expected \"%s\" got \"%s\"", tt.err, err)
}

if tt.name != ar.args.idOrName {
t.Fatalf("expected \"%s\" got \"%s\"", tt.name, ar.args.idOrName)
if tt.name != ar.args.nameOrUUID {
t.Fatalf("expected \"%s\" got \"%s\"", tt.name, ar.args.nameOrUUID)
}
}
}
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestDescribeApplicationExecution(t *testing.T) {
client: client,
logger: logger,
turbineCLI: mockTurbineCLI,
args: struct{ idOrName string }{idOrName: a.Name},
args: struct{ nameOrUUID string }{nameOrUUID: a.Name},
flags: struct {
Path string "long:\"path\" usage:\"Path to the app directory (default is local directory)\""
}{Path: filepath.Join(path, appName)},
Expand Down
2 changes: 1 addition & 1 deletion cmd/meroxa/root/apps/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (l *List) Execute(ctx context.Context) error {
l.logger.Info(ctx, display.PrintList(apps.Items, displayDetails))
l.logger.JSON(ctx, apps)

output := fmt.Sprintf("✨ To view your applications, visit %s/apps", global.GetMeroxaAPIURL())
output := fmt.Sprintf("\n ✨ To view your applications, visit %s/apps", global.GetMeroxaAPIURL())
l.logger.Info(ctx, output)
return nil
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/meroxa/root/apps/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ func (o *Open) Execute(ctx context.Context) error {
o.Opener = &browserOpener{}
}

apps := &Applications{}
apps, err := apps.RetrieveApplicationID(ctx, o.client, o.args.NameOrUUID, o.flags.Path)
apps, err := RetrieveApplicationByNameOrID(ctx, o.client, o.args.NameOrUUID, o.flags.Path)
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/meroxa/root/apps/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Remove struct {
turbineCLI turbine.CLI

args struct {
idOrName string
nameOrUUID string
}
flags struct {
Path string `long:"path" usage:"Path to the app directory (default is local directory)"`
Expand All @@ -62,20 +62,20 @@ func (r *Remove) Docs() builder.Docs {
or the Application specified by the given name or UUID identifier.`,
Example: `meroxa apps remove # assumes that the Application is in the current directory
meroxa apps remove --path /my/app
meroxa apps remove IDorName`,
meroxa apps remove nameOrUUID`,
}
}

func (r *Remove) Execute(ctx context.Context) error {
if !r.flags.Force {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("To proceed, type %q or re-run this command with --force\n▸ ", r.args.idOrName)
fmt.Printf("To proceed, type %q or re-run this command with --force\n▸ ", r.args.nameOrUUID)
input, err := reader.ReadString('\n')
if err != nil {
return err
}

if r.args.idOrName != strings.TrimRight(input, "\r\n") {
if r.args.nameOrUUID != strings.TrimRight(input, "\r\n") {
return errors.New("action aborted")
}
}
Expand All @@ -101,18 +101,18 @@ func (r *Remove) Execute(ctx context.Context) error {
}
addTurbineHeaders(r.client, r.lang, turbineVersion)

apps, err = apps.RetrieveApplicationID(ctx, r.client, r.args.idOrName, r.flags.Path)
apps, err = RetrieveApplicationByNameOrID(ctx, r.client, r.args.nameOrUUID, r.flags.Path)
if err != nil {
return err
}

r.logger.Infof(ctx, "Removing application %q...", r.args.idOrName)
r.logger.Infof(ctx, "Removing application %q...", r.args.nameOrUUID)
response, err := r.client.CollectionRequest(ctx, "DELETE", collectionName, apps.Items[0].ID, nil, nil)
if err != nil {
return err
}

r.logger.Infof(ctx, "Application %q successfully removed", r.args.idOrName)
r.logger.Infof(ctx, "Application %q successfully removed", r.args.nameOrUUID)
r.logger.JSON(ctx, response)

return nil
Expand All @@ -128,7 +128,7 @@ func (r *Remove) BasicClient(client global.BasicClient) {

func (r *Remove) ParseArgs(args []string) error {
if len(args) > 0 {
r.args.idOrName = args[0]
r.args.nameOrUUID = args[0]
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/meroxa/root/apps/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func TestRemoveAppArgs(t *testing.T) {
t.Fatalf("expected \"%s\" got \"%s\"", tt.err, err)
}

if tt.name != cc.args.idOrName {
t.Fatalf("expected \"%s\" got \"%s\"", tt.name, cc.args.idOrName)
if tt.name != cc.args.nameOrUUID {
t.Fatalf("expected \"%s\" got \"%s\"", tt.name, cc.args.nameOrUUID)
}
}
}
2 changes: 2 additions & 0 deletions cmd/meroxa/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/meroxa/cli/cmd/meroxa/root/login"
"github.com/meroxa/cli/cmd/meroxa/root/logout"
"github.com/meroxa/cli/cmd/meroxa/root/open"
"github.com/meroxa/cli/cmd/meroxa/root/secrets"
"github.com/meroxa/cli/cmd/meroxa/root/version"
"github.com/meroxa/cli/cmd/meroxa/root/whoami"

Expand Down Expand Up @@ -81,6 +82,7 @@ meroxa resources list --types
cmd.AddCommand(builder.BuildCobraCommand(&open.Open{}))
cmd.AddCommand(builder.BuildCobraCommand(&version.Version{}))
cmd.AddCommand(builder.BuildCobraCommand(&whoami.WhoAmI{}))
cmd.AddCommand(builder.BuildCobraCommand(&secrets.Secrets{}))

return cmd
}
120 changes: 120 additions & 0 deletions cmd/meroxa/root/secrets/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package secrets

import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"

"github.com/meroxa/cli/log"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/cmd/meroxa/global"
"github.com/meroxa/cli/config"
)

type Create struct {
flags struct {
Data string `long:"data" usage:"Secret's data, passed as a JSON string"`
}
args struct {
secretName string
}
client global.BasicClient
config config.Config
logger log.Logger
}

var (
_ builder.CommandWithBasicClient = (*Create)(nil)
_ builder.CommandWithConfig = (*Create)(nil)
_ builder.CommandWithDocs = (*Create)(nil)
_ builder.CommandWithExecute = (*Create)(nil)
_ builder.CommandWithFlags = (*Create)(nil)
_ builder.CommandWithLogger = (*Create)(nil)
_ builder.CommandWithArgs = (*Create)(nil)
)

func (*Create) Usage() string {
return "create NAME --data '{}'"
}

func (*Create) Docs() builder.Docs {
return builder.Docs{
Short: "Create a Turbine Secret",
Long: `This command will create a secret as promted by the user.'
After successful creation, the secret can be used in a connector.
`,
Example: `meroxa secret create NAME
meroxa secret create NAME --data '{}'
`,
}
}

func (d *Create) ParseArgs(args []string) error {
if len(args) > 0 {
d.args.secretName = args[0]
}
return nil
}

func (d *Create) Config(cfg config.Config) {
d.config = cfg
}

func (d *Create) BasicClient(client global.BasicClient) {
d.client = client
}

func (d *Create) Flags() []builder.Flag {
return builder.BuildFlags(&d.flags)
}

func (d *Create) Logger(logger log.Logger) {
d.logger = logger
}

func (d *Create) Execute(ctx context.Context) error {
if d.flags.Data == "" {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("To proceed, enter the secret's data as a JSON string: ")
input, err := reader.ReadString('\n')
if err != nil {
return err
}
if len(input) == 0 {
return errors.New("action aborted")
}
d.flags.Data = strings.TrimRight(input, "\r\n")
}

secret := &Secrets{
Name: d.args.secretName,
}
err := json.Unmarshal([]byte(d.flags.Data), &secret.Data)
if err != nil {
return err
}

d.logger.Infof(ctx, "Adding a secret %q...", d.args.secretName)
response, err := d.client.CollectionRequest(ctx, "POST", collectionName, "", secret, nil)
if err != nil {
return err
}

responseSecret := Secrets{}
err = json.NewDecoder(response.Body).Decode(&responseSecret)
if err != nil {
return err
}

d.logger.Infof(ctx, "Secret %q successfully added", responseSecret.Name)
d.logger.JSON(ctx, responseSecret)
dashboardURL := fmt.Sprintf("\n ✨ To view your secrets, visit %s/secrets/%s", global.GetMeroxaAPIURL(), responseSecret.ID)
d.logger.Info(ctx, dashboardURL)

return nil
}
Loading
Loading