Skip to content

Commit

Permalink
added scrape flag
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Oct 26, 2022
1 parent 4e36208 commit 0a7c3e9
Show file tree
Hide file tree
Showing 10 changed files with 370 additions and 215 deletions.
5 changes: 5 additions & 0 deletions cmd/carapace/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ var rootCmd = &cobra.Command{
} else {
fmt.Fprintln(cmd.OutOrStdout(), schema)
}
case "--scrape":
if len(args) > 1 {
scrape(args[1])
}
case "--style":
if len(args) > 1 {
if err := setStyle(args[1]); err != nil {
Expand Down Expand Up @@ -327,6 +331,7 @@ func Execute(version string) error {
func init() {
rootCmd.Flags().String("bridge", "", "bridge completion")
rootCmd.Flags().Bool("list", false, "list completers")
rootCmd.Flags().String("scrape", "", "scrape spec to go code")
rootCmd.Flags().String("spec", "", "spec completion")
rootCmd.Flags().String("style", "", "set style")

Expand Down
15 changes: 11 additions & 4 deletions cmd/carapace/cmd/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"bytes"
"fmt"
"github.com/rsteube/carapace-spec"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"io"
"os"
"path/filepath"
"strings"
)

func loadSpec(path string) (string, *cobra.Command, error) {
func loadSpec(path string) (string, *spec.Command, error) {
abs, err := filepath.Abs(path)
if err != nil {
return "", nil, err
Expand All @@ -27,7 +26,14 @@ func loadSpec(path string) (string, *cobra.Command, error) {
if err := yaml.Unmarshal(content, &specCmd); err != nil {
return "", nil, err
}
return abs, specCmd.ToCobra(), nil
return abs, &specCmd, nil
}

func scrape(path string) {
// TODO yuck - all this needs some cleanup
if _, spec, err := loadSpec(path); err == nil {
spec.Scrape()
}
}

func specCompletion(path string, args ...string) {
Expand All @@ -43,10 +49,11 @@ func specCompletion(path string, args ...string) {
outC <- buf.String()
}()

abs, cmd, err := loadSpec(path)
abs, spec, err := loadSpec(path)
if err != nil {
return /// TODO handle error
}
cmd := spec.ToCobra()

a := []string{"_carapace"}
a = append(a, args...)
Expand Down
6 changes: 4 additions & 2 deletions completers/carapace_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func flagCmd(args []string) *cobra.Command {
cmd.Flags().String("list", "", "list completers")
cmd.Flags().String("macros", "test local json schema", "list spec macros")
cmd.Flags().Bool("schema", false, "json schema for spec files")
cmd.Flags().String("scrape", "", "scrape spec to go code")
cmd.Flags().String("spec", "", "spec completion")
cmd.Flags().String("style", "", "set style")
cmd.Flags().BoolP("version", "v", false, "version for carapace")
Expand Down Expand Up @@ -84,8 +85,9 @@ func flagCmd(args []string) *cobra.Command {
}
return carapace.ActionValuesDescribed(vals...).Invoke(carapace.Context{}).ToMultiPartsA(".")
}),
"spec": carapace.ActionFiles(".yaml"),
"style": carapace.ActionStyleConfig(),
"scrape": carapace.ActionFiles(".yaml"),
"spec": carapace.ActionFiles(".yaml"),
"style": carapace.ActionStyleConfig(),
})

carapace.Gen(cmd).PositionalAnyCompletion(
Expand Down
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [Bridge](./spec/bride.md)
- [Embed](./spec/embed.md)
- [Generate](./spec/generate.md)
- [Scrape](./spec/scrape.md)
- [Examples](./spec/examples.md)
- [Macros](./spec/macros.md)
- [Development](./development.md)
Expand Down
337 changes: 337 additions & 0 deletions docs/src/spec/scrape.cast

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions docs/src/spec/scrape.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Scrape

[Specs](../spec.md) can be scraped to go code.

```sh
carapace --scrape [spec]
```

![](./scrape.cast)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/pelletier/go-toml v1.9.5
github.com/rsteube/carapace v0.25.1
github.com/rsteube/carapace-spec v0.1.3
github.com/rsteube/carapace-spec v0.2.1
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
gopkg.in/ini.v1 v1.67.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ github.com/rsteube/carapace v0.25.1 h1:zF6gJqrQ82fPv15A8APh2giAjj/IXX1ENri/svNm7
github.com/rsteube/carapace v0.25.1/go.mod h1:3N1bLcmfoBOn0WwCniEhVF52AqvN//zyV1JrtT52ECw=
github.com/rsteube/carapace-pflag v0.0.4 h1:Onb0cLNLxg1xJr2EsMlBldAI5KkybrvZ89b5cRElZXI=
github.com/rsteube/carapace-pflag v0.0.4/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/rsteube/carapace-spec v0.1.3 h1:rTsETXp0XyQ6LqUTzkQgOBZ3I/NrtKEcc3mapfWkYBc=
github.com/rsteube/carapace-spec v0.1.3/go.mod h1:vhyWE0ml+1RhJNWgVBDBOaSQtTDIX9uaOqBZeASRfPk=
github.com/rsteube/carapace-spec v0.2.1 h1:VuXLp0uTvd6IXe207+eAyUwvMweZS57JDBwNYq4Gjc4=
github.com/rsteube/carapace-spec v0.2.1/go.mod h1:dBCF8d0tMCKU6QMReRRTI1rgwKJTN/zZeoGMqzxzUf4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.6.0/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
Expand Down
173 changes: 0 additions & 173 deletions pkg/carascrape/scrape.go

This file was deleted.

33 changes: 0 additions & 33 deletions pkg/carascrape/scrape_test.go

This file was deleted.

0 comments on commit 0a7c3e9

Please sign in to comment.