1package fsext
2
3import (
4 "os"
5 "strings"
6
7 "mvdan.cc/sh/v3/expand"
8 "mvdan.cc/sh/v3/syntax"
9)
10
11// Expand is a wrapper around [expand.Literal]. It will escape the input
12// string, expand any shell symbols (such as '~') and resolve any environment
13// variables.
14func Expand(s string) (string, error) {
15 if s == "" {
16 return "", nil
17 }
18 p := syntax.NewParser()
19 word, err := p.Document(strings.NewReader(s))
20 if err != nil {
21 return "", err
22 }
23 cfg := &expand.Config{
24 Env: expand.FuncEnviron(os.Getenv),
25 ReadDir2: os.ReadDir,
26 GlobStar: true,
27 }
28 return expand.Literal(cfg, word)
29}