Skip to content

Commit

Permalink
feat: Add 'get' command for retrieving and displaying applied resourc…
Browse files Browse the repository at this point in the history
…es (#46)
  • Loading branch information
siyul-park authored Dec 11, 2023
1 parent 06c45cd commit 8a069c9
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 14 deletions.
9 changes: 9 additions & 0 deletions cmd/printer/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ type TablePrinter struct {
formats []*jsonata.Expr
}

// SpecTableColumnDefinitions defines columns for displaying spec information.
var SpecTableColumnDefinitions = []TableColumnDefinition{
{Name: "id", Format: "$.id"},
{Name: "kind", Format: "$.kind"},
{Name: "namespace", Format: "$.namespace"},
{Name: "name", Format: "$.name"},
{Name: "links", Format: "$.links"},
}

// style is the default style configuration for the table.
var style = table.Style{
Name: "StyleDefault",
Expand Down
11 changes: 1 addition & 10 deletions cmd/uniflow/apply/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ type Config struct {
FS fs.FS
}

// SpecTableColumnDefinitions defines columns for displaying spec information.
var SpecTableColumnDefinitions = []printer.TableColumnDefinition{
{Name: "id", Format: "$.id"},
{Name: "kind", Format: "$.kind"},
{Name: "namespace", Format: "$.namespace"},
{Name: "name", Format: "$.name"},
{Name: "links", Format: "$.links"},
}

// NewCmd creates a new cobra.Command for the apply command.
func NewCmd(config Config) *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -150,7 +141,7 @@ func applySpecs(ctx context.Context, st *storage.Storage, specs []scheme.Spec) e
}

func printSpecTable(cmd *cobra.Command, specs []scheme.Spec) error {
tablePrinter, err := printer.NewTable(SpecTableColumnDefinitions)
tablePrinter, err := printer.NewTable(printer.SpecTableColumnDefinitions)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/uniflow/apply/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func TestExecute(t *testing.T) {

cmd := NewCmd(Config{
Scheme: s,
FS: fsys,
Database: db,
FS: fsys,
})
cmd.SetOut(output)
cmd.SetErr(output)
Expand All @@ -65,4 +65,6 @@ func TestExecute(t *testing.T) {
r, err := st.FindOne(context.Background(), storage.Where[string](scheme.KeyName).EQ(spec.GetName()))
assert.NoError(t, err)
assert.NotNil(t, r)

assert.Contains(t, output.String(), spec.Name)
}
11 changes: 8 additions & 3 deletions cmd/uniflow/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/fs"

"github.com/siyul-park/uniflow/cmd/uniflow/apply"
"github.com/siyul-park/uniflow/cmd/uniflow/get"
"github.com/siyul-park/uniflow/cmd/uniflow/start"
"github.com/siyul-park/uniflow/pkg/database"
"github.com/siyul-park/uniflow/pkg/hook"
Expand Down Expand Up @@ -32,15 +33,19 @@ func NewCmd(config Config) *cobra.Command {
Long: "Create your uniflow and integrate it anywhere!",
}

cmd.AddCommand(start.NewCmd(start.Config{
cmd.AddCommand(apply.NewCmd(apply.Config{
Scheme: sc,
Hook: hk,
Database: db,
FS: fsys,
}))
cmd.AddCommand(apply.NewCmd(apply.Config{
cmd.AddCommand(get.NewCmd(get.Config{
Scheme: sc,
Database: db,
}))
cmd.AddCommand(start.NewCmd(start.Config{
Scheme: sc,
Hook: hk,
Database: db,
FS: fsys,
}))

Expand Down
83 changes: 83 additions & 0 deletions cmd/uniflow/get/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package get

import (
"fmt"

"github.com/siyul-park/uniflow/cmd/flag"
"github.com/siyul-park/uniflow/cmd/printer"
"github.com/siyul-park/uniflow/pkg/database"
"github.com/siyul-park/uniflow/pkg/scheme"
"github.com/siyul-park/uniflow/pkg/storage"
"github.com/spf13/cobra"
)

// Config represents the configuration for the get command.
type Config struct {
Scheme *scheme.Scheme
Database database.Database
}

// NewCmd creates a new cobra.Command for the get command.
func NewCmd(config Config) *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: "Get and display applied resources",
RunE: runGetCommand(config),
}

cmd.PersistentFlags().StringP(FlagNamespace, flag.ToShorthand(FlagNamespace), "", "Set the resource's namespace. If not set, use all namespace")

return cmd
}

func runGetCommand(config Config) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()

ns, err := cmd.Flags().GetString(FlagNamespace)
if err != nil {
return err
}

st, err := storage.New(ctx, storage.Config{
Scheme: config.Scheme,
Database: config.Database,
})
if err != nil {
return err
}

filter := createNamespaceFilter(ns)
specs, err := st.FindMany(ctx, filter)
if err != nil {
return err
}

return printSpecTable(cmd, specs)
}
}

func createNamespaceFilter(ns string) *storage.Filter {
if ns == "" {
return nil
}
return storage.Where[string](scheme.KeyNamespace).EQ(ns)
}

func printSpecTable(cmd *cobra.Command, specs []scheme.Spec) error {
tablePrinter, err := printer.NewTable(printer.SpecTableColumnDefinitions)
if err != nil {
return err
}

table, err := tablePrinter.Print(specs)
if err != nil {
return err
}

if _, err := fmt.Fprint(cmd.OutOrStdout(), table); err != nil {
return err
}

return nil
}
55 changes: 55 additions & 0 deletions cmd/uniflow/get/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package get

import (
"bytes"
"context"
"testing"

"github.com/go-faker/faker/v4"
"github.com/siyul-park/uniflow/pkg/database/memdb"
"github.com/siyul-park/uniflow/pkg/node"
"github.com/siyul-park/uniflow/pkg/scheme"
"github.com/siyul-park/uniflow/pkg/storage"
"github.com/stretchr/testify/assert"
)

func TestExecute(t *testing.T) {
s := scheme.New()
db := memdb.New("")

st, _ := storage.New(context.Background(), storage.Config{
Scheme: s,
Database: db,
})

kind := faker.Word()

spec := &scheme.SpecMeta{
Kind: kind,
Namespace: scheme.DefaultNamespace,
Name: faker.Word(),
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
return node.NewOneToOneNode(nil), nil
})

s.AddKnownType(kind, &scheme.SpecMeta{})
s.AddCodec(kind, codec)

id, _ := st.InsertOne(context.Background(), spec)

output := new(bytes.Buffer)

cmd := NewCmd(Config{
Scheme: s,
Database: db,
})
cmd.SetOut(output)
cmd.SetErr(output)

err := cmd.Execute()
assert.NoError(t, err)

assert.Contains(t, output.String(), id.String())
}
5 changes: 5 additions & 0 deletions cmd/uniflow/get/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package get

const (
FlagNamespace = "namespace"
)

0 comments on commit 8a069c9

Please sign in to comment.