-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
f29f6aa
commit 52210b1
Showing
8 changed files
with
257 additions
and
16 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"github.com/webhookdb/webhookdb-cli/formatting" | ||
"github.com/webhookdb/webhookdb-cli/types" | ||
) | ||
|
||
type SavedQueryListInput struct { | ||
OrgIdentifier types.OrgIdentifier `json:"-"` | ||
} | ||
|
||
func SavedQueryList(c context.Context, auth Auth, input SavedQueryListInput) (out types.CollectionResponse, err error) { | ||
err = makeRequest(c, GET, auth, nil, &out, "/v1/organizations/%v/saved_queries", input.OrgIdentifier) | ||
return | ||
} | ||
|
||
type SavedQueryCreateInput struct { | ||
OrgIdentifier types.OrgIdentifier `json:"-"` | ||
Description string `json:"description"` | ||
Sql string `json:"sql"` | ||
Public bool `json:"public"` | ||
} | ||
|
||
func SavedQueryCreate(c context.Context, auth Auth, input SavedQueryCreateInput) (out types.MessageResponse, err error) { | ||
err = makeRequest(c, POST, auth, input, &out, "/v1/organizations/%s/saved_queries/create", input.OrgIdentifier) | ||
return | ||
} | ||
|
||
type SavedQueryIdentifierInput struct { | ||
OrgIdentifier types.OrgIdentifier `json:"-"` | ||
Identifier string `json:"-"` | ||
} | ||
|
||
func SavedQueryDelete(c context.Context, auth Auth, input SavedQueryIdentifierInput) (out types.MessageResponse, err error) { | ||
err = makeRequest(c, POST, auth, input, &out, "/v1/organizations/%v/saved_queries/%v/delete", input.OrgIdentifier, input.Identifier) | ||
return | ||
} | ||
|
||
type SavedQueryUpdateInput struct { | ||
OrgIdentifier types.OrgIdentifier `json:"-"` | ||
Identifier string `json:"-"` | ||
Field string `json:"field"` | ||
Value string `json:"value"` | ||
} | ||
|
||
func SavedQueryUpdate(c context.Context, auth Auth, input SavedQueryUpdateInput) (out types.MessageResponse, err error) { | ||
err = makeRequest(c, POST, auth, input, &out, "/v1/organizations/%v/saved_queries/%v/update", input.OrgIdentifier, input.Identifier) | ||
return | ||
} | ||
|
||
type SavedQueryInfoInput struct { | ||
OrgIdentifier types.OrgIdentifier `json:"-"` | ||
Identifier string `json:"-"` | ||
Field string `json:"field"` | ||
} | ||
|
||
type SavedQueryInfoOutput struct { | ||
Blocks formatting.Blocks `json:"blocks"` | ||
} | ||
|
||
func SavedQueryInfo(c context.Context, auth Auth, input SavedQueryInfoInput) (out SavedQueryInfoOutput, err error) { | ||
err = makeRequest(c, POST, auth, input, &out, "/v1/organizations/%v/saved_queries/%v/info", input.OrgIdentifier, input.Identifier) | ||
return | ||
} | ||
|
||
func SavedQueryRun(c context.Context, auth Auth, input SavedQueryIdentifierInput) (out types.RunQueryOutput, err error) { | ||
err = makeRequest(c, GET, auth, input, &out, "/v1/organizations/%v/saved_queries/%v/run", input.OrgIdentifier, input.Identifier) | ||
return | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"github.com/urfave/cli/v2" | ||
"github.com/webhookdb/webhookdb-cli/appcontext" | ||
"github.com/webhookdb/webhookdb-cli/client" | ||
) | ||
|
||
var savedQueriesCmd = &cli.Command{ | ||
Name: "saved-query", | ||
Aliases: []string{"saved-queries", "custom-query", "custom-queries"}, | ||
Usage: "Manage your library of saved queries, including ones that can be accessed publicly.", | ||
Subcommands: []*cli.Command{ | ||
{ | ||
Name: "create", | ||
Usage: "Create a new saved query.", | ||
Flags: []cli.Flag{ | ||
orgFlag(), | ||
&cli.StringFlag{ | ||
Name: "description", | ||
Aliases: []string{"d", "desc"}, | ||
Usage: "Explain what this query is used for.", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "sql", | ||
Usage: "SQL statement to run.", | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "public", | ||
Usage: "If true, the query can be accessed publicly, without authentication. " + | ||
"Allows a saved query to be used on public dashboards or websites, " + | ||
"without exposing a database connection string or " + | ||
"allowing public access to a database.", | ||
}, | ||
}, | ||
Action: cliAction(func(c *cli.Context, ac appcontext.AppContext, ctx context.Context) error { | ||
input := client.SavedQueryCreateInput{ | ||
OrgIdentifier: getOrgFlag(c, ac.Prefs), | ||
Description: c.String("description"), | ||
Sql: c.String("sql"), | ||
Public: false, | ||
} | ||
out, err := client.SavedQueryCreate(ctx, ac.Auth, input) | ||
if err != nil { | ||
return err | ||
} | ||
printlnif(c, out.Message, false) | ||
return nil | ||
}), | ||
}, | ||
{ | ||
Name: "list", | ||
Usage: "List all saved queries.", | ||
Flags: []cli.Flag{ | ||
orgFlag(), | ||
formatFlag(), | ||
}, | ||
Action: cliAction(func(c *cli.Context, ac appcontext.AppContext, ctx context.Context) error { | ||
input := client.SavedQueryListInput{ | ||
OrgIdentifier: getOrgFlag(c, ac.Prefs), | ||
} | ||
out, err := client.SavedQueryList(ctx, ac.Auth, input) | ||
if err != nil { | ||
return err | ||
} | ||
printlnif(c, out.Message(), true) | ||
return getFormatFlag(c).WriteCollection(c.App.Writer, out) | ||
}), | ||
}, | ||
{ | ||
Name: "update", | ||
Usage: "Update a new saved query.", | ||
Aliases: []string{"edit", "modify"}, | ||
Flags: []cli.Flag{ | ||
orgFlag(), | ||
savedQueryFlag(), | ||
fieldFlag(), | ||
valueFlag(), | ||
}, | ||
Action: cliAction(func(c *cli.Context, ac appcontext.AppContext, ctx context.Context) error { | ||
input := client.SavedQueryUpdateInput{ | ||
OrgIdentifier: getOrgFlag(c, ac.Prefs), | ||
Identifier: getSavedQueryArgOrFlag(c), | ||
Field: c.String("field"), | ||
Value: c.String("value"), | ||
} | ||
out, err := client.SavedQueryUpdate(ctx, ac.Auth, input) | ||
if err != nil { | ||
return err | ||
} | ||
printlnif(c, out.Message, false) | ||
return nil | ||
}), | ||
}, | ||
{ | ||
Name: "info", | ||
Usage: "Display information about given saved query.", | ||
Flags: []cli.Flag{ | ||
orgFlag(), | ||
savedQueryFlag(), | ||
&cli.StringFlag{ | ||
Name: "field", | ||
Aliases: s1("f"), | ||
Usage: "The field that you want information about.", | ||
}, | ||
}, | ||
Action: cliAction(func(c *cli.Context, ac appcontext.AppContext, ctx context.Context) error { | ||
input := client.SavedQueryInfoInput{ | ||
OrgIdentifier: getOrgFlag(c, ac.Prefs), | ||
Identifier: getSavedQueryArgOrFlag(c), | ||
Field: c.String("field"), | ||
} | ||
out, err := client.SavedQueryInfo(ctx, ac.Auth, input) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = out.Blocks.WriteTo(c.App.Writer) | ||
return err | ||
}), | ||
}, | ||
{ | ||
Name: "run", | ||
Usage: "Run the query.", | ||
Flags: []cli.Flag{ | ||
savedQueryFlag(), | ||
colorFlag(), | ||
}, | ||
Action: cliAction(func(c *cli.Context, ac appcontext.AppContext, ctx context.Context) error { | ||
useColors := c.Bool("color") | ||
out, err := client.SavedQueryRun(ctx, ac.Auth, client.SavedQueryIdentifierInput{ | ||
OrgIdentifier: getOrgFlag(c, ac.Prefs), | ||
Identifier: getSavedQueryArgOrFlag(c), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
err = printSqlOutput(c, out, useColors) | ||
return nil | ||
}), | ||
}, | ||
{ | ||
Name: "delete", | ||
Usage: "Delete this saved query.", | ||
Flags: []cli.Flag{savedQueryFlag()}, | ||
Action: cliAction(func(c *cli.Context, ac appcontext.AppContext, ctx context.Context) error { | ||
out, err := client.SavedQueryDelete(ctx, ac.Auth, client.SavedQueryIdentifierInput{ | ||
OrgIdentifier: getOrgFlag(c, ac.Prefs), | ||
Identifier: getSavedQueryArgOrFlag(c), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
printlnif(c, out.Message, false) | ||
return nil | ||
}), | ||
}, | ||
}, | ||
} | ||
|
||
func savedQueryFlag() *cli.StringFlag { | ||
return &cli.StringFlag{ | ||
Name: "saved-query", | ||
Aliases: s1("q"), | ||
Usage: usage("Saved query opaque id. Run `webhookdb saved-query list` to see a list of all your saved queries."), | ||
} | ||
} | ||
|
||
func getSavedQueryArgOrFlag(c *cli.Context) string { | ||
return requireFlagOrArg(c, "saved-query", "Use `webhookdb saved-query list` to see available saved queries.") | ||
} |
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