Skip to content

Commit

Permalink
Add print command
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed Jul 20, 2022
1 parent ceabf69 commit 3cb900a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
26 changes: 26 additions & 0 deletions internal/cmd/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"strings"

"github.com/spf13/cobra"
)

func validCmdNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
p, err := newParser()
if err != nil {
cmd.PrintErrf("failed to get parser: %s", err)
return nil, cobra.ShellCompDirectiveError
}

names := p.Snippets().Names()

var filtered []string
for _, name := range names {
if strings.HasPrefix(name, toComplete) {
filtered = append(filtered, name)
}
}

return filtered, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
}
33 changes: 33 additions & 0 deletions internal/cmd/print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func printCmd() *cobra.Command {
cmd := cobra.Command{
Use: "print",
Short: "Print a selected snippet.",
Args: cobra.ExactArgs(1),
ValidArgsFunction: validCmdNames,
RunE: func(cmd *cobra.Command, args []string) error {
p, err := newParser()
if err != nil {
return errors.Wrap(err, "fail to read README file")
}

snippets := p.Snippets()

snippet, found := snippets.Lookup(args[0])
if !found {
return errors.Errorf("command %q not found; known command names: %s", args[0], snippets.Names())
}

_, err = cmd.OutOrStdout().Write([]byte(snippet.Content()))
return errors.Wrap(err, "failed to write to stdout")
},
}

return &cmd
}
1 change: 1 addition & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func Root() *cobra.Command {

cmd.AddCommand(runCmd())
cmd.AddCommand(listCmd())
cmd.AddCommand(printCmd())
cmd.AddCommand(tasksCmd())

return &cmd
Expand Down
28 changes: 5 additions & 23 deletions internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"os"
"os/signal"
"strings"
"syscall"

"github.com/pkg/errors"
Expand All @@ -15,28 +14,11 @@ import (

func runCmd() *cobra.Command {
cmd := cobra.Command{
Use: "run",
Aliases: []string{"exec"},
Short: "Run a selected command.",
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
p, err := newParser()
if err != nil {
cmd.PrintErrf("failed to get parser: %s", err)
return nil, cobra.ShellCompDirectiveError
}

names := p.Snippets().Names()

var filtered []string
for _, name := range names {
if strings.HasPrefix(name, toComplete) {
filtered = append(filtered, name)
}
}

return filtered, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
},
Use: "run",
Aliases: []string{"exec"},
Short: "Run a selected command.",
Args: cobra.ExactArgs(1),
ValidArgsFunction: validCmdNames,
RunE: func(cmd *cobra.Command, args []string) error {
p, err := newParser()
if err != nil {
Expand Down

0 comments on commit 3cb900a

Please sign in to comment.