Skip to content

Commit

Permalink
feat: if no action, display docs
Browse files Browse the repository at this point in the history
Signed-off-by: Yves Brissaud <[email protected]>
  • Loading branch information
eunomie committed Oct 10, 2024
1 parent fb9c479 commit 2265af8
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions internal/commands/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func NewCmd(dockerCli command.Cli, isPlugin bool) *cobra.Command {
return err
}

if action == "" && !list && !docs && len(rk.Config.Actions) == 0 {
_, _ = fmt.Fprintln(dockerCli.Out(), tui.Markdown(rk.Readme))
return nil
}

if docs {
_, _ = fmt.Fprintln(dockerCli.Out(), tui.Markdown(rk.Readme+"\n---\n"+mdActions(rk)))
return nil
Expand All @@ -99,7 +104,7 @@ func NewCmd(dockerCli command.Cli, isPlugin bool) *cobra.Command {
action = selectAction(action, lc.Images[src], rk.Config.Default)

if list || action == "" {
if tui.IsATTY(dockerCli.In().FD()) {
if tui.IsATTY(dockerCli.In().FD()) && len(rk.Config.Actions) > 0 {
selectedAction := prompt.SelectAction(rk.Config.Actions)
if selectedAction != "" {
return run(cmd.Context(), dockerCli.Err(), src, rk, selectedAction)
Expand Down Expand Up @@ -232,18 +237,22 @@ func mdActions(rk *runkit.RunKit) string {
p := pluralize.NewClient()
s := strings.Builder{}
s.WriteString("# Available actions\n\n")
for _, action := range rk.Config.Actions {
if action.Desc != "" {
s.WriteString(fmt.Sprintf(" - `%s`: %s\n", action.ID, action.Desc))
} else {
s.WriteString(fmt.Sprintf(" - `%s`\n", action.ID))
}
vars := "variable"
if len(action.Env) > 1 {
vars = p.Plural(vars)
}
if len(action.Env) > 0 {
s.WriteString(" - Environment " + vars + ": " + strings.Join(tui.BackQuoteItems(action.Env), ", ") + "\n")
if len(rk.Config.Actions) == 0 {
s.WriteString("> No available action\n")
} else {
for _, action := range rk.Config.Actions {
if action.Desc != "" {
s.WriteString(fmt.Sprintf(" - `%s`: %s\n", action.ID, action.Desc))
} else {
s.WriteString(fmt.Sprintf(" - `%s`\n", action.ID))
}
vars := "variable"
if len(action.Env) > 1 {
vars = p.Plural(vars)
}
if len(action.Env) > 0 {
s.WriteString(" - Environment " + vars + ": " + strings.Join(tui.BackQuoteItems(action.Env), ", ") + "\n")
}
}
}

Expand Down

0 comments on commit 2265af8

Please sign in to comment.