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

allow printing to stdout if no output file is specified #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go:
- "1.13"
- "1.14"
- "1.15"
- "1.16"

cache:
directories:
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ go get github.com/twitchtv/circuitgen
# Usage

```bash
circuitgen --pkg <package path> --name <type name> --out <output path> [--alias <alias>] [--circuit-major-version <circuit major version>]
circuitgen --pkg <package path> --name <type name> [--out <output path>] [--alias <alias>] [--circuit-major-version <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.
Expand Down
11 changes: 7 additions & 4 deletions circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
}

Expand Down Expand Up @@ -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 {
Expand Down