Add 'pwd' and 'go-pwd' variables
This commit is contained in:
		
							
								
								
									
										13
									
								
								encoder.go
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								encoder.go
									
									
									
									
									
								
							| @@ -5,6 +5,7 @@ import ( | |||||||
| 	"log" | 	"log" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
|  | 	"strings" | ||||||
| 	"text/template" | 	"text/template" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| @@ -25,6 +26,8 @@ type Ast struct { | |||||||
| 	BuildDate     time.Time                          `json:"build-date"` | 	BuildDate     time.Time                          `json:"build-date"` | ||||||
| 	BuildHostname string                             `json:"build-hostname"` | 	BuildHostname string                             `json:"build-hostname"` | ||||||
| 	BuildUser     string                             `json:"build-user"` | 	BuildUser     string                             `json:"build-user"` | ||||||
|  | 	GoPWD         string                             `json:"go-pwd",omitempty` | ||||||
|  | 	PWD           string                             `json:"pwd"` | ||||||
| 	Debug         bool                               `json:"debug"` | 	Debug         bool                               `json:"debug"` | ||||||
| 	File          *descriptor.FileDescriptorProto    `json:"file"` | 	File          *descriptor.FileDescriptorProto    `json:"file"` | ||||||
| 	Filename      string                             `json:"filename"` | 	Filename      string                             `json:"filename"` | ||||||
| @@ -86,10 +89,20 @@ func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (str | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	hostname, _ := os.Hostname() | 	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{ | 	ast := Ast{ | ||||||
| 		BuildDate:     time.Now(), | 		BuildDate:     time.Now(), | ||||||
| 		BuildHostname: hostname, | 		BuildHostname: hostname, | ||||||
| 		BuildUser:     os.Getenv("USER"), | 		BuildUser:     os.Getenv("USER"), | ||||||
|  | 		PWD:           pwd, | ||||||
|  | 		GoPWD:         goPwd, | ||||||
| 		File:          e.file, | 		File:          e.file, | ||||||
| 		Filename:      templateFilename, | 		Filename:      templateFilename, | ||||||
| 		Service:       e.service, | 		Service:       e.service, | ||||||
|   | |||||||
| @@ -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-hostname": "moul-rose.local", | ||||||
|   "build-user": "moul", |   "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, |   "debug": false, | ||||||
|   "file": { |   "file": { | ||||||
|     "name": "dummy.proto", |     "name": "dummy.proto", | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user