Skip to content

Commit

Permalink
(choria-io#1665) add transforms to kv and some bug fixes
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar committed May 25, 2022
1 parent 656a1a4 commit d6b9b75
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
7 changes: 6 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ func runBuilder() {

err := builder.RunStandardCLI(ctx, filepath.Base(os.Args[0]), false, nil, builderOptions()...)
if err != nil {
panic(fmt.Sprintf("app builder setup failed: %v", err))
if strings.Contains(err.Error(), "must select a") {
fmt.Println(err.Error())
} else {
fmt.Printf("app builder setup failed: %v\n", err)
}
os.Exit(1)
}

os.Exit(0)
Expand Down
13 changes: 12 additions & 1 deletion cmd/tool_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
package cmd

import (
"github.com/choria-io/appbuilder/commands/exec"
"github.com/choria-io/appbuilder/commands/parent"
"github.com/choria-io/go-choria/providers/appbuilder/discover"
"github.com/choria-io/go-choria/providers/appbuilder/kv"
"github.com/choria-io/go-choria/providers/appbuilder/rpc"
"sync"

"github.com/choria-io/appbuilder/builder"
Expand All @@ -15,6 +20,12 @@ type tBuilderCommand struct {
}

func (c *tBuilderCommand) Setup() error {
kv.MustRegister()
rpc.MustRegister()
discover.MustRegister()
parent.MustRegister()
exec.MustRegister()

if tool, ok := cmdWithFullCommand("tool"); ok {
c.cmd = tool.Cmd().Command("builder", "Application Builder tools")
bldr, err := builder.New(ctx, "builder", builderOptions()...)
Expand All @@ -32,7 +43,7 @@ func (c *tBuilderCommand) Setup() error {
}

func (c *tBuilderCommand) Configure() error {
return nil
return commonConfigure()
}

func (c *tBuilderCommand) Run(wg *sync.WaitGroup) error {
Expand Down
67 changes: 58 additions & 9 deletions providers/appbuilder/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ package kv
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/choria-io/appbuilder/builder"
Expand All @@ -21,11 +24,12 @@ import (
)

type Command struct {
Action string `json:"action"`
Bucket string `json:"bucket"`
Key string `json:"key"`
Value string `json:"value"`
RenderJSON bool `json:"json"`
Action string `json:"action"`
Bucket string `json:"bucket"`
Key string `json:"key"`
Value string `json:"value"`
RenderJSON bool `json:"json"`
Transform *builder.GenericTransform `json:"transform"`

builder.GenericCommand
builder.GenericSubCommands
Expand Down Expand Up @@ -65,8 +69,50 @@ func MustRegister() {
builder.MustRegisterCommand("kv", NewKVCommand)
}

func (r *KV) Validate(log builder.Logger) error { return nil }
func (r *KV) String() string { return fmt.Sprintf("%s (kv)", r.def.Name) }
func (r *KV) Validate(log builder.Logger) error {
if r.def.Type != "kv" {
return fmt.Errorf("not a kv command")
}

var errs []string

err := r.def.GenericCommand.Validate(log)
if err != nil {
errs = append(errs, err.Error())
}

if r.def.Transform != nil {
err := r.def.Transform.Validate()
if err != nil {
errs = append(errs, err.Error())
}
}

if r.def.Bucket == "" {
errs = append(errs, "bucket is required")
}

if r.def.Key == "" {
errs = append(errs, "key is required")
}

act := r.def.Action
if act == "put" && r.def.Value == "" {
errs = append(errs, "value is required for put operations")
}

if !(act == "put" || act == "get" || act == "history" || act == "del") {
errs = append(errs, "invalid action %q", act)
}

if len(errs) > 0 {
return errors.New(strings.Join(errs, ", "))
}

return nil
}

func (r *KV) String() string { return fmt.Sprintf("%s (kv)", r.def.Name) }

func (r *KV) SubCommands() []json.RawMessage {
return r.def.Commands
Expand All @@ -88,14 +134,17 @@ func (r *KV) getAction(kv nats.KeyValue) error {
return err
}

if r.def.RenderJSON {
switch {
case r.def.Transform != nil:
r.def.Transform.FTransformJSON(r.ctx, os.Stdout, entry.Value())
case r.def.RenderJSON:
ej, err := json.MarshalIndent(r.entryMap(entry), "", " ")
if err != nil {
return err
}

fmt.Println(string(ej))
} else {
default:
fmt.Println(string(entry.Value()))
}

Expand Down

0 comments on commit d6b9b75

Please sign in to comment.