github.com/hashicorp/packer@v1.14.3/hcl2template/function/env.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package function 5 6 import ( 7 "os" 8 9 "github.com/zclconf/go-cty/cty" 10 "github.com/zclconf/go-cty/cty/function" 11 ) 12 13 // EnvFunc constructs a function that returns a string representation of the 14 // env var behind a value 15 var EnvFunc = function.New(&function.Spec{ 16 Params: []function.Parameter{ 17 { 18 Name: "key", 19 Type: cty.String, 20 AllowNull: false, 21 AllowUnknown: false, 22 }, 23 }, 24 Type: function.StaticReturnType(cty.String), 25 RefineResult: refineNotNull, 26 Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) { 27 key := args[0].AsString() 28 value := os.Getenv(key) 29 return cty.StringVal(value), nil 30 }, 31 }) 32 33 // Env returns a string representation of the env var behind key. 34 func Env(key cty.Value) (cty.Value, error) { 35 return EnvFunc.Call([]cty.Value{key}) 36 }