-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
65 additions
and
23 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
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 | ||
} |
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,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 | ||
} |
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