terminal: expand ~ in paths passed to 'source' (#3387)

This commit is contained in:
Alessandro Arzilli 2023-06-07 05:52:19 +02:00 committed by GitHub
parent 5880f4cec9
commit c958128f21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,6 +19,7 @@ import (
"path/filepath"
"reflect"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
@ -2451,15 +2452,26 @@ func (c *Commands) sourceCommand(t *Term, ctx callContext, args string) error {
return fmt.Errorf("wrong number of arguments: source <filename>")
}
if args == "-" {
return t.starlarkEnv.REPL()
}
if runtime.GOOS != "windows" && strings.HasPrefix(args, "~") {
home, err := os.UserHomeDir()
if err == nil {
if args == "~" {
args = home
} else if strings.HasPrefix(args, "~/") {
args = filepath.Join(home, args[2:])
}
}
}
if filepath.Ext(args) == ".star" {
_, err := t.starlarkEnv.Execute(args, nil, "main", nil)
return err
}
if args == "-" {
return t.starlarkEnv.REPL()
}
return c.executeFile(t, args)
}