From cbd891785ebfcf058c1ed4ca09096130f312fab4 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 29 Jul 2024 11:06:33 +0800 Subject: [PATCH] library: os.Expand --- _cmptest/envexpand/expand.go | 20 ++++++++++++++++++++ internal/lib/os/env.go | 2 -- 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 _cmptest/envexpand/expand.go diff --git a/_cmptest/envexpand/expand.go b/_cmptest/envexpand/expand.go new file mode 100644 index 00000000..0c4baaf0 --- /dev/null +++ b/_cmptest/envexpand/expand.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "os" +) + +func main() { + mapper := func(placeholderName string) string { + switch placeholderName { + case "DAY_PART": + return "morning" + case "NAME": + return "Gopher" + } + return "" + } + + fmt.Println(os.Expand("Good ${DAY_PART}, $NAME!", mapper)) +} diff --git a/internal/lib/os/env.go b/internal/lib/os/env.go index 7c0f3174..550dac19 100644 --- a/internal/lib/os/env.go +++ b/internal/lib/os/env.go @@ -10,7 +10,6 @@ import ( "syscall" ) -/* TODO(xsw): // Expand replaces ${var} or $var in the string based on the mapping function. // For example, os.ExpandEnv(s) is equivalent to os.Expand(s, os.Getenv). func Expand(s string, mapping func(string) string) string { @@ -94,7 +93,6 @@ func getShellName(s string) (string, int) { } return s[:i], i } -*/ // Getenv retrieves the value of the environment variable named by the key. // It returns the value, which will be empty if the variable is not present.