Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support Input source yaml #1

Merged
merged 7 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/homebrew.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Bump Homebrew formula

on:
push:
tags:
- '*'

jobs:
homebrew:
runs-on: ubuntu-latest

steps:
- name: Update Homebrew formula
uses: dawidd6/action-homebrew-bump-formula@v3
with:
# GitHub token, required, not the default one
token: ${{secrets.TOKEN}}
# Optional, defaults to homebrew/core
tap: tenfyzhong/tap
# Formula name, required
formula: st2
# Optional, will be determined automatically
tag: ${{github.ref}}
# Optional, will be determined automatically
revision: ${{github.sha}}
# Optional, if don't want to check for already open PRs
force: false # true
2 changes: 1 addition & 1 deletion cmd/st2/completions/st2.fish
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ complete st2 -f
complete st2 -r -f -s r -l root -d 'The root struct name (default: Root)'
complete st2 -r -F -s i -l input -d 'Input file, if not set, it will read from stdio'
complete st2 -l rc -d 'Read input from clipboard'
complete st2 -r -f -s s -l src -a "json proto thrift go csv" -d 'The source data type, it will use the suffix of the input file if not set'
complete st2 -r -f -s s -l src -a "json yaml proto thrift go csv" -d 'The source data type, it will use the suffix of the input file if not set'
complete st2 -r -f -s d -l dst -a "go proto thrift" -d 'The destination data type, it will use the suffix of the output file if not set'
complete st2 -r -F -s o -l output -d 'Output file, if not set, it will write to stdout'
complete st2 -l wc -d 'Write output to clipboard'
106 changes: 42 additions & 64 deletions cmd/st2/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
"fmt"
"io"
Expand All @@ -26,12 +27,12 @@ const (
categoryOutput = "output"
)

