client

package
v0.0.0-...-cfbe396 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2015 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// SchemaCmd is the wrapper command for schemas
	SchemaCmd = &cobra.Command{
		Use:   "schema",
		Short: "work with schemas",
		PersistentPreRun: func(cmd *cobra.Command, args []string) {
			setupClient()
		},
	}

	// SchemaListCmd lists schemas
	SchemaListCmd = &cobra.Command{
		Use:   "list",
		Short: "list schemas",
		Long:  "List schemas, optionally pretty-printed. In successful cases, this command prints JSON. This command corresponds to calling `GET /v1/schemas`.",
		Run: func(cmd *cobra.Command, args []string) {
			resp, err := client.Do(
				"GET",
				"/v1/schemas",
				map[string]interface{}{},
				nil,
			)
			if err != nil {
				log.Fatal(err)
			}

			client.HandleResponse(resp)
		},
	}

	// SchemaSubmitCmd submits a schema
	SchemaSubmitCmd = &cobra.Command{
		Use:   "submit {schema.json}",
		Short: "submit a schema",
		Long:  "Create or update a schema in the remote store. This is specified as `schema.json`, a path to a JSON file on disk. If the `--set-defaults` flag is set, the API will immediately set any defaults specified in the file. In successful cases, this command prints JSON equivalent to the schema which was persisted. This command corresponds to calling `POST /v1/schemas` with the given flag. The `--set-defaults` flag corresponds to the `setDefaults` query string option.",
		PreRunE: func(cmd *cobra.Command, args []string) error {
			if len(args) != 1 {
				return errors.New("expected exactly one JSON file as an argument")
			}

			return nil
		},
		Run: func(cmd *cobra.Command, args []string) {

			body, err := ioutil.ReadFile(args[0])
			if err != nil {
				log.Fatal(err)
			}

			var dest interface{}
			err = json.Unmarshal(body, &dest)
			if err != nil {
				log.Fatal(err)
			}

			schema, err := app.LoadSchema(dest)
			if err != nil {
				log.Fatal(err)
			}

			err = schema.Validate()
			if err != nil {
				log.Fatal(err)
			}

			resp, err := client.Do(
				"POST",
				"/v1/schemas",
				map[string]interface{}{
					"setDefaults": viper.Get("set-defaults"),
				},
				bytes.NewBuffer(body),
			)
			if err != nil {
				log.Fatal(err)
			}

			client.HandleResponse(resp)
		},
	}

	// SchemaShowCmd shows a schema
	SchemaShowCmd = &cobra.Command{
		Use:   "show {name}",
		Short: "show a schema",
		Long:  "Show a schema, specified by `name`. In successful cases, this command prints JSON. This command corresponds to calling `GET /v1/schemas/{name}`.",
		PreRunE: func(cmd *cobra.Command, args []string) error {
			if len(args) != 1 {
				return errors.New("expected exactly one name as an argument")
			}

			return nil
		},
		Run: func(cmd *cobra.Command, args []string) {
			resp, err := client.Do(
				"GET",
				path.Join("/v1/schemas", args[0]),
				map[string]interface{}{},
				nil,
			)
			if err != nil {
				log.Fatal(err)
			}

			client.HandleResponse(resp)
		},
	}

	// SchemaDeleteCmd deletes a schema
	SchemaDeleteCmd = &cobra.Command{
		Use:   "delete {name}",
		Short: "delete a schema",
		Long:  "Delete an existing schema, specified by `name`. If the `--delete-keys` flag is set, the API will also delete any keys that are specified in the schema being deleted. In successful cases, this command does not print anything. This command corresponds to calling `DELETE /v1/schemas/{name}`. The `--delete-keys` flag corresponds to the `deleteKeys` query string option.",
		PreRunE: func(cmd *cobra.Command, args []string) error {
			if len(args) != 1 {
				return errors.New("expected exactly one schema name as an argument")
			}

			return nil
		},
		Run: func(cmd *cobra.Command, args []string) {
			resp, err := client.Do(
				"DELETE",
				path.Join("/v1/schemas", args[0]),
				map[string]interface{}{
					"deleteKeys": viper.Get("delete-keys"),
				},
				nil,
			)
			if err != nil {
				log.Fatal(err)
			}

			client.HandleResponse(resp)
		},
	}

	// SchemaValidateCmd submits a schema
	SchemaValidateCmd = &cobra.Command{
		Use:   "validate {schema.json}",
		Short: "validate a schema",
		Long:  "Validate the schema specified as `schema.json`, a path to a JSON file on disk. If it is valid, the command prints \"OK\". This command has no corresponding API call, but the creation/update logic on the server performs the same validation.",
		PreRunE: func(cmd *cobra.Command, args []string) error {
			if len(args) != 1 {
				return errors.New("expected exactly one JSON file as an argument")
			}

			return nil
		},
		Run: func(cmd *cobra.Command, args []string) {

			body, err := ioutil.ReadFile(args[0])
			if err != nil {
				log.Fatal(err)
			}

			var dest interface{}
			err = json.Unmarshal(body, &dest)
			if err != nil {
				log.Fatal(err)
			}

			schema, err := app.LoadSchema(dest)
			if err != nil {
				log.Fatal(err)
			}

			err = schema.Validate()
			if err != nil {
				log.Fatal(err)
			}

			fmt.Println("OK")
		},
	}
)
View Source
var (
	// ValueCmd is the wrapper command for values
	ValueCmd = &cobra.Command{
		Use:   "value",
		Short: "work with values",
		PersistentPreRun: func(cmd *cobra.Command, args []string) {
			setupClient()
		},
	}

	// ValueListCmd lists values in a schema
	ValueListCmd = &cobra.Command{
		Use:   "list {schema name}",
		Short: "list the values in a schema",
		Long:  "List values in the schema specified by `schema name`. In successful cases, this command prints JSON. This command corresponds to calling `GET /v1/schemas/{schema name}/values`.",
		PreRunE: func(cmd *cobra.Command, args []string) error {
			if len(args) != 1 {
				return errors.New("expected exactly one name as an argument")
			}

			return nil
		},
		Run: func(cmd *cobra.Command, args []string) {
			resp, err := client.Do(
				"GET",
				path.Join("/v1/schemas", args[0], "values"),
				map[string]interface{}{},
				nil,
			)
			if err != nil {
				log.Fatal(err)
			}

			client.HandleResponse(resp)
		},
	}

	// ValueWriteCmd writes a value
	ValueWriteCmd = &cobra.Command{
		Use:   "write {schema name} {value name} {value}",
		Short: "write a value",
		Long:  "Write a value to the specified schema and value in the remote store. In successful cases, the new value will be printed as JSON. This command corresponds to calling `PUT /v1/schemas/{schema name}/values/{value name}` with `{value}` as the JSON payload. Because of this, `value` must be valid JSON.",
		PreRunE: func(cmd *cobra.Command, args []string) error {
			if len(args) != 3 {
				return errors.New("exected exactly two arguments: schema name, value name, and value")
			}

			return nil
		},
		Run: func(cmd *cobra.Command, args []string) {
			resp, err := client.Do(
				"PUT",
				path.Join("/v1/schemas", args[0], "values", args[1]),
				map[string]interface{}{},
				bytes.NewBufferString(args[2]),
			)
			if err != nil {
				log.Fatal(err)
			}

			client.HandleResponse(resp)
		},
	}

	// ValueShowCmd shows a value
	ValueShowCmd = &cobra.Command{
		Use:   "show {schema name} {value name}",
		Short: "show a single value in a schema",
		Long:  "Show a value specifed by `{schema name}` and `{value name}`. In successful cases, the value will be printed as JSON. This command corresponds to `GET /v1/schemas/{schema name}/values/{value name}`.",
		PreRunE: func(cmd *cobra.Command, args []string) error {
			if len(args) != 2 {
				return errors.New("exected exactly two arguments: schema name and value name")
			}

			return nil
		},
		Run: func(cmd *cobra.Command, args []string) {
			resp, err := client.Do(
				"GET",
				path.Join("/v1/schemas", args[0], "values", args[1]),
				map[string]interface{}{},
				nil,
			)
			if err != nil {
				log.Fatal(err)
			}

			client.HandleResponse(resp)
		},
	}

	// ValueDeleteCmd deletes a value
	ValueDeleteCmd = &cobra.Command{
		Use:   "delete {schema name} {value name}",
		Short: "delete a single value in a schema",
		Long:  "Delete a single value specified by `{schema name}` and `{value name}`. In the semantics of the API, this will delete the value if not required and no default set, set the default value if present, and refuse to delete the key if required. This command corresponds to `DELETE /v1/schemas/{schema name}/values/{value name}`.",
		PreRunE: func(cmd *cobra.Command, args []string) error {
			if len(args) != 2 {
				return errors.New("exected exactly two arguments: schema name and value name")
			}

			return nil
		},
		Run: func(cmd *cobra.Command, args []string) {
			resp, err := client.Do(
				"DELETE",
				path.Join("/v1/schemas", args[0], "values", args[1]),
				map[string]interface{}{},
				nil,
			)
			if err != nil {
				log.Fatal(err)
			}

			client.HandleResponse(resp)
		},
	}
)

Functions

func SchemaFlags

func SchemaFlags()

SchemaFlags sets up flags for the client commands

func ValueFlags

func ValueFlags()

ValueFlags sets up flags for the client commands

Types

type Client

type Client struct {
	Host      string
	Scheme    string
	UserAgent string
	// contains filtered or unexported fields
}

Client is a client for Gestalt

func New

func New() *Client

New returns a new client

func (*Client) Do

func (c *Client) Do(method, path string, query map[string]interface{}, body io.Reader) (*http.Response, error)

Do makes a request

func (*Client) HandleResponse

func (c *Client) HandleResponse(resp *http.Response)

HandleResponse handles the response for a command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL