You've already forked goreleaser
							
							
				mirror of
				https://github.com/goreleaser/goreleaser.git
				synced 2025-10-30 23:58:09 +02:00 
			
		
		
		
	feat: envOrDefault template function (#4097)
You can achieve the same with `{{ with (index .Env "NAME") }}etc`, but
this looks cleaner.
refs #4096
			
			
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							0805ec0553
						
					
				
				
					commit
					51592e1424
				
			| @@ -219,6 +219,7 @@ func (t *Template) Apply(s string) (string, error) { | ||||
| 			"filter":        filter(false), | ||||
| 			"reverseFilter": filter(true), | ||||
| 			"mdv2escape":    mdv2Escape, | ||||
| 			"envOrDefault":  t.envOrDefault, | ||||
| 		}). | ||||
| 		Parse(s) | ||||
| 	if err != nil { | ||||
| @@ -229,6 +230,14 @@ func (t *Template) Apply(s string) (string, error) { | ||||
| 	return out.String(), err | ||||
| } | ||||
|  | ||||
| func (t *Template) envOrDefault(name, value string) string { | ||||
| 	s, ok := t.fields[env].(context.Env)[name] | ||||
| 	if !ok { | ||||
| 		return value | ||||
| 	} | ||||
| 	return s | ||||
| } | ||||
|  | ||||
| type ExpectedSingleEnvErr struct{} | ||||
|  | ||||
| func (e ExpectedSingleEnvErr) Error() string { | ||||
|   | ||||
| @@ -91,6 +91,8 @@ func TestWithArtifact(t *testing.T) { | ||||
| 		"nightly false":                    `nightly {{.IsNightly}}`, | ||||
| 		"draft true":                       `draft {{.IsDraft}}`, | ||||
| 		"dirty true":                       `dirty {{.IsGitDirty}}`, | ||||
| 		"env bar: barrrrr":                 `env bar: {{ envOrDefault "BAR" "barrrrr" }}`, | ||||
| 		"env foo: bar":                     `env foo: {{ envOrDefault "FOO" "barrrrr" }}`, | ||||
|  | ||||
| 		"remove this": "{{ filter .Env.MULTILINE \".*remove.*\" }}", | ||||
| 		"something with\nmultiple lines\nto test things": "{{ reverseFilter .Env.MULTILINE \".*remove.*\" }}", | ||||
|   | ||||
| @@ -155,6 +155,7 @@ On all fields, you have these available functions: | ||||
| | `reverseFilter "text" "regex"` | keeps only the lines **not** matching the given regex, analogous to `grep -vE`. Since v1.6.                                     | | ||||
| | `title "foo"`                  | "titlenize" the string using english as language. See [Title](https://pkg.go.dev/golang.org/x/text/cases#Title). Since v1.14.   | | ||||
| | `mdv2escape "foo"`             | escape characters according to MarkdownV2, especially useful in the Telegram integration. Since v1.19.                          | | ||||
| | `envOrDefault "NAME" "value"`  | either gets the value of the given environment variable, or the given default. Since v1.19.                                     | | ||||
|  | ||||
| With all those fields, you may be able to compose the name of your artifacts | ||||
| pretty much the way you want: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user