-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #437 from rsteube/preinvoke
added PreInvoke
- Loading branch information
Showing
11 changed files
with
300 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package carapace | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/rsteube/carapace/third_party/golang.org/x/sys/execabs" | ||
) | ||
|
||
// Context provides information during completion | ||
type Context struct { | ||
// CallbackValue contains the (partial) value (or part of it during an ActionMultiParts) currently being completed | ||
CallbackValue string | ||
// Args contains the positional arguments of current (sub)command (exclusive the one currently being completed) | ||
Args []string | ||
// Parts contains the splitted CallbackValue during an ActionMultiParts (exclusive the part currently being completed) | ||
Parts []string | ||
// Env contains environment variables for current context | ||
Env []string | ||
// Dir contains the working directory for current context | ||
Dir string | ||
} | ||
|
||
// Setenv sets the value of the environment variable named by the key. | ||
func (c *Context) Setenv(key, value string) { | ||
if c.Env == nil { | ||
c.Env = []string{} | ||
} | ||
c.Env = append(c.Env, fmt.Sprintf("%v=%v", key, value)) | ||
} | ||
|
||
// Command returns the Cmd struct to execute the named program with the given arguments. | ||
// Env and Dir are set using the Context. | ||
// See exec.Command for most details. | ||
func (c Context) Command(name string, arg ...string) *execabs.Cmd { | ||
cmd := execabs.Command(name, arg...) | ||
cmd.Env = c.Env | ||
cmd.Dir = c.Dir | ||
return cmd | ||
} | ||
|
||
func expandHome(s string) (string, error) { | ||
if strings.HasPrefix(s, "~") { | ||
home, err := os.UserHomeDir() // TODO duplicated code | ||
if err != nil { | ||
return "", err | ||
} | ||
s = strings.Replace(s, "~", home+"/", 1) | ||
} | ||
return s, nil | ||
} | ||
|
||
func (c Context) Abs(s string) (string, error) { | ||
var path string | ||
if strings.HasPrefix(s, "/") || strings.HasPrefix(s, "~/") { | ||
path = s // path is absolute | ||
} else { | ||
expanded, err := expandHome(c.Dir) | ||
if err != nil { | ||
return "", err | ||
} | ||
abs, err := filepath.Abs(expanded) | ||
if err != nil { | ||
return "", err | ||
} | ||
path = abs + "/" + s | ||
} | ||
|
||
expanded, err := expandHome(path) | ||
if err != nil { | ||
return "", err | ||
} | ||
path = expanded | ||
|
||
result, err := filepath.Abs(path) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if strings.HasSuffix(path, "/") && !strings.HasSuffix(result, "/") { | ||
result += "/" | ||
} else if strings.HasSuffix(path, "/.") && !strings.HasSuffix(result, "/.") { | ||
result += "/." | ||
} | ||
return result, nil | ||
} |
Oops, something went wrong.