-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(add): Use add instead of create for resources (#41)
* 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
Showing
4 changed files
with
129 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/meroxa | ||
/cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters