Add common method to convert from Project to map suitable for template expansion

This commit is contained in:
Luis Muñoz 2017-09-06 11:56:12 -07:00
parent b787445794
commit 9ef456ac44

View file

@ -2,6 +2,7 @@ package cmd
import ( import (
"os" "os"
"path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -16,6 +17,32 @@ type Project struct {
name string name string
} }
// ProjectToMap returns a map representation of a Project suitable for using
// in template expansion.
func (p *Project) ProjectToMap() map[string]interface{} {
ret := make(map[string]interface{})
ret["absPath"] = p.absPath
ret["cmdPath"] = p.cmdPath
ret["srcPath"] = p.srcPath
ret["name"] = p.name
m := make(map[string]string)
m["Name"] = p.license.Name
m["Text"] = p.license.Text
m["Header"] = p.license.Header
ret["License"] = m
ret["copyright"] = copyrightLine()
ret["importpath"] = path.Join(p.Name(), filepath.Base(p.CmdPath()))
ret["appName"] = path.Base(p.Name())
expHeader, _ := executeTemplate(p.License().Header, ret)
ret["license"] = expHeader
return ret
}
// NewProject returns Project with specified project name. // NewProject returns Project with specified project name.
// If projectName is blank string, it returns nil. // If projectName is blank string, it returns nil.
func NewProject(projectName string) *Project { func NewProject(projectName string) *Project {