Skip to content

Commit

Permalink
Merge pull request #2466 from carapace-sh/just-fix-quotes
Browse files Browse the repository at this point in the history
just: fix quotes
  • Loading branch information
rsteube authored Aug 1, 2024
2 parents 829430f + 2ac72ee commit 2d24ee5
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 43 deletions.
81 changes: 50 additions & 31 deletions completers/just_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,56 +23,75 @@ func init() {
carapace.Gen(rootCmd).Standalone()

rootCmd.Flags().Bool("changelog", false, "Print changelog")
rootCmd.Flags().Bool("check", false, "Run `--fmt` in 'check' mode. Exits with 0 if justfile is formatted correctly. Exits with 1 and prints a diff if formatting is required.")
rootCmd.Flags().Bool("choose", false, "Select one or more recipes to run using a binary. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`")
rootCmd.Flags().String("chooser", "", "Override binary invoked by `--choose`")
rootCmd.Flags().Bool("check", false, "Run `--fmt` in 'check' mode. Exits with 0 if justfile is formatted correctly. Exits with 1")
rootCmd.Flags().Bool("choose", false, "Select one or more recipes to run using a binary chooser. If `--chooser` is not passed the")
rootCmd.Flags().String("chooser", "", "Override binary invoked by `--choose` [env: JUST_CHOOSER=]")
rootCmd.Flags().Bool("clear-shell-args", false, "Clear shell arguments")
rootCmd.Flags().String("color", "", "Print colorful output [default: auto] [possible values: auto, always, never]")
rootCmd.Flags().String("color", "", "Print colorful output [env: JUST_COLOR=] [default: auto] [possible values: auto, always,")
rootCmd.Flags().StringP("command", "c", "", "Run an arbitrary command with the working directory, `.env`, overrides, and exports set")
rootCmd.Flags().String("completions", "", "Print shell completion script for <SHELL> [possible values: zsh, bash, fish, powershell, elvish]")
rootCmd.Flags().String("command-color", "", "Echo recipe lines in <COMMAND-COLOR> [env: JUST_COMMAND_COLOR=] [possible values: black,")
rootCmd.Flags().String("completions", "", "Print shell completion script for <SHELL> [possible values: bash, elvish, fish, nushell,")
rootCmd.Flags().String("dotenv-filename", "", "Search for environment file named <DOTENV-FILENAME> instead of `.env`")
rootCmd.Flags().String("dotenv-path", "", "Load environment file at <DOTENV-PATH> instead of searching for one")
rootCmd.Flags().Bool("dry-run", false, "Print what just would do without doing it")
rootCmd.Flags().StringP("dotenv-path", "E", "", "Load <DOTENV-PATH> as environment file instead of searching for one")
rootCmd.Flags().BoolP("dry-run", "n", false, "Print what just would do without doing it [env: JUST_DRY_RUN=]")
rootCmd.Flags().Bool("dump", false, "Print justfile")
rootCmd.Flags().String("dump-format", "", "Dump justfile as <FORMAT> [default: just] [possible values: just, json]")
rootCmd.Flags().String("dump-format", "", "Dump justfile as <FORMAT> [env: JUST_DUMP_FORMAT=] [default: just] [possible values: json,")
rootCmd.Flags().BoolP("edit", "e", false, "Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`")
rootCmd.Flags().Bool("evaluate", false, "Evaluate and print all variables. If a variable name is given as an argument, only print that variable's value.")
rootCmd.Flags().Bool("evaluate", false, "Evaluate and print all variables. If a variable name is given as an argument, only print")
rootCmd.Flags().Bool("fmt", false, "Format and overwrite justfile")
rootCmd.Flags().BoolP("help", "h", false, "Print help information")
rootCmd.Flags().Bool("highlight", false, "Highlight echoed recipe lines in bold")
rootCmd.Flags().BoolP("global-justfile", "g", false, "Use global justfile")
rootCmd.Flags().Bool("groups", false, "List recipe groups")
rootCmd.Flags().BoolP("help", "h", false, "Print help")
rootCmd.Flags().Bool("highlight", false, "Highlight echoed recipe lines in bold [env: JUST_HIGHLIGHT=]")
rootCmd.Flags().Bool("init", false, "Initialize new justfile in project root")
rootCmd.Flags().StringP("justfile", "f", "", "Use <JUSTFILE> as justfile")
rootCmd.Flags().BoolP("list", "l", false, "List available recipes and their arguments")
rootCmd.Flags().String("list-heading", "", "Print <TEXT> before list")
rootCmd.Flags().String("list-prefix", "", "Print <TEXT> before each list item")
rootCmd.Flags().Bool("no-dotenv", false, "Don't load `.env` file")
rootCmd.Flags().Bool("no-highlight", false, "Don't highlight echoed recipe lines in bold")
rootCmd.Flags().BoolP("quiet", "q", false, "Suppress all output")
rootCmd.Flags().String("set", "", "Override <VARIABLE> with <VALUE>")
rootCmd.Flags().StringP("justfile", "f", "", "Use <JUSTFILE> as justfile [env: JUST_JUSTFILE=]")
rootCmd.Flags().StringP("list", "l", "", "List available recipes")
rootCmd.Flags().String("list-heading", "", "Print <TEXT> before list [env: JUST_LIST_HEADING=] [default: \"Available recipes:\n\"]")
rootCmd.Flags().String("list-prefix", "", "Print <TEXT> before each list item [env: JUST_LIST_PREFIX=] [default: \" \"]")
rootCmd.Flags().Bool("list-submodules", false, "List recipes in submodules [env: JUST_LIST_SUBMODULES=]")
rootCmd.Flags().Bool("man", false, "Print man page")
rootCmd.Flags().Bool("no-aliases", false, "Don't show aliases in list [env: JUST_NO_ALIASES=]")
rootCmd.Flags().Bool("no-deps", false, "Don't run recipe dependencies [env: JUST_NO_DEPS=]")
rootCmd.Flags().Bool("no-dotenv", false, "Don't load `.env` file [env: JUST_NO_DOTENV=]")
rootCmd.Flags().Bool("no-highlight", false, "Don't highlight echoed recipe lines in bold [env: JUST_NO_HIGHLIGHT=]")
rootCmd.Flags().BoolP("quiet", "q", false, "Suppress all output [env: JUST_QUIET=]")
rootCmd.Flags().StringSlice("set", nil, "Override <VARIABLE> with <VALUE>")
rootCmd.Flags().String("shell", "", "Invoke <SHELL> to run recipes")
rootCmd.Flags().String("shell-arg", "", "Invoke shell with <SHELL-ARG> as an argument")
rootCmd.Flags().Bool("shell-command", false, "Invoke <COMMAND> with the shell used to run recipe lines and backticks")
rootCmd.Flags().StringP("show", "s", "", "Show information about <RECIPE>")
rootCmd.Flags().StringP("show", "s", "", "Show recipe at <PATH>")
rootCmd.Flags().Bool("summary", false, "List names of available recipes")
rootCmd.Flags().BoolP("unsorted", "u", false, "Return list and summary entries in source order")
rootCmd.Flags().Bool("unstable", false, "Enable unstable features")
rootCmd.Flags().Bool("timestamp", false, "Print recipe command timestamps [env: JUST_TIMESTAMP=]")
rootCmd.Flags().String("timestamp-format", "", "Timestamp format string [env: JUST_TIMESTAMP_FORMAT=] [default: %H:%M:%S]")
rootCmd.Flags().BoolP("unsorted", "u", false, "Return list and summary entries in source order [env: JUST_UNSORTED=]")
rootCmd.Flags().Bool("unstable", false, "Enable unstable features [env: JUST_UNSTABLE=]")
rootCmd.Flags().Bool("variables", false, "List names of variables")
rootCmd.Flags().BoolP("verbose", "v", false, "Use verbose output")
rootCmd.Flags().BoolP("version", "V", false, "Print version information")
rootCmd.Flags().StringP("working-directory", "d", "", "Use <WORKING-DIRECTORY> as working directory. --justfile must also be set")
rootCmd.Flags().CountP("verbose", "v", "Use verbose output [env: JUST_VERBOSE=]")
rootCmd.Flags().BoolP("version", "V", false, "Print version")
rootCmd.Flags().StringP("working-directory", "d", "", "Use <WORKING-DIRECTORY> as working directory. --justfile must also be set [env:")
rootCmd.Flags().Bool("yes", false, "Automatically confirm all recipes. [env: JUST_YES=]")

rootCmd.Flag("set").Nargs = 2

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"chooser": carapace.Batch(
carapace.ActionExecutables(),
carapace.ActionFiles(),
).ToA(),
"color": carapace.ActionValues("auto", "always", "never").StyleF(style.ForKeyword),
"completions": carapace.ActionValues("zsh", "bash", "fish", "powershell", "elvish"),
"dotenv-path": carapace.ActionDirectories(),
"dump-format": carapace.ActionValues("just", "json"),
"justfile": carapace.ActionFiles(),
"color": carapace.ActionValues("auto", "always", "never").StyleF(style.ForKeyword),
"command-color": carapace.ActionValues("auto", "always", "never").StyleF(style.ForKeyword),
"completions": carapace.ActionValues("bash", "elvish", "fish", "nushell", "powershell", "zsh"),
"dotenv-path": carapace.ActionDirectories(),
"dump-format": carapace.ActionValues("just", "json"),
"justfile": carapace.ActionFiles(),
"list": carapace.ActionFiles(),
"set": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return just.ActionVariables(rootCmd.Flag("justfile").Value.String())
switch len(c.Parts) {
case 0:
return just.ActionVariables(rootCmd.Flag("justfile").Value.String())
default:
return carapace.ActionValues()
}
}),
"shell": os.ActionShells(),
"show": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
Expand Down
63 changes: 51 additions & 12 deletions pkg/actions/tools/just/recipe.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,71 @@
package just

