Add 'pwd' and 'go-pwd' variables

This commit is contained in:
Manfred Touron 2016-11-07 10:27:57 +01:00
parent 32072ab20b
commit 136f9b6fba
No known key found for this signature in database
GPG Key ID: 9CCF47DF1FD978A1
2 changed files with 16 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import (
"log"
"os"
"path/filepath"
"strings"
"text/template"
"time"
@ -25,6 +26,8 @@ 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"`
Filename string `json:"filename"`
@ -86,10 +89,20 @@ func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (str
}
hostname, _ := os.Hostname()
pwd, _ := os.Getwd()
goPwd := ""
if os.Getenv("GOPATH") != "" {
goPwd, _ = filepath.Rel(os.Getenv("GOPATH")+"/src", pwd)
if strings.Contains(goPwd, "../") {
goPwd = ""
}
}
ast := Ast{
BuildDate: time.Now(),
BuildHostname: hostname,
BuildUser: os.Getenv("USER"),
PWD: pwd,
GoPWD: goPwd,
File: e.file,
Filename: templateFilename,
Service: e.service,

View File

@ -1,7 +1,9 @@
{
"build-date": "2016-11-07T09:21:48.495773574+01:00",
"build-date": "2016-11-07T10:27:35.986181944+01:00",
"build-hostname": "moul-rose.local",
"build-user": "moul",
"go-pwd": "github.com/moul/protoc-gen-gotemplate/examples/dummy",
"pwd": "/Users/moul/go/src/github.com/moul/protoc-gen-gotemplate/examples/dummy",
"debug": false,
"file": {
"name": "dummy.proto",