11package  templatelib
22
33import  (
4- 	"encoding/json" 
54	"fmt" 
65	"os" 
76	"reflect" 
87	"strings" 
98	"text/template" 
9+ 
10+ 	"github.com/Masterminds/sprig/v3" 
1011)
1112
1213func  swapStringsFuncBoolArgsOrder (a  func (string , string ) bool ) func (string , string ) bool  {
@@ -68,55 +69,33 @@ func stringsModifierActionFactory(a func(string, string) string) func([]string,
6869	}
6970}
7071
71- var  FuncMap  =  template.FuncMap {
72- 	// {{- $isGitHub := hasPrefix "https://github.com/" $url -}} 
73- 	// {{- $isHtml := hasSuffix ".html" $url -}} 
74- 	"hasPrefix" : swapStringsFuncBoolArgsOrder (strings .HasPrefix ),
75- 	"hasSuffix" : swapStringsFuncBoolArgsOrder (strings .HasSuffix ),
76- 
77- 	// {{- $hugeIfTrue := .SomeValue | ternary "HUGE" "not so huge" -}} 
78- 	// if .SomeValue is truthy, $hugeIfTrue will be "HUGE" 
79- 	// (otherwise, "not so huge") 
80- 	"ternary" : func (truthy  interface {}, falsey  interface {}, val  interface {}) interface {} {
81- 		if  t , ok  :=  template .IsTrue (val ); ! ok  {
82- 			panic (fmt .Sprintf (`template.IsTrue(%+v) says things are NOT OK` , val ))
83- 		} else  if  t  {
84- 			return  truthy 
85- 		} else  {
86- 			return  falsey 
72+ func  FuncMap () template.FuncMap  {
73+ 	funcMap  :=  sprig .TxtFuncMap ()
74+ 
75+ 	// https://github.com/Masterminds/sprig/pull/276 
76+ 	funcMap ["ternary" ] =  func (vt  interface {}, vf  interface {}, v  interface {}) interface {} {
77+ 		if  truth , ok  :=  template .IsTrue (v ); ! ok  {
78+ 			panic (fmt .Sprintf (`template.IsTrue(%+v) says things are NOT OK` , v ))
79+ 		} else  if  truth  {
80+ 			return  vt 
8781		}
88- 	},
82+ 		return  vf 
83+ 	}
8984
90- 	// First Tag : {{- .Tags | first  -}} 
91- 	// Last Tag:   {{- .Tags | last  -}} 
92- 	"first" :  thingsActionFactory ( "first " , true , func ( args  [] interface {},  arg   interface {})  interface {} {  return   arg  }), 
93- 	"last" :   thingsActionFactory ( "last" ,  false ,  func ( args  [] interface {},  arg   interface {})  interface {} {  return   arg  }), 
85+ 	// Everybody : {{- join ", " .Names  -}} 
86+ 	// Concat:  {{- join "/" "https://github.com" "jsmith" "some-repo"  -}} 
87+ 	funcMap [ "join" ]  =   stringsActionFactory ( "join " , true , strings . Join ) 
88+ 	// (this differs slightly from the Sprig "join" in that it accepts either a list of strings or multiple arguments - Sprig instead has an explicit "list" function which can create a list of strings *from* a list of arguments so that multiple-signature usability like this is not necessary) 
9489
9590	// JSON data dump: {{ json . }} 
9691	// (especially nice for taking data and piping it to "jq") 
9792	// (ie "some-tool inspect --format '{{ json . }}' some-things | jq .") 
98- 	"json" : func (v  interface {}) (string , error ) {
99- 		j , err  :=  json .Marshal (v )
100- 		return  string (j ), err 
101- 	},
102- 
103- 	// Everybody: {{- join ", " .Names -}} 
104- 	// Concat: {{- join "/" "https://github.com" "jsmith" "some-repo" -}} 
105- 	"join" : stringsActionFactory ("join" , true , strings .Join ),
106- 
107- 	// {{- $mungedUrl := $url | replace "git://" "https://" | trimSuffixes ".git" -}} 
108- 	// turns: git://github.com/jsmith/some-repo.git 
109- 	// into: https://github.com/jsmith/some-repo 
110- 	"trimPrefixes" : stringsActionFactory ("trimPrefixes" , false , stringsModifierActionFactory (strings .TrimPrefix )),
111- 	"trimSuffixes" : stringsActionFactory ("trimSuffixes" , false , stringsModifierActionFactory (strings .TrimSuffix )),
112- 	"replace" : stringsActionFactory ("replace" , false , func (strs  []string , str  string ) string  {
113- 		return  strings .NewReplacer (strs ... ).Replace (str )
114- 	}),
93+ 	funcMap ["json" ] =  funcMap ["toJson" ]
11594
11695	// {{- getenv "PATH" -}} 
11796	// {{- getenv "HOME" "no HOME set" -}} 
11897	// {{- getenv "HOME" "is set" "is NOT set (or is empty)" -}} 
119- 	"getenv" :  thingsActionFactory ("getenv" , true , func (args  []interface {}, arg  interface {}) interface {} {
98+ 	funcMap [ "getenv" ]  =  thingsActionFactory ("getenv" , true , func (args  []interface {}, arg  interface {}) interface {} {
12099		var  (
121100			val                   =  os .Getenv (arg .(string ))
122101			setVal    interface {} =  val 
@@ -134,5 +113,13 @@ var FuncMap = template.FuncMap{
134113		} else  {
135114			return  unsetVal 
136115		}
137- 	}),
116+ 	})
117+ 
118+ 	// {{- $mungedUrl := $url | replace "git://" "https://" | trimSuffixes ".git" -}} 
119+ 	// turns: git://github.com/jsmith/some-repo.git 
120+ 	// into: https://github.com/jsmith/some-repo 
121+ 	funcMap ["trimPrefixes" ] =  stringsActionFactory ("trimPrefixes" , false , stringsModifierActionFactory (strings .TrimPrefix ))
122+ 	funcMap ["trimSuffixes" ] =  stringsActionFactory ("trimSuffixes" , false , stringsModifierActionFactory (strings .TrimSuffix ))
123+ 
124+ 	return  funcMap 
138125}
0 commit comments