Skip to content

Commit

Permalink
Add ability to customize format options for command
Browse files Browse the repository at this point in the history
  • Loading branch information
fondoger committed Oct 6, 2024
1 parent f1a45d8 commit 5c995b4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions tools/goctl/api/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func init() {
dartCmdFlags.BoolVar(&dartgen.VarStringLegacy, "legacy")
dartCmdFlags.StringVar(&dartgen.VarStringHostname, "hostname")
dartCmdFlags.StringVar(&dartgen.VarStringScheme, "scheme")
dartCmdFlags.StringVar(&dartgen.VarStringFormatArgs, "format-args")

docCmdFlags.StringVar(&docgen.VarStringDir, "dir")
docCmdFlags.StringVar(&docgen.VarStringOutput, "o")
Expand Down
9 changes: 8 additions & 1 deletion tools/goctl/api/dartgen/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"fmt"
"os"
"os/exec"
"strings"
)

const dartExec = "dart"

func formatDir(dir string) error {
func formatDir(dir string, formatArgs string) error {
ok, err := dirctoryExists(dir)
if err != nil {
return err
Expand All @@ -25,6 +26,12 @@ func formatDir(dir string) error {
cmd.Env = os.Environ()
cmd.Stderr = os.Stderr

// Add additional format arguments to `dart format` command.
// Eg: `dart format --line-length=120`
if len(formatArgs) > 0 {
cmd.Args = append(cmd.Args, strings.Split(formatArgs, " ")...)
}

return cmd.Run()
}

Expand Down
8 changes: 7 additions & 1 deletion tools/goctl/api/dartgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var (
VarStringHostname string
// VarStringSchema defines the scheme.
VarStringScheme string
// VarFormatArgs defines the format arguments, eg: "--line-length=120"
VarStringFormatArgs string
)

// DartCommand create dart network request code
Expand All @@ -30,6 +32,7 @@ func DartCommand(_ *cobra.Command, _ []string) error {
isLegacy := VarStringLegacy
hostname := VarStringHostname
scheme := VarStringScheme
formatArgs := VarStringFormatArgs
if len(apiFile) == 0 {
return errors.New("missing -api")
}
Expand All @@ -44,6 +47,9 @@ func DartCommand(_ *cobra.Command, _ []string) error {
fmt.Println("you could use '-scheme' flag to specify your server scheme")
scheme = "http"
}
if len(formatArgs) == 0 {
fmt.Println(`you could use '-format-args "--line-length=120"' flag to specify the dart format arguments`)
}

api, err := parser.Parse(apiFile)
if err != nil {
Expand All @@ -62,7 +68,7 @@ func DartCommand(_ *cobra.Command, _ []string) error {
logx.Must(genData(dir+"data/", api, isLegacy))
logx.Must(genApi(dir+"api/", api, isLegacy))
logx.Must(genVars(dir+"vars/", isLegacy, scheme, hostname))
if err := formatDir(dir); err != nil {
if err := formatDir(dir, formatArgs); err != nil {
logx.Errorf("failed to format, %v", err)
}
return nil
Expand Down

0 comments on commit 5c995b4

Please sign in to comment.