From 136f9b6fbaaf4e767a2cdacaba801806e9a7277b Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Mon, 7 Nov 2016 10:27:57 +0100 Subject: [PATCH] Add 'pwd' and 'go-pwd' variables --- encoder.go | 13 +++++++++++++ examples/dummy/output/export.json | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/encoder.go b/encoder.go index 7fc6e83..5edf0a0 100644 --- a/encoder.go +++ b/encoder.go @@ -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, diff --git a/examples/dummy/output/export.json b/examples/dummy/output/export.json index dd87bd0..bd2b309 100644 --- a/examples/dummy/output/export.json +++ b/examples/dummy/output/export.json @@ -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",