func getReader(ctx *cli.Context) (io.ReadCloser, error) {
if ctx.Bool(flagReadClipboard) {
func getReader(cmd *cli.Command) (io.ReadCloser, error) {
if cmd.Bool(flagReadClipboard) {
return NewClipboardReadCloser(), nil
}

readfile := ctx.String(flagInput)
readfile := cmd.String(flagInput)
if readfile == "" {
return os.Stdin, nil
}
Expand All @@ -42,12 +43,12 @@ func getReader(ctx *cli.Context) (io.ReadCloser, error) {
return file, nil
}

func getWriter(ctx *cli.Context) (io.WriteCloser, error) {
if ctx.Bool(flagWriteClipboard) {
func getWriter(cmd *cli.Command) (io.WriteCloser, error) {
if cmd.Bool(flagWriteClipboard) {
return NewClipboardWriteCloser(), nil
}

writefile := ctx.String(flagOutput)
writefile := cmd.String(flagOutput)
if writefile == "" {
return os.Stdout, nil
}
Expand All @@ -58,12 +59,12 @@ func getWriter(ctx *cli.Context) (io.WriteCloser, error) {
return file, nil
}

func action(ctx *cli.Context) error {
src := getSrc(ctx)
func action(ctx context.Context, cmd *cli.Command) error {
src := getSrc(cmd)
if src == "" {
return fmt.Errorf("flag: %s is required\n\n", flagSrc)
}
dst := getDst(ctx)
dst := getDst(cmd)
if dst == "" {
return fmt.Errorf("flag: %s is required\n\n", flagDst)
}
Expand All @@ -75,16 +76,16 @@ func action(ctx *cli.Context) error {
st2Ctx := st2.Context{
Src: src,
Dst: dst,
Root: ctx.String(flagRoot),
Root: cmd.String(flagRoot),
}

reader, err := getReader(ctx)
reader, err := getReader(cmd)
if err != nil {
return err
}
defer reader.Close()

writer, err := getWriter(ctx)
writer, err := getWriter(cmd)
if err != nil {
return err
}
Expand All @@ -111,33 +112,10 @@ func (f FlagList) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}

// func bashComplete(ctx *cli.Context) {
// flags := FlagList{}
// for _, flag := range ctx.App.Flags {
// for _, name := range flag.Names() {
// flags = append(flags, name)
// }
// }
// sort.Sort(flags)
// for _, flag := range flags {
// if len(flag) == 1 {
// fmt.Printf("-%s\n", flag)
// } else {
// fmt.Printf("--%s\n", flag)
// }
// }

// fmt.Printf("%s\n", jsonType)
// fmt.Printf("%s\n", goType)
// fmt.Printf("%s\n", protoType)
// fmt.Printf("%s\n", thriftType)
// }

func main() {
app := &cli.App{
cmd := &cli.Command{
Name: "st2",
HelpName: "",
Usage: "convert between json, protobuf, thrift, go struct",
Usage: "convert between json, yaml, protobuf, thrift, go struct",
UsageText: "",
ArgsUsage: "",
Version: config.Version,
Expand All @@ -158,18 +136,20 @@ func main() {
Usage: fmt.Sprintf("The destination data `type`, it will use the suffix of the output file if not set, available value: `%s`", arrayString(st2.DestinationLangs)),
},
&cli.StringFlag{
Name: flagInput,
Aliases: []string{"i"},
Category: categoryInput,
Required: false,
Usage: "Input `file`, if not set, it will read from stdio",
Name: flagInput,
Aliases: []string{"i"},
Category: categoryInput,
Required: false,
TakesFile: true,
Usage: "Input `file`, if not set, it will read from stdio",
},
&cli.StringFlag{
Name: flagOutput,
Aliases: []string{"o"},
Category: categoryOutput,
Required: false,
Usage: "Output `file`, if not set, it will write to stdout",
Name: flagOutput,
Aliases: []string{"o"},
Category: categoryOutput,
Required: false,
TakesFile: true,
Usage: "Output `file`, if not set, it will write to stdout",
},
&cli.StringFlag{
Name: flagRoot,
Expand All @@ -189,20 +169,18 @@ func main() {
Usage: "Write output to clipboard",
},
},
EnableBashCompletion: true,
HideHelp: false,
HideHelpCommand: true,
HideVersion: false,
EnableShellCompletion: true,
ShellCompletionCommandName: "st2",
HideHelp: false,
HideHelpCommand: true,
HideVersion: false,
// BashComplete: bashComplete,
Action: action,
Authors: []*cli.Author{
{
Name: "tenfyzhong",
Email: "[email protected]",
},
Authors: []any{
"tenfyzhong",
},
Copyright: "Copyright (c) 2022 tenfy",
ExitErrHandler: func(ctx *cli.Context, err error) {
ExitErrHandler: func(ctx context.Context, cmd *cli.Command, err error) {
if err != nil {
cli.ErrWriter.Write([]byte(strings.TrimSpace(err.Error())))
os.Exit(-1)
Expand All @@ -212,7 +190,7 @@ func main() {
Suggest: true,
}

app.Run(os.Args)
cmd.Run(context.Background(), os.Args)
}

func srcTypeFromName(name string) string {
Expand All @@ -233,20 +211,20 @@ func dstTypeFromName(name string) string {
return ""
}

func getSrc(ctx *cli.Context) string {
src := ctx.String(flagSrc)
func getSrc(cmd *cli.Command) string {
src := cmd.String(flagSrc)
if src != "" {
return src
}
return srcTypeFromName(ctx.String(flagInput))
return srcTypeFromName(cmd.String(flagInput))
}

func getDst(ctx *cli.Context) string {
dst := ctx.String(flagDst)
func getDst(cmd *cli.Command) string {
dst := cmd.String(flagDst)
if dst != "" {
return dst
}
return dstTypeFromName(ctx.String(flagOutput))
return dstTypeFromName(cmd.String(flagOutput))
}

func arrayString(arr []string) string {
Expand Down
19 changes: 19 additions & 0 deletions cmd/st2/testdata/a.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- a:
b: 1
c: hello
b:
b: 2
c: world
c: ["123"]
d:
- b: 3
c: 4
e:
aa: true
bb: false
f:
a:
hello: true
gg:
- null
h: null
3 changes: 2 additions & 1 deletion const.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
LangProto = "proto"
LangThrift = "thrift"
LangCsv = "csv"
LangYaml = "yaml"

RootDefault = "Root"
)
Expand Down Expand Up @@ -55,7 +56,7 @@ const (
)

var (
SourceLangs = []string{LangJson, LangProto, LangThrift, LangGo, LangCsv}
SourceLangs = []string{LangJson, LangYaml, LangProto, LangThrift, LangGo, LangCsv}
DestinationLangs = []string{LangGo, LangProto, LangThrift}
LangTmplMap = map[string]string{
LangGo: tmpl.Go,
Expand Down
3 changes: 3 additions & 0 deletions factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ func CreateParser(ctx Context) Parse {
return NewGoParser(ctx)
case LangJson:
return NewJsonParser(ctx)
case LangYaml:
return NewYamlParser(ctx)
case LangProto:
return NewProtoParser(ctx)
case LangThrift:
Expand All @@ -25,6 +27,7 @@ func CreateTmpl(ctx Context) string {
case LangGo:
return tmpl.Go
case LangJson:
case LangYaml:
case LangProto:
return tmpl.Proto
case LangThrift:
Expand Down
19 changes: 11 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@ module github.com/tenfyzhong/st2
go 1.18

require (
github.com/cloudwego/thriftgo v0.2.4 // indirect
github.com/cloudwego/thriftgo v0.2.4
github.com/iancoleman/strcase v0.2.0
github.com/json-iterator/go v1.1.12
github.com/stretchr/testify v1.8.4
github.com/yoheimuta/go-protoparser/v4 v4.7.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gobeam/stringy v0.0.5 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/samuel/go-thrift v0.0.0-20210915161234-7b67f98e972f // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/urfave/cli/v2 v2.23.7 // indirect
github.com/urfave/cli/v3 v3.0.0-alpha // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/yoheimuta/go-protoparser/v4 v4.7.0 // indirect
github.com/urfave/cli/v3 v3.0.0-alpha8 // indirect
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e // indirect
golang.design/x/clipboard v0.6.3 // indirect
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect
golang.org/x/mobile v0.0.0-20210716004757-34ab1303b554 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/urfave/cli/v2 v2.23.7 h1:YHDQ46s3VghFHFf1DdF+Sh7H4RqhcM+t0TmZRJx4oJY=
github.com/urfave/cli/v2 v2.23.7/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/urfave/cli/v3 v3.0.0-alpha h1:Cbc2CVsHVveE6SvoyOetqQKYNhxKsgp3bTlqH1nyi1Q=
github.com/urfave/cli/v3 v3.0.0-alpha/go.mod h1:o9y/j7PxPajDAEl+kKAdwePXiN/ZA5IDRjCCa8/Wu6s=
github.com/urfave/cli/v3 v3.0.0-alpha8 h1:H+qxFPoCkGzdF8KUMs2fEOZl5io/1QySgUiGfar8occ=
github.com/urfave/cli/v3 v3.0.0-alpha8/go.mod h1:0kK/RUFHyh+yIKSfWxwheGndfnrvYSmYFVeKCh03ZUc=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e h1:+SOyEddqYF09QP7vr7CgJ1eti3pY9Fn3LHO1M1r/0sI=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yoheimuta/go-protoparser/v4 v4.7.0 h1:80LGfVM25sCoNDD08hv9O0ShQMjoTrIE76j5ON+gq3U=
github.com/yoheimuta/go-protoparser/v4 v4.7.0/go.mod h1:AHNNnSWnb0UoL4QgHPiOAg2BniQceFscPI5X/BZNHl8=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
Expand Down
Loading
Loading