Add override to support YAML inputs for apps (#921)

## Changes
<!-- Summary of your changes that are easy to understand -->

Take @andrefurlan-db 's original
[commit](https://github.com/databricks/cli/compare/databricks:6e21ced...andrefurlan-db:12ed10c)
to add `apps` support to the CLI and add the yaml file-support as an
override (the apps routes are already apart of the Go SDK and are
available for use in the CLI)

**NOTE: this feature is still private preview. CLI usage will be
internal only**

## Tests
<!-- How is this tested? -->
This commit is contained in:
Taiga Matsumoto 2023-10-27 11:57:26 -07:00 committed by GitHub
parent b91fab7d09
commit e408b701ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 100 additions and 0 deletions

View File

@ -0,0 +1,58 @@
package apps
import (
"fmt"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/flags"
"github.com/databricks/databricks-sdk-go/service/serving"
"github.com/spf13/cobra"
)
func createOverride(cmd *cobra.Command, deployReq *serving.DeployAppRequest) {
var manifestYaml flags.YamlFlag
var resourcesYaml flags.YamlFlag
createJson := cmd.Flag("json").Value.(*flags.JsonFlag)
// TODO: short flags
cmd.Flags().Var(&manifestYaml, "manifest", `either inline YAML string or @path/to/manifest.yaml`)
cmd.Flags().Var(&resourcesYaml, "resources", `either inline YAML string or @path/to/resources.yaml`)
cmd.Annotations = make(map[string]string)
cmd.PreRunE = root.MustWorkspaceClient
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
ctx := cmd.Context()
w := root.WorkspaceClient(ctx)
if cmd.Flags().Changed("json") {
err = createJson.Unmarshal(&deployReq)
if err != nil {
return err
}
} else if cmd.Flags().Changed("manifest") {
err = manifestYaml.Unmarshal(&deployReq.Manifest)
if err != nil {
return err
}
if cmd.Flags().Changed("resources") {
err = resourcesYaml.Unmarshal(&deployReq.Resources)
if err != nil {
return err
}
}
} else {
return fmt.Errorf("please provide command input in YAML format by specifying the --manifest flag or provide a json payload using the --json flag")
}
response, err := w.Apps.Create(ctx, *deployReq)
if err != nil {
return err
}
return cmdio.Render(ctx, response)
}
}
func init() {
createOverrides = append(createOverrides, createOverride)
}

42
libs/flags/yaml_flag.go Normal file
View File

@ -0,0 +1,42 @@
package flags
import (
"fmt"
"os"
"github.com/ghodss/yaml"
)
type YamlFlag struct {
raw []byte
}
func (y *YamlFlag) String() string {
return fmt.Sprintf("YAML (%d bytes)", len(y.raw))
}
// TODO: Command.MarkFlagFilename()
func (y *YamlFlag) Set(v string) error {
// Load request from file if it starts with '@' (like curl).
if v[0] != '@' {
y.raw = []byte(v)
return nil
}
buf, err := os.ReadFile(v[1:])
if err != nil {
return fmt.Errorf("read %s: %w", v, err)
}
y.raw = buf
return nil
}
func (y *YamlFlag) Unmarshal(v any) error {
if y.raw == nil {
return nil
}
return yaml.Unmarshal(y.raw, v)
}
func (y *YamlFlag) Type() string {
return "YAML"
}