From 9ef456ac441a8800d735aaa880ed19663fc3d9c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Mun=CC=83oz?= Date: Wed, 6 Sep 2017 11:56:12 -0700 Subject: [PATCH] Add common method to convert from Project to map suitable for template expansion --- cobra/cmd/project.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/cobra/cmd/project.go b/cobra/cmd/project.go index de1168a1..22eb4937 100644 --- a/cobra/cmd/project.go +++ b/cobra/cmd/project.go @@ -2,6 +2,7 @@ package cmd import ( "os" + "path" "path/filepath" "runtime" "strings" @@ -16,6 +17,32 @@ type Project struct { 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. // If projectName is blank string, it returns nil. func NewProject(projectName string) *Project {