Skip to content

Commit

Permalink
refactor: more readable cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Dec 1, 2023
1 parent 84f9e9d commit 8d17948
Show file tree
Hide file tree
Showing 15 changed files with 367 additions and 328 deletions.
2 changes: 2 additions & 0 deletions cmd/flag/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package flag

import "github.com/iancoleman/strcase"

// ToKey converts a flag string to snake case.
func ToKey(flag string) string {
return strcase.ToSnake(flag)
}

// ToShorthand returns the first character of the input string.
func ToShorthand(flag string) string {
if flag == "" {
return ""
Expand Down
98 changes: 50 additions & 48 deletions cmd/printer/table.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,59 @@
package printer

import (
"errors"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/siyul-park/uniflow/pkg/primitive"
"github.com/xiatechs/jsonata-go"
)

type (
TableColumnDefinition struct {
Name string
Format string
}
// TableColumnDefinition represents the definition of a table column.
type TableColumnDefinition struct {
Name string // Name is the name of the column.
Format string // Format is the JSONata expression for formatting the column.
}

TablePrinter struct {
names []string
formats []*jsonata.Expr
}
)
// TablePrinter is responsible for printing tabular data based on the provided columns.
type TablePrinter struct {
names []string
formats []*jsonata.Expr
}

var (
style = table.Style{
Name: "StyleDefault",
Box: table.BoxStyle{
BottomLeft: "",
BottomRight: "",
BottomSeparator: "",
EmptySeparator: text.RepeatAndTrim(" ", text.RuneWidthWithoutEscSequences(" ")),
Left: "",
LeftSeparator: "",
MiddleHorizontal: "",
MiddleSeparator: "",
MiddleVertical: "",
PaddingLeft: " ",
PaddingRight: " ",
PageSeparator: "\n",
Right: "",
RightSeparator: "",
TopLeft: "",
TopRight: "",
TopSeparator: "",
UnfinishedRow: " ~",
},
Color: table.ColorOptionsDefault,
Format: table.FormatOptionsDefault,
HTML: table.DefaultHTMLOptions,
Options: table.Options{
DrawBorder: false,
SeparateColumns: true,
SeparateFooter: false,
SeparateHeader: false,
SeparateRows: false,
},
Title: table.TitleOptionsDefault,
}
)
// style is the default style configuration for the table.
var style = table.Style{
Name: "StyleDefault",
Box: table.BoxStyle{
BottomLeft: "",
BottomRight: "",
BottomSeparator: "",
EmptySeparator: text.RepeatAndTrim(" ", text.RuneWidthWithoutEscSequences(" ")),
Left: "",
LeftSeparator: "",
MiddleHorizontal: "",
MiddleSeparator: "",
MiddleVertical: "",
PaddingLeft: " ",
PaddingRight: " ",
PageSeparator: "\n",
Right: "",
RightSeparator: "",
TopLeft: "",
TopRight: "",
TopSeparator: "",
UnfinishedRow: " ~",
},
Color: table.ColorOptionsDefault,
Format: table.FormatOptionsDefault,
HTML: table.DefaultHTMLOptions,
Options: table.Options{
DoNotColorBordersAndSeparators: true,
},
Title: table.TitleOptionsDefault,
}

// NewTable creates a new TablePrinter based on the provided column definitions.
func NewTable(columns []TableColumnDefinition) (*TablePrinter, error) {
names := make([]string, len(columns))
formats := make([]*jsonata.Expr, len(columns))
Expand All @@ -77,17 +75,21 @@ func NewTable(columns []TableColumnDefinition) (*TablePrinter, error) {
}, nil
}

// Print formats and prints the provided data as a table.
func (p *TablePrinter) Print(data any) (string, error) {
value, err := primitive.MarshalText(data)
if err != nil {
return "", err
}

var elements []any
if v, ok := value.(*primitive.Slice); ok {
switch v := value.(type) {
case *primitive.Slice:
elements = v.Slice()
} else if v, ok := value.(*primitive.Map); ok {
case *primitive.Map:
elements = append(elements, v.Interface())
default:
return "", errors.New("unsupported data type")
}

header := make(table.Row, len(p.names))
Expand Down
22 changes: 14 additions & 8 deletions cmd/resource/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,49 @@ import (
"github.com/siyul-park/uniflow/pkg/scheme"
)

type (
Builder struct {
scheme *scheme.Scheme
namespace string
fsys fs.FS
filename string
}
)
// Builder is responsible for building scheme.Spec instances from raw data.
type Builder struct {
scheme *scheme.Scheme
namespace string
fsys fs.FS
filename string
}

// NewBuilder creates a new Builder instance.
func NewBuilder() *Builder {
return &Builder{}
}

// Scheme sets the scheme for the Builder.
func (b *Builder) Scheme(scheme *scheme.Scheme) *Builder {
b.scheme = scheme
return b
}

// Namespace sets the namespace for the Builder.
func (b *Builder) Namespace(namespace string) *Builder {
b.namespace = namespace
return b
}

// FS sets the file system for the Builder.
func (b *Builder) FS(fsys fs.FS) *Builder {
b.fsys = fsys
return b
}

// Filename sets the filename for the Builder.
func (b *Builder) Filename(filename string) *Builder {
b.filename = filename
return b
}

// Build builds scheme.Spec instances based on the configured parameters.
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
Expand Down
22 changes: 12 additions & 10 deletions cmd/resource/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import (
"github.com/siyul-park/uniflow/pkg/scheme"
)

type (
SpecCodecOptions struct {
Scheme *scheme.Scheme
Namespace string
}
// SpecCodecOptions holds options for creating a SpecCodec.
type SpecCodecOptions struct {
Scheme *scheme.Scheme
Namespace string
}

SpecCodec struct {
scheme *scheme.Scheme
namespace string
}
)
// SpecCodec is responsible for decoding raw data into scheme.Spec instances.
type SpecCodec struct {
scheme *scheme.Scheme
namespace string
}

// NewSpecCodec creates a new SpecCodec instance with the provided options.
func NewSpecCodec(opts ...SpecCodecOptions) *SpecCodec {
var scheme *scheme.Scheme
var namespace string
Expand All @@ -38,6 +39,7 @@ func NewSpecCodec(opts ...SpecCodecOptions) *SpecCodec {
}
}

// Decode decodes raw data into a scheme.Spec instance.
func (c *SpecCodec) Decode(data any) (scheme.Spec, error) {
doc, err := primitive.MarshalBinary(data)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/resource/yamljson.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"gopkg.in/yaml.v3"
)

// UnmarshalYAMLOrJSON unmarshals data based on its content type, supporting both JSON and YAML formats.
func UnmarshalYAMLOrJSON(data []byte, v any) error {
if http.DetectContentType(data) == "application/json" {
return json.Unmarshal(data, v)
Expand Down
Loading

0 comments on commit 8d17948

Please sign in to comment.