Skip to content

Commit

Permalink
Merge pull request #259 from rsteube/rename-scrape
Browse files Browse the repository at this point in the history
renamed scrape
  • Loading branch information
rsteube authored Jan 29, 2024
2 parents 35d1d67 + 89318bf commit 9ec6b79
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
10 changes: 5 additions & 5 deletions cmd/carapace-spec/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ var rootCmd = &cobra.Command{
switch args[0] {
case "-h", "--help":
cmd.Help()
case "--scrape":
case "--codegen":
if len(args) < 2 {
return errors.New("flag needs an argument: --scrape")
return errors.New("flag needs an argument: --codegen")
}
command, err := loadSpec(args[1])
if err != nil {
return err
}
command.Scrape()
command.Codegen()
case "--run":
command, err := loadSpec(args[1])
if err != nil {
Expand Down Expand Up @@ -92,7 +92,7 @@ func Execute() error {
return rootCmd.Execute()
}
func init() {
rootCmd.Flags().Bool("scrape", false, "scrape to go code")
rootCmd.Flags().Bool("codegen", false, "generate code for spec file")
rootCmd.Flags().Bool("run", false, "run with given args")

carapace.Gen(rootCmd).PositionalCompletion(
Expand All @@ -110,7 +110,7 @@ func init() {
}
return carapace.ActionFiles(".yaml")

case c.Args[0] == "--scrape" && len(c.Args) == 1:
case c.Args[0] == "--codegen" && len(c.Args) == 1:
return carapace.ActionFiles(".yaml")

case !strings.HasPrefix(c.Args[0], "-"):
Expand Down
28 changes: 14 additions & 14 deletions scrape.go → codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
"github.com/spf13/pflag"
)

type scrapeXXX struct {
type codegenCmd struct {
cmd *cobra.Command
}

func (s scrapeXXX) formatHeader() string {
func (s codegenCmd) formatHeader() string {
return `package cmd
import (
"github.com/rsteube/carapace"
Expand All @@ -28,7 +28,7 @@ import (
`
}

func (s scrapeXXX) formatGroups() string {
func (s codegenCmd) formatGroups() string {
if len(s.cmd.Groups()) == 0 {
return ""
}
Expand All @@ -40,7 +40,7 @@ func (s scrapeXXX) formatGroups() string {
return fmt.Sprintf("%vCmd.AddGroup(\n%v\n)\n", cmdVarName(s.cmd), strings.Join(groups, "\n"))
}

func (s scrapeXXX) formatCommand() string {
func (s codegenCmd) formatCommand() string {
snippet := fmt.Sprintf(
`var %vCmd = &cobra.Command{
Use: "%v",
Expand Down Expand Up @@ -71,7 +71,7 @@ func (s scrapeXXX) formatCommand() string {
return snippet
}

func (s scrapeXXX) formatExecute() string {
func (s codegenCmd) formatExecute() string {
if s.cmd.HasParent() {
return ""
}
Expand All @@ -80,25 +80,25 @@ func (s scrapeXXX) formatExecute() string {
}
`
}
func Scrape(cmd *cobra.Command) {
dir, err := os.MkdirTemp(os.TempDir(), "carapace-scrape-")
func Codegen(cmd *cobra.Command) {
dir, err := os.MkdirTemp(os.TempDir(), "carapace-codegen-")
if err != nil {
panic(err.Error())
}

scrape(cmd, dir)
codegen(cmd, dir)
}

func scrape(cmd *cobra.Command, tmpDir string) {
func codegen(cmd *cobra.Command, tmpDir string) {
out := &bytes.Buffer{}
fmt.Fprintln(out, scrapeXXX{cmd}.formatHeader())
fmt.Fprintln(out, scrapeXXX{cmd}.formatCommand())
fmt.Fprintln(out, scrapeXXX{cmd}.formatExecute())
fmt.Fprintln(out, codegenCmd{cmd}.formatHeader())
fmt.Fprintln(out, codegenCmd{cmd}.formatCommand())
fmt.Fprintln(out, codegenCmd{cmd}.formatExecute())

fmt.Fprintf(out, `func init() {
carapace.Gen(%vCmd).Standalone()
%v
`, cmdVarName(cmd), scrapeXXX{cmd}.formatGroups())
`, cmdVarName(cmd), codegenCmd{cmd}.formatGroups())

cmd.LocalFlags().VisitAll(func(f *pflag.Flag) {
if f.Deprecated != "" {
Expand Down Expand Up @@ -170,7 +170,7 @@ func scrape(cmd *cobra.Command, tmpDir string) {

for _, subcmd := range cmd.Commands() {
if subcmd.Deprecated == "" && subcmd.Name() != "_carapace" {
scrape(subcmd, tmpDir)
codegen(subcmd, tmpDir)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ func (c Command) ToCobraE() (*cobra.Command, error) {
return cmd, nil
}

func (c Command) Scrape() {
func (c Command) Codegen() {
cmd, err := c.ToCobraE()
// TODO handle error
if err == nil {
Scrape(cmd)
Codegen(cmd)
}
}

Expand Down

0 comments on commit 9ec6b79

Please sign in to comment.