Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add optional file flags for cli view add command #3314

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 46 additions & 20 deletions cli/view_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,44 @@ import (
)

func MakeViewAddCommand() *cobra.Command {
var lensFile string
var cmd = &cobra.Command{
Use: "add [query] [sdl] [transform]",
var query, sdl, lens string
var queryFile, sdlFile, lensFile string
cmd := &cobra.Command{
Use: "add",
Short: "Add new view",
Long: `Add new database view.

Example: add from an argument string:
defradb client view add 'Foo { name, ...}' 'type Foo { ... }' '{"lenses": [...'
Example: add from string flags:
defradb client view add --query 'Foo { name, ...}' --sdl 'type Foo { ... }' --lens '{"lenses": [...'
Example: add from file flags:
defradb client view add --query-file /path/to/query --sdl-file /path/to/sdl --lens-file /path/to/lens

Flag pairs <key>/<key>-file are mutually exclusive.

Learn more about the DefraDB GraphQL Schema Language on https://docs.source.network.`,
Args: cobra.RangeArgs(2, 4),
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
store := mustGetContextStore(cmd)

query := args[0]
sdl := args[1]
query, err := pickDataOrReadFile(query, queryFile)
if err != nil {
return err
}
sdl, err := pickDataOrReadFile(sdl, sdlFile)
if err != nil {
return err
}
lensCfgJson, err := pickDataOrReadFile(lens, lensFile)
if err != nil {
return err
}

var lensCfgJson string
switch {
case lensFile != "":
data, err := os.ReadFile(lensFile)
if err != nil {
return err
}
lensCfgJson = string(data)
case len(args) == 3 && args[2] == "-":
if lensCfgJson == "-" {
data, err := io.ReadAll(cmd.InOrStdin())
if err != nil {
return err
}
lensCfgJson = string(data)
case len(args) == 3:
lensCfgJson = args[2]
}

var transform immutable.Option[model.Lens]
Expand All @@ -76,6 +81,27 @@ Learn more about the DefraDB GraphQL Schema Language on https://docs.source.netw
return writeJSON(cmd, defs)
},
}
cmd.Flags().StringVarP(&lensFile, "file", "f", "", "Lens configuration file")
cmd.Flags().StringVarP(&query, "query", "", "", "Query")
cmd.Flags().StringVarP(&queryFile, "query-file", "", "", "Query file")
cmd.Flags().StringVarP(&sdl, "sdl", "", "", "SDL")
cmd.Flags().StringVarP(&sdlFile, "sdl-file", "", "", "SDL file")
cmd.Flags().StringVarP(&lens, "lens", "", "", "Lens configuration")
cmd.Flags().StringVarP(&lensFile, "lens-file", "", "", "Lens configuration file")

cmd.MarkFlagsMutuallyExclusive("query", "query-file")
cmd.MarkFlagsMutuallyExclusive("sdl", "sdl-file")
cmd.MarkFlagsMutuallyExclusive("lens", "lens-file")
return cmd
}

// pickDataOrReadFile gets the result from file path when provided, or from data.
func pickDataOrReadFile(data string, dataPath string) (string, error) {
if dataPath == "" {
return data, nil
}
b, err := os.ReadFile(dataPath)
if err != nil {
return "", err
}
return string(b), nil
}
19 changes: 14 additions & 5 deletions docs/website/references/cli/defradb_client_view_add.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,29 @@ Add new view

Add new database view.

Example: add from an argument string:
defradb client view add 'Foo { name, ...}' 'type Foo { ... }' '{"lenses": [...'
Example: add from string flags:
defradb client view add --query 'Foo { name, ...}' --sdl 'type Foo { ... }' --lens '{"lenses": [...'
Example: add from file flags:
defradb client view add --query-file /path/to/query --sdl-file /path/to/sdl --lens-file /path/to/lens

Flag pairs <key>/<key>-file are mutually exclusive.

Learn more about the DefraDB GraphQL Schema Language on https://docs.source.network.

```
defradb client view add [query] [sdl] [transform] [flags]
defradb client view add [flags]
```

### Options

```
-f, --file string Lens configuration file
-h, --help help for add
--lens string Lens configuration
--lens-file string Lens configuration file
--query string Query
--query-file string Query file
--sdl string SDL
--sdl-file string SDL file
-h, --help help for add
```

### Options inherited from parent commands
Expand Down