From 52964c32aa4881f06327976d3c99018c7708f6e3 Mon Sep 17 00:00:00 2001 From: siyul-park Date: Tue, 21 Nov 2023 06:17:20 -0500 Subject: [PATCH] refactor: remove duplicated in cmd --- cmd/uniflow/apply/cmd.go | 37 ++++++------------------------------- cmd/uniflow/start/cmd.go | 37 ++++++------------------------------- 2 files changed, 12 insertions(+), 62 deletions(-) diff --git a/cmd/uniflow/apply/cmd.go b/cmd/uniflow/apply/cmd.go index c1b28e17..897bf090 100644 --- a/cmd/uniflow/apply/cmd.go +++ b/cmd/uniflow/apply/cmd.go @@ -1,7 +1,6 @@ package apply import ( - "io" "io/fs" "github.com/oklog/ulid/v2" @@ -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() != "" { diff --git a/cmd/uniflow/start/cmd.go b/cmd/uniflow/start/cmd.go index c3ba645d..25c5df3c 100644 --- a/cmd/uniflow/start/cmd.go +++ b/cmd/uniflow/start/cmd.go @@ -1,7 +1,6 @@ package start import ( - "io" "io/fs" "os" "os/signal" @@ -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 }