import (
"strings"
"encoding/json"

"github.com/carapace-sh/carapace"
)

type attributes []map[string]string

func (a attributes) Description() string {
for _, attr := range a {
if doc, ok := attr["doc"]; ok {
return doc
}
}
return ""
}

type recipe struct {
Attributes attributes `json:"attributes"`
Name string `json:"name"`
Doc string `json:"doc"`
}

func (r recipe) Description() string {
if doc := r.Doc; doc != "" {
return doc
}
return r.Attributes.Description()
}

type justfile struct {
Aliases map[string]struct {
Attributes attributes `json:"attributes"`
Name string `json:"name"`
Target string `json:"target"`
} `json:"aliases"`
Recipes map[string]recipe `json:"recipes"`
}

// ActionRecipes completes recipes
// default
// build (build project)
func ActionRecipes(justfile string) carapace.Action {
func ActionRecipes(path string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
args := []string{"--list"}
if justfile != "" {
args = append(args, "--justfile", justfile)
args := []string{"--dump", "--dump-format", "json"}
if path != "" {
args = append(args, "--justfile", path)
}

return carapace.ActionExecCommand("just", args...)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
var jf justfile
if err := json.Unmarshal(output, &jf); err != nil {
return carapace.ActionMessage(err.Error())
}

vals := make([]string, 0)
for _, line := range lines[1 : len(lines)-1] {
if splitted := strings.SplitN(line, "#", 2); len(splitted) == 2 {
vals = append(vals, strings.TrimSpace(splitted[0]), strings.TrimSpace(splitted[1]))
} else {
vals = append(vals, strings.TrimSpace(splitted[0]), "")
for _, recipe := range jf.Recipes {
vals = append(vals, recipe.Name, recipe.Description())
}

for _, alias := range jf.Aliases {
if recipe, ok := jf.Recipes[alias.Target]; ok {
vals = append(vals, alias.Name, recipe.Description())
}
}
return carapace.ActionValuesDescribed(vals...)
})
})
}).Tag("recipes")
}
1 change: 1 addition & 0 deletions pkg/actions/tools/just/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
// variableb
func ActionVariables(justfile string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
// TODO use dump
args := []string{"--variables"}
if justfile != "" {
args = append(args, "--justfile", justfile)
Expand Down

0 comments on commit 2d24ee5

Please sign in to comment.