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

CLI improvement to facilitate acceptance tests #35

Merged
merged 3 commits into from
Nov 4, 2020
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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ SHELL=/bin/bash -o pipefail
build:
go build -o meroxa

.PHONY: install
install:
go build -o $$(go env GOPATH)/bin/meroxa

PRIVATE_REPOS = github.com/meroxa/meroxa-go
.PHONY: gomod
gomod:
Expand Down
80 changes: 57 additions & 23 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,52 @@ import (
"github.com/spf13/cobra"
)

const MeroxaDirPath = ".config/meroxa"
const ConfigFileName = "meroxa.config"
const (
MeroxaDirPath = ".config/meroxa"
ConfigFileName = "meroxa.config"
)

var (
flagSignupUsername string
flagSignupPassword string
flagSignupEmail string

flagLoginUsername string
flagLoginPassword string
)

var signupCmd = &cobra.Command{
Use: "signup",
Short: "sign up to the Meroxa platform",
RunE: func(cmd *cobra.Command, args []string) error {
u, err := prompt("Username", usernameValidator, false)
if err != nil {
fmt.Println("Username invalid: ", err)
return err
var err error

if flagSignupUsername == "" {
flagSignupUsername, err = prompt("Username", usernameValidator, false)
if err != nil {
fmt.Println("Username invalid: ", err)
return err
}
}
p, err := prompt("Password", passwordValidator, true)
if err != nil {
fmt.Println("Password invalid: ", err)
return err

if flagSignupPassword == "" {
flagSignupPassword, err = prompt("Password", passwordValidator, true)
if err != nil {
fmt.Println("Password invalid: ", err)
return err
}
}
e, err := prompt("Email", emailValidator, false)
if err != nil {
fmt.Println("Email invalid: ", err)
return err

if flagSignupEmail == "" {
flagSignupEmail, err = prompt("Email", emailValidator, false)
if err != nil {
fmt.Println("Email invalid: ", err)
return err
}
}

fmt.Println("Registering user...")
err = signup(u, p, e)
err = signup(flagSignupUsername, flagSignupPassword, flagSignupEmail)
if err != nil {
return err
}
Expand All @@ -74,21 +95,28 @@ var loginCmd = &cobra.Command{
Use: "login",
Short: "log into the Meroxa platform",
RunE: func(cmd *cobra.Command, args []string) error {
u, err := prompt("Username", usernameValidator, false)
if err != nil {
return err
var err error

if flagLoginUsername == "" {
flagLoginUsername, err = prompt("Username", usernameValidator, false)
if err != nil {
return err
}
}
p, err := prompt("Password", passwordValidator, true)
if err != nil {
return err

if flagLoginPassword == "" {
flagLoginPassword, err = prompt("Password", passwordValidator, true)
if err != nil {
return err
}
}

err = verifyCredentials(u, p)
err = verifyCredentials(flagLoginUsername, flagLoginPassword)
if err != nil {
return err
}

err = saveCreds(u, p)
err = saveCreds(flagLoginUsername, flagLoginPassword)
if err != nil {
return err
}
Expand Down Expand Up @@ -128,6 +156,9 @@ var whoAmICmd = &cobra.Command{
func init() {
// Login
rootCmd.AddCommand(loginCmd)
loginCmd.PersistentFlags().StringVar(&flagLoginUsername, "username", "", "username")
loginCmd.PersistentFlags().StringVar(&flagLoginPassword, "password", "", "password")

// Subcommands
loginCmd.AddCommand(whoAmICmd)

Expand All @@ -136,6 +167,9 @@ func init() {

// Signup
rootCmd.AddCommand(signupCmd)
signupCmd.PersistentFlags().StringVar(&flagSignupUsername, "username", "", "username")
signupCmd.PersistentFlags().StringVar(&flagSignupPassword, "password", "", "password")
signupCmd.PersistentFlags().StringVar(&flagSignupEmail, "email", "", "email")

// Here you will define your flags and configuration settings.

Expand Down
38 changes: 29 additions & 9 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,22 @@ var createResourceCmd = &cobra.Command{
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

fmt.Printf("Creating %s Resource...\n", resType)
if !flagRootOutputJson {
fmt.Printf("Creating %s Resource...\n", resType)
}

res, err := c.CreateResource(ctx, &r)
if err != nil {
fmt.Println("Error: ", err)
return
}

fmt.Println("Resource successfully created!")
prettyPrint("resource", res)
if flagRootOutputJson {
jsonPrint(res)
} else {
fmt.Println("Resource successfully created!")
prettyPrint("resource", res)
}
},
}

Expand Down Expand Up @@ -148,14 +154,22 @@ var createConnectionCmd = &cobra.Command{
return
}

fmt.Println("Creating connection...")
if !flagRootOutputJson {
fmt.Println("Creating connection...")
}

con, err := createConnection(resName, cfg, input)
if err != nil {
fmt.Println("Error: ", err)
return
}
fmt.Println("Connection successfully created!")
prettyPrint("connector", con)

if flagRootOutputJson {
jsonPrint(con)
} else {
fmt.Println("Connection successfully created!")
prettyPrint("connector", con)
}
},
}

Expand Down Expand Up @@ -201,16 +215,22 @@ var createPipelineCmd = &cobra.Command{
p.Metadata = metadata
}

fmt.Print("Creating Pipeline...\n")
if !flagRootOutputJson {
fmt.Println("Creating Pipeline...")
}

res, err := c.CreatePipeline(ctx, p)
if err != nil {
fmt.Println("Error: ", err)
return
}

fmt.Println("Pipeline successfully created!")
prettyPrint("pipeline", res)
if flagRootOutputJson {
jsonPrint(res)
} else {
fmt.Println("Pipeline successfully created!")
prettyPrint("pipeline", res)
}
},
}

Expand Down
16 changes: 13 additions & 3 deletions cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import (
"context"
"errors"
"fmt"
"github.com/meroxa/meroxa-go"
"time"

"github.com/meroxa/meroxa-go"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -53,7 +54,11 @@ var describeResourceCmd = &cobra.Command{
fmt.Println("Error: ", err)
}

prettyPrint("resource", res)
if flagRootOutputJson {
jsonPrint(res)
} else {
prettyPrint("resource", res)
}
},
}

Expand Down Expand Up @@ -83,7 +88,12 @@ var describeConnectionCmd = &cobra.Command{
fmt.Println("Error: ", err)
return
}
prettyPrint("connection", conn)

if flagRootOutputJson {
jsonPrint(conn)
} else {
prettyPrint("connection", conn)
}
},
}

Expand Down
16 changes: 13 additions & 3 deletions cmd/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ package cmd
import (
"encoding/json"
"fmt"
"github.com/alexeyco/simpletable"
"github.com/meroxa/meroxa-go"
"strconv"
"strings"

"github.com/alexeyco/simpletable"
"github.com/meroxa/meroxa-go"
)

func jsonPrint(data interface{}) {
p, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s\n", p)
}

func prettyPrint(section string, data interface{}) {
var p []byte
// var err := error
Expand All @@ -18,7 +28,7 @@ func prettyPrint(section string, data interface{}) {
return
}
fmt.Printf("== %s ==\n", strings.ToTitle(section))
fmt.Printf("%s \n", p)
fmt.Printf("%s\n", p)
}

func printResourcesTable(resources []*meroxa.Resource) {
Expand Down
47 changes: 14 additions & 33 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,16 @@ var listResourcesCmd = &cobra.Command{
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

output, err := cmd.Flags().GetString("output")
if err != nil {
fmt.Println("Error: ", err)
return
}

rr, err := c.ListResources(ctx)
if err != nil {
fmt.Println("Error: ", err)
}

switch output {
case "json":
prettyPrint("resources", rr)
default:
if flagRootOutputJson {
jsonPrint(rr)
} else {
printResourcesTable(rr)
}

},
}

Expand All @@ -78,21 +70,14 @@ var listConnectionsCmd = &cobra.Command{
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

output, err := cmd.Flags().GetString("output")
if err != nil {
fmt.Println("Error: ", err)
return
}

connections, err := c.ListConnections(ctx)
if err != nil {
fmt.Println("Error: ", err)
}

switch output {
case "json":
prettyPrint("connections", connections)
default:
if flagRootOutputJson {
jsonPrint(connections)
} else {
printConnectionsTable(connections)
}
},
Expand All @@ -111,21 +96,14 @@ var listResourceTypesCmd = &cobra.Command{
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

output, err := cmd.Flags().GetString("output")
if err != nil {
fmt.Println("Error: ", err)
return
}

resTypes, err := c.ListResourceTypes(ctx)
if err != nil {
fmt.Println("Error: ", err)
}

switch output {
case "json":
prettyPrint("resource types", resTypes)
default:
if flagRootOutputJson {
jsonPrint(resTypes)
} else {
printResourceTypesTable(resTypes)
}
},
Expand All @@ -149,15 +127,18 @@ var listPipelinesCmd = &cobra.Command{
fmt.Println("Error: ", err)
}

prettyPrint("pipelines", rr)
if flagRootOutputJson {
jsonPrint(rr)
} else {
prettyPrint("pipelines", rr)
}
},
}

func init() {
rootCmd.AddCommand(listCmd)

// Subcommands
listCmd.PersistentFlags().StringP("output", "o", "table", "output format [json|table]")
listCmd.AddCommand(listResourcesCmd)
listCmd.AddCommand(listConnectionsCmd)
listCmd.AddCommand(listResourceTypesCmd)
Expand Down
Loading