-
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 'get' command for retrieving and displaying applied resourc…
…es (#46)
- Loading branch information
1 parent
06c45cd
commit 8a069c9
Showing
7 changed files
with
164 additions
and
14 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
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
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,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 | ||
} |
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,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()) | ||
} |
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,5 @@ | ||
package get | ||
|
||
const ( | ||
FlagNamespace = "namespace" | ||
) |