Skip to content

Commit 9a26b57

Browse files
robinbryceandreynering
authored andcommitted
Provides some further examples on using environment variables. (#1)
1 parent 70a304c commit 9a26b57

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

env-var-templating/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Environment Variables
2+
3+
Supplimental, examples of setting, overriding and constructing environment
4+
variables. There is nothing special about showenv.sh, it is simply a place
5+
holder for an external program. echo would do just as well
6+

env-var-templating/Taskfile.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# CGO environment variables are used as a motivating example.
2+
3+
flexible:
4+
# A task intended to be extended or called by others. Note the use of yaml
5+
# anchors "&flexible_env"
6+
cmds:
7+
- ./showenv.sh
8+
env: &flexible_env
9+
# OTHERPATH defaults to the emptystring in Taskvars.yml, and can be set
10+
# on the command line: OTHERPATH=other/path/ task flexible
11+
# Or in any task calling or extending this one
12+
CGO_LDFLAGS: "-L{{.OTHERPATH}}some/path/lib {{- range $_, $libname := .LIBNAMES | splitLines}}{{if $libname}} -l{{$libname}}{{end}}{{end}}"
13+
vars:
14+
LIBNAMES: |
15+
aaa
16+
bbb
17+
ccc
18+
19+
calling:
20+
cmds:
21+
- task: flexible
22+
vars:
23+
LIBNAMES: |
24+
xxx
25+
yyy
26+
27+
extending:
28+
cmds:
29+
- ./showenv.sh
30+
env:
31+
<<: *flexible_env
32+
CGO_ANOTHER_VAR: "extending env key, this is a shallow merge"
33+
vars:
34+
# These variables override any pulled in from flexible_env
35+
LIBNAMES: |
36+
ddd
37+
eee
38+
39+
40+
envproperty:
41+
cmds:
42+
- ./showenv.sh
43+
env:
44+
CGO_LDFLAGS: "-L/some/path -laaa -lbbb -lccc"
45+
46+
possible:
47+
# Notice the quotes guarding the variable references on the second line of
48+
# the cmds.
49+
cmds:
50+
- |
51+
CGO_LDFLAGS="-L/some/path -laaa -lbbb -lccc"
52+
CGO_LDFLAGS="$CGO_LDFLAGS" ./showenv.sh "$CGO_LDFLAGS"
53+
54+
wrong:
55+
# In this instance the quotes are missing, the output will be:
56+
#
57+
# CGO_LDFLAGS=-L/some/path-laaa-lbbb-lccc
58+
# $@=-L/some/path-laaa-lbbb-lccc
59+
cmds:
60+
- |
61+
CGO_LDFLAGS="-L/some/path -laaa -lbbb -lccc"
62+
CGO_LDFLAGS=$CGO_LDFLAGS ./showenv.sh $CGO_LDFLAGS

env-var-templating/Taskvars.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OTHERPATH: ""

env-var-templating/showenv.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
env | grep CGO_
3+
echo "\$@=$@"

0 commit comments

Comments
 (0)