From c958128f2143d13636bcda5afc1951dc445c576f Mon Sep 17 00:00:00 2001 From: Alessandro Arzilli Date: Wed, 7 Jun 2023 05:52:19 +0200 Subject: [PATCH] terminal: expand ~ in paths passed to 'source' (#3387) --- pkg/terminal/command.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/pkg/terminal/command.go b/pkg/terminal/command.go index 3103af8d..ef83ea46 100644 --- a/pkg/terminal/command.go +++ b/pkg/terminal/command.go @@ -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 ") } + 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) }