Skip to content

Commit

Permalink
refactor(cmd): remove duplicated code for making spec (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park authored Nov 24, 2023
1 parent 538e05b commit 4b66c51
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 64 deletions.
83 changes: 83 additions & 0 deletions cmd/resource/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package resource

import (
"io"
"io/fs"

"github.com/siyul-park/uniflow/pkg/scheme"
)

type (
Builder struct {
scheme *scheme.Scheme
namespace string
fsys fs.FS
filename string
}
)

func NewBuilder() *Builder {
return &Builder{}
}

func (b *Builder) Scheme(scheme *scheme.Scheme) *Builder {
b.scheme = scheme
return b
}

func (b *Builder) Namespace(namespace string) *Builder {
b.namespace = namespace
return b
}

func (b *Builder) FS(fsys fs.FS) *Builder {
b.fsys = fsys
return b
}

func (b *Builder) Filename(filename string) *Builder {
b.filename = filename
return b
}

func (b *Builder) Build() ([]scheme.Spec, error) {
if b.fsys == nil || b.filename == "" {
return nil, nil
}
file, err := b.fsys.Open(b.filename)
if err != nil {
return nil, err
}
defer file.Close()

data, err := io.ReadAll(file)
if err != nil {
return nil, err
}

var raws []map[string]any
if err := UnmarshalYAMLOrJSON(data, &raws); err != nil {
var e map[string]any
if err := UnmarshalYAMLOrJSON(data, &e); err != nil {
return nil, err
} else {
raws = []map[string]any{e}
}
}

codec := NewSpecCodec(SpecCodecOptions{
Scheme: b.scheme,
Namespace: b.namespace,
})

var specs []scheme.Spec
for _, raw := range raws {
if spec, err := codec.Decode(raw); err != nil {
return nil, err
} else {
specs = append(specs, spec)
}
}

return specs, nil
}
50 changes: 50 additions & 0 deletions cmd/resource/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package resource

import (
"encoding/json"
"testing"
"testing/fstest"

"github.com/go-faker/faker/v4"
"github.com/oklog/ulid/v2"
"github.com/siyul-park/uniflow/pkg/node"
"github.com/siyul-park/uniflow/pkg/scheme"
"github.com/stretchr/testify/assert"
)

func TestBuilder_Build(t *testing.T) {
s := scheme.New()
fsys := make(fstest.MapFS)

filename := "spec.json"
kind := faker.Word()

spec := &scheme.SpecMeta{
ID: ulid.Make(),
Kind: kind,
Namespace: scheme.NamespaceDefault,
}

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

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

data, _ := json.Marshal(spec)

fsys[filename] = &fstest.MapFile{
Data: data,
}

builder := NewBuilder().
Scheme(s).
Namespace(scheme.NamespaceDefault).
FS(fsys).
Filename(filename)

specs, err := builder.Build()
assert.NoError(t, err)
assert.Len(t, specs, 1)
}
37 changes: 6 additions & 31 deletions cmd/uniflow/apply/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package apply

import (
"io"
"io/fs"

"github.com/oklog/ulid/v2"
Expand Down Expand Up @@ -50,41 +49,17 @@ func NewCmd(config Config) *cobra.Command {
return err
}

file, err := fsys.Open(fl)
if err != nil {
return err
}
defer func() { _ = file.Close() }()
b := resource.NewBuilder().
Scheme(sc).
Namespace(ns).
FS(fsys).
Filename(fl)

data, err := io.ReadAll(file)
specs, err := b.Build()
if err != nil {
return err
}

var raws []map[string]any
if err := resource.UnmarshalYAMLOrJSON(data, &raws); err != nil {
var e map[string]any
if err := resource.UnmarshalYAMLOrJSON(data, &e); err != nil {
return err
} else {
raws = []map[string]any{e}
}
}

codec := resource.NewSpecCodec(resource.SpecCodecOptions{
Scheme: sc,
Namespace: ns,
})

var specs []scheme.Spec
for _, raw := range raws {
if spec, err := codec.Decode(raw); err != nil {
return err
} else {
specs = append(specs, spec)
}
}

for _, spec := range specs {
if spec.GetID() == (ulid.ULID{}) {
if spec.GetName() != "" {
Expand Down
37 changes: 6 additions & 31 deletions cmd/uniflow/start/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package start

import (
"io"
"io/fs"
"os"
"os/signal"
Expand Down Expand Up @@ -67,41 +66,17 @@ func NewCmd(config Config) *cobra.Command {
}); err != nil {
return err
} else if len(specs) == 0 {
file, err := fsys.Open(boot)
if err != nil {
return err
}
defer func() { _ = file.Close() }()
b := resource.NewBuilder().
Scheme(sc).
Namespace(ns).
FS(fsys).
Filename(boot)

data, err := io.ReadAll(file)
specs, err := b.Build()
if err != nil {
return err
}

var raws []map[string]any
if err := resource.UnmarshalYAMLOrJSON(data, &raws); err != nil {
var e map[string]any
if err := resource.UnmarshalYAMLOrJSON(data, &e); err != nil {
return err
} else {
raws = []map[string]any{e}
}
}

codec := resource.NewSpecCodec(resource.SpecCodecOptions{
Scheme: sc,
Namespace: ns,
})

var specs []scheme.Spec
for _, raw := range raws {
if spec, err := codec.Decode(raw); err != nil {
return err
} else {
specs = append(specs, spec)
}
}

if _, err := st.InsertMany(ctx, specs); err != nil {
return err
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/scheme/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

type (

// Unstructured is an Spec that is not marshaled for structuring.
Unstructured struct {
doc *primitive.Map
Expand Down Expand Up @@ -73,7 +72,6 @@ func (u *Unstructured) GetNamespace() string {
var val string
_ = u.Get(KeyNamespace, &val)
return val

}

// SetNamespace sets the Namespace of the Unstructured.
Expand Down

0 comments on commit 4b66c51

Please sign in to comment.