Skip to content

Commit

Permalink
feat(add): Use add instead of create for resources (#41)
Browse files Browse the repository at this point in the history
* Add .gitignore

* Move add resource to its own command

* Update gitignore

* Make name, url, and credentials required

* return err so it’s handled in main

* Use `stringVarP` to fetch flags

* Update README.md

* Update command description
  • Loading branch information
raulb authored Feb 3, 2021
1 parent f8b9116 commit 193b55f
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 98 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/meroxa
/cli
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ All commands start with `meroxa` (the name of the binary).

### Examples

* Create Resource:
* Add a resource:
```
meroxa create resource postgres --name mypg --url postgres://user:secret@localhost:5432/db
meroxa add resource postgres --name mypg --url postgres://user:secret@localhost:5432/db
```
* Create Connection:
* Create a connection:
```
meroxa create connection mypg --config '{"table.whitelist":"public.purchases"}'
```
* List Resources:
* List resources:
```
meroxa list resources
```
Expand Down
119 changes: 119 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
Copyright © 2020 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 cmd

import (
"context"
"encoding/json"
"fmt"

"time"

"github.com/meroxa/meroxa-go"
"github.com/spf13/cobra"
)

const clientTimeOut = 5 * time.Second

var resName, resURL, resCredentials, resMetadata string

var addCmd = &cobra.Command{
Use: "add",
Short: "add a meroxa resource",
Long: `Use the add command to add various Meroxa resources to your account.`,
}

var addResourceCmd = &cobra.Command{
Use: "resource <resource-type>",
Short: "Add a resource to your account",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
resType := args[0]
c, err := client()

if err != nil {
return err
}

r := meroxa.Resource{
Kind: resType,
Name: resName,
URL: resURL,
// We're not doing anything with `config` in the CLI.
// Maybe deprecate this altogether in the client.
Configuration: nil,
Metadata: nil,
}

// TODO: Figure out best way to handle creds, config and metadata
// Get credentials (expect a JSON string)
if resCredentials != "" {
var creds meroxa.Credentials
err = json.Unmarshal([]byte(resCredentials), &creds)
if err != nil {
return err
}

r.Credentials = &creds
}

if resMetadata != "" {
var metadata map[string]string
err = json.Unmarshal([]byte(resMetadata), &metadata)
if err != nil {
return err
}

r.Metadata = metadata
}

ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, clientTimeOut)
defer cancel()

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

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

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

return nil
},
}

func init() {
rootCmd.AddCommand(addCmd)
addCmd.AddCommand(addResourceCmd)

addResourceCmd.Flags().StringVarP(&resName, "name", "n", "foo", "resource name")
addResourceCmd.MarkFlagRequired("name")

addResourceCmd.Flags().StringVarP(&resURL, "url", "u", "", "resource url")
addResourceCmd.MarkFlagRequired("url")

addResourceCmd.Flags().StringVarP(&resCredentials, "credentials", "", "", "resource credentials")
addResourceCmd.Flags().StringVarP(&resMetadata, "metadata", "m", "", "resource metadata")
}
98 changes: 4 additions & 94 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,94 +30,12 @@ import (
var createCmd = &cobra.Command{
Use: "create",
Short: "create meroxa pipeline components",
Long: `use the create command to create various Meroxa pipeline components
including Resources, Connectors and Functions.`,
}

var createResourceCmd = &cobra.Command{
Use: "resource <resource-type>",
Short: "create resource",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {

// Resource Type
resType := args[0]

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

// Assemble resource struct from config
name, err := cmd.Flags().GetString("name")
if err != nil {
fmt.Println("Error: ", err)
}

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

r := meroxa.Resource{
Kind: resType,
Name: name,
URL: u,
Configuration: nil,
Metadata: nil,
}

// TODO: Figure out best way to handle creds, config and metadata
// Get credentials (expect a JSON string)
credsString, err := cmd.Flags().GetString("credentials")
if err != nil {
fmt.Println("Error: ", err)
}
if credsString != "" {
var creds meroxa.Credentials
err = json.Unmarshal([]byte(credsString), &creds)
if err != nil {
fmt.Println("Error: ", err)
}

r.Credentials = &creds
}
Long: `Use the create command to create various Meroxa pipeline components
including Connectors and Functions.
metadataString, err := cmd.Flags().GetString("metadata")
if err != nil {
fmt.Println("Error: ", err)
}
if metadataString != "" {
var metadata map[string]string
err = json.Unmarshal([]byte(metadataString), &metadata)
if err != nil {
fmt.Println("Error: ", err)
}

r.Metadata = metadata
}

ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

if !flagRootOutputJSON {
fmt.Printf("Creating %s Resource...\n", resType)
}
If you need to add a resource, try:
res, err := c.CreateResource(ctx, &r)
if err != nil {
fmt.Println("Error: ", err)
return
}

if flagRootOutputJSON {
jsonPrint(res)
} else {
fmt.Println("Resource successfully created!")
prettyPrint("resource", res)
}
},
$ meroxa add resource <resource-type> [name]`,
}

var createConnectorCmd = &cobra.Command{
Expand Down Expand Up @@ -258,14 +176,6 @@ var createPipelineCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(createCmd)

// Subcommands
createCmd.AddCommand(createResourceCmd)
createResourceCmd.Flags().StringP("name", "n", "", "resource name")
createResourceCmd.Flags().StringP("url", "u", "", "resource url")
createResourceCmd.Flags().String("credentials", "", "resource credentials")
createResourceCmd.Flags().StringP("config", "c", "", "resource configuration")
createResourceCmd.Flags().StringP("metadata", "m", "", "resource metadata")

createCmd.AddCommand(createConnectorCmd)
createConnectorCmd.Flags().StringP("name", "n", "", "connector name")
createConnectorCmd.Flags().StringP("config", "c", "", "connector configuration")
Expand Down

0 comments on commit 193b55f

Please sign in to comment.