Skip to content

Commit

Permalink
refactor: remove duplicated in cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 21, 2023
1 parent 4a244c4 commit 52964c3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 62 deletions.
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 util.IsZero(spec.GetID()) {
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

0 comments on commit 52964c3

Please sign in to comment.