diff --git a/encoder.go b/encoder.go index 86e20e6..c0ce638 100644 --- a/encoder.go +++ b/encoder.go @@ -14,33 +14,36 @@ import ( ) type GenericTemplateBasedEncoder struct { - templateDir string - service *descriptor.ServiceDescriptorProto - file *descriptor.FileDescriptorProto - debug bool + templateDir string + service *descriptor.ServiceDescriptorProto + file *descriptor.FileDescriptorProto + debug bool + destinationDir string } type Ast struct { - BuildDate time.Time `json:"build-date"` - BuildHostname string `json:"build-hostname"` - BuildUser string `json:"build-user"` - GoPWD string `json:"go-pwd,omitempty"` - PWD string `json:"pwd"` - Debug bool `json:"debug"` - File *descriptor.FileDescriptorProto `json:"file"` - RawFilename string `json:"raw-filename"` - Filename string `json:"filename"` - TemplateDir string `json:"template-dir"` - Service *descriptor.ServiceDescriptorProto `json:"service"` - Environment []string `json:"environment"` + BuildDate time.Time `json:"build-date"` + BuildHostname string `json:"build-hostname"` + BuildUser string `json:"build-user"` + GoPWD string `json:"go-pwd,omitempty"` + PWD string `json:"pwd"` + Debug bool `json:"debug"` + DestinationDir string `json:"destination-dir"` + File *descriptor.FileDescriptorProto `json:"file"` + RawFilename string `json:"raw-filename"` + Filename string `json:"filename"` + TemplateDir string `json:"template-dir"` + Service *descriptor.ServiceDescriptorProto `json:"service"` + Environment []string `json:"environment"` } -func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool) (e *GenericTemplateBasedEncoder) { +func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) { e = &GenericTemplateBasedEncoder{ - service: service, - file: file, - templateDir: templateDir, - debug: debug, + service: service, + file: file, + templateDir: templateDir, + debug: debug, + destinationDir: destinationDir, } if debug { @@ -88,17 +91,18 @@ func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, err } } ast := Ast{ - BuildDate: time.Now(), - BuildHostname: hostname, - BuildUser: os.Getenv("USER"), - PWD: pwd, - GoPWD: goPwd, - File: e.file, - TemplateDir: e.templateDir, - RawFilename: templateFilename, - Filename: "", - Environment: os.Environ(), - Service: e.service, + BuildDate: time.Now(), + BuildHostname: hostname, + BuildUser: os.Getenv("USER"), + PWD: pwd, + GoPWD: goPwd, + File: e.file, + TemplateDir: e.templateDir, + DestinationDir: e.destinationDir, + RawFilename: templateFilename, + Filename: "", + Environment: os.Environ(), + Service: e.service, } buffer := new(bytes.Buffer) tmpl, err := template.New("").Funcs(ProtoHelpersFuncMap).Parse(templateFilename) diff --git a/main.go b/main.go index 3907e1b..6a4b8e3 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ func main() { // Parse parameters templateDir := "./templates" + destinationDir := "." debug := false if parameter := g.Request.GetParameter(); parameter != "" { for _, param := range strings.Split(parameter, ",") { @@ -42,6 +43,9 @@ func main() { case "template_dir": templateDir = parts[1] break + case "destination_dir": + destinationDir = parts[1] + break case "debug": switch strings.ToLower(parts[1]) { case "true", "t": @@ -60,7 +64,7 @@ func main() { // Generate the encoders for _, file := range g.Request.GetProtoFile() { for _, service := range file.GetService() { - encoder := NewGenericTemplateBasedEncoder(templateDir, service, file, debug) + encoder := NewGenericTemplateBasedEncoder(templateDir, service, file, debug, destinationDir) g.Response.File = append(g.Response.File, encoder.Files()...) } }