diff --git a/.travis.yml b/.travis.yml index 534c339..eee3849 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ go: - "1.13" - "1.14" - "1.15" + - "1.16" cache: directories: diff --git a/README.md b/README.md index 31aa62e..f79f5b7 100644 --- a/README.md +++ b/README.md @@ -35,9 +35,11 @@ go get github.com/twitchtv/circuitgen # Usage ```bash -circuitgen --pkg --name --out [--alias ] [--circuit-major-version ] +circuitgen --pkg --name [--out ] [--alias ] [--circuit-major-version ] ``` +If no `out` flag is included, the generated code is printed to standard output. + Add `./vendor/` to package path if the dependency is vendored; when using Go modules this is unnecessary. Set the `circuit-major-version` flag if using Go modules and major version 3 or later. This makes the wrappers import the same version as the rest of your code. diff --git a/circuit.go b/circuit.go index fccc7d4..fcb855b 100644 --- a/circuit.go +++ b/circuit.go @@ -223,9 +223,7 @@ func (c *circuitCmd) Cobra() *cobra.Command { pf.StringVar(&c.name, "name", "", "(Required) The name of the type (interface or struct) in the package path") markFlagRequired(pf, "name") - pf.StringVar(&c.out, "out", "", "(Required) The output path. A default filename is given if the path looks like a directory. The path is lazily created (equivalent to mkdir -p)") - markFlagRequired(pf, "out") - + pf.StringVar(&c.out, "out", "", "(Optional) The output path. A default filename is given if the path looks like a directory. The path is lazily created (equivalent to mkdir -p). If not set, prints to standard output.") pf.StringVar(&c.alias, "alias", "", "(Optional) The name used for the generated wrapper in the struct, constructor, and default circuit prefix. Defaults to name") pf.BoolVar(&c.debug, "debug", false, "Enable debug logging mode") pf.BoolVar(&c.goimports, "goimports", true, "Enable goimports formatting. If false, uses gofmt") @@ -247,7 +245,7 @@ func (c *circuitCmd) Execute() error { c.alias = c.name } - if !strings.HasSuffix(c.out, ".go") { + if c.out != "" && !strings.HasSuffix(c.out, ".go") { c.out = filepath.Join(c.out, strings.ToLower(c.alias)+".gen.go") } @@ -341,7 +339,12 @@ func (c *circuitCmd) log(msg string, args ...interface{}) { } // Writes the src to the path. The directory is lazily created for the path (equivalent to `mkdir -p`) +// If path is empty, writes to stdout. func writeFile(path string, src []byte) error { + if path == "" { + _, err := os.Stdout.Write(src) + return err + } dir := filepath.Dir(path) err := os.MkdirAll(dir, 0750) if err != nil {