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

Add ability to specify output filename #32

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
17 changes: 14 additions & 3 deletions dataloaden.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/vektah/dataloaden/pkg/generator"
)

func main() {
if len(os.Args) != 4 {
output := flag.String("output", "", "output filename")

flag.Parse()

args := flag.Args()
if len(args) != 3 {
fmt.Println("usage: name keyType valueType")
fmt.Println(" example:")
fmt.Println(" dataloaden 'UserLoader int []*github.com/my/package.User'")
Expand All @@ -20,8 +26,13 @@ func main() {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(2)
}

if err := generator.Generate(os.Args[1], os.Args[2], os.Args[3], wd); err != nil {
if err := generator.Generate(&generator.GenerateInput{
Name: args[0],
KeyType: args[1],
ValueType: args[2],
WorkingDir: wd,
Output: *output,
}); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(2)
}
Expand Down
224 changes: 224 additions & 0 deletions example/filename/myloader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions example/filename/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package filename

//go:generate go run github.com/vektah/dataloaden -output=myloader.go UserLoader string *github.com/vektah/dataloaden/example.User
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module github.com/vektah/dataloaden

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365
github.com/pkg/errors v0.8.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365 h1:ECW73yc9MY7935nNYXUkK7Dz17YuSUI9yqRqYS8aBww=
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
20 changes: 16 additions & 4 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"unicode"

"github.com/iancoleman/strcase"
"github.com/pkg/errors"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/imports"
Expand Down Expand Up @@ -78,15 +79,26 @@ func parseType(str string) (*goType, error) {
return t, nil
}

func Generate(name string, keyType string, valueType string, wd string) error {
data, err := getData(name, keyType, valueType, wd)
type GenerateInput struct {
Name string
KeyType string
ValueType string
WorkingDir string
Output string
}

func Generate(input *GenerateInput) error {
data, err := getData(input.Name, input.KeyType, input.ValueType, input.WorkingDir)
if err != nil {
return err
}

filename := strings.ToLower(data.Name) + "_gen.go"
filename := input.Output
if filename == "" {
filename = strcase.ToSnake(data.Name) + "_gen.go"
}

if err := writeTemplate(filepath.Join(wd, filename), data); err != nil {
if err := writeTemplate(filepath.Join(input.WorkingDir, filename), data); err != nil {
return err
}

Expand Down