mirror of
https://github.com/spf13/cobra
synced 2025-05-05 21:07:24 +00:00
Add common method to convert from Project to map suitable for template expansion
This commit is contained in:
parent
b787445794
commit
9ef456ac44
1 changed files with 27 additions and 0 deletions
|
@ -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 {
|
||||||
|
|
Loading…
Add table
Reference in a new issue