Skip to content

Commit

Permalink
return error if meroxa.config file is missing/assume user is not logg…
Browse files Browse the repository at this point in the history
…ed in (#38)

Co-authored-by: Raúl <raul@meroxa.io>
  • Loading branch information
ahmeroxa and raulb authored Feb 5, 2021
1 parent 6305694 commit d66a062
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 57 deletions.
6 changes: 6 additions & 0 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ var (

flagLoginUsername string
flagLoginPassword string

ErrNotLoggedIn = errors.New("you do not appear to be logged in to the Meroxa Platform")
)

var signupCmd = &cobra.Command{
Expand Down Expand Up @@ -202,8 +204,12 @@ func readCreds() (string, string, error) {
if err != nil {
return "", "", err
}

dat, err := ioutil.ReadFile(filePath)
if err != nil {
if os.IsNotExist(err) {
return "", "", ErrNotLoggedIn
}
return "", "", err
}

Expand Down
27 changes: 11 additions & 16 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,55 +42,49 @@ var createConnectorCmd = &cobra.Command{
Use: "connector",
Short: "create connector",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
// Resource Name
resName := args[0]

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

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

cfg := &Config{}
if cfgString != "" {
err = json.Unmarshal([]byte(cfgString), cfg)
if err != nil {
fmt.Println("Error: ", err)
return
return err
}
}

// Process metadata
metadataString, err := cmd.Flags().GetString("metadata")
if err != nil {
fmt.Println("Error: ", err)
return err
}
metadata := map[string]string{}
if metadataString != "" {
err = json.Unmarshal([]byte(metadataString), &metadata)
if err != nil {
fmt.Println("Error: ", err)
return
return err
}
}

// merge in input
input, err := cmd.Flags().GetString("input")
if err != nil {
fmt.Println("Error: ", err)
return
return err
}
err = cfg.Set("input", input)
if err != nil {
fmt.Println("Error: ", err)
return
return err
}

if !flagRootOutputJSON {
Expand All @@ -99,8 +93,7 @@ var createConnectorCmd = &cobra.Command{

con, err := createConnector(name, resName, cfg, metadata, input)
if err != nil {
fmt.Println("Error: ", err)
return
return err
}

if flagRootOutputJSON {
Expand All @@ -109,6 +102,8 @@ var createConnectorCmd = &cobra.Command{
fmt.Println("Connector successfully created!")
prettyPrint("connector", con)
}

return nil
},
}

Expand Down
16 changes: 10 additions & 6 deletions cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,28 @@ var describeResourceCmd = &cobra.Command{
Use: "resource <name>",
Short: "describe resource",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]

c, err := client()
if err != nil {
fmt.Println("Error: ", err)
return err
}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

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

if flagRootOutputJSON {
jsonPrint(res)
} else {
prettyPrint("resource", res)
}
return nil
},
}

Expand All @@ -72,29 +73,32 @@ var describeConnectorCmd = &cobra.Command{
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
var (
err error
conn *meroxa.Connector
)
name := args[0]
c, err := client()
if err != nil {
return err
}

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

conn, err = c.GetConnectorByName(ctx, name)
if err != nil {
fmt.Println("Error: ", err)
return
return err
}

if flagRootOutputJSON {
jsonPrint(conn)
} else {
prettyPrint("connector", conn)
}
return nil
},
}

Expand Down
39 changes: 18 additions & 21 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ var destroyResourceCmd = &cobra.Command{
Use: "resource <name>",
Short: "destroy resource",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
// Resource Name
resName := args[0]

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

// get Resource ID from name
Expand All @@ -53,13 +52,12 @@ var destroyResourceCmd = &cobra.Command{

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

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

ctx = context.Background()
Expand All @@ -68,25 +66,25 @@ var destroyResourceCmd = &cobra.Command{

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

prettyPrint("resource destroyed", res)
return nil
},
}

var destroyConnectorCmd = &cobra.Command{
Use: "connector <name>",
Short: "destroy connector",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
// Connector Name
conName := args[0]

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

// get Connector ID from name
Expand All @@ -96,13 +94,12 @@ var destroyConnectorCmd = &cobra.Command{

con, err := c.GetConnectorByName(ctx, conName)
if err != nil {
fmt.Println("Error: ", err)
return
return err
}

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

ctx = context.Background()
Expand All @@ -111,10 +108,11 @@ var destroyConnectorCmd = &cobra.Command{

err = c.DeleteConnector(ctx, con.ID)
if err != nil {
fmt.Println("Error: ", err)
return err
}

prettyPrint("connector destroyed", con)
return nil
},
}

Expand All @@ -130,14 +128,13 @@ var destroyPipelineCmd = &cobra.Command{
Use: "pipeline <name>",
Short: "destroy pipeline",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
// Pipeline Name
pipelineName := args[0]

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

// get Pipeline ID from name
Expand All @@ -147,13 +144,12 @@ var destroyPipelineCmd = &cobra.Command{

pipeline, err := c.GetPipelineByName(ctx, pipelineName)
if err != nil {
fmt.Println("Error: ", err)
return
return err
}

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

ctx = context.Background()
Expand All @@ -162,10 +158,11 @@ var destroyPipelineCmd = &cobra.Command{

err = c.DeletePipeline(ctx, pipeline.ID)
if err != nil {
fmt.Println("Error: ", err)
return err
}

prettyPrint("pipeline destroyed", pipeline)
return nil
},
}

Expand Down
Loading

0 comments on commit d66a062

Please sign in to comment.