-
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.
Add support for saved views (beta feature)
See webhookdb/webhookdb#866 for API.
- Loading branch information
1 parent
7a525ad
commit 3cdcb9b
Showing
3 changed files
with
130 additions
and
0 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,36 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"github.com/webhookdb/webhookdb-cli/types" | ||
) | ||
|
||
type SavedViewListInput struct { | ||
OrgIdentifier types.OrgIdentifier `json:"-"` | ||
} | ||
|
||
func SavedViewList(c context.Context, auth Auth, input SavedViewListInput) (out types.CollectionResponse, err error) { | ||
err = makeRequest(c, GET, auth, nil, &out, "/v1/organizations/%v/saved_views", input.OrgIdentifier) | ||
return | ||
} | ||
|
||
type SavedViewCreateInput struct { | ||
OrgIdentifier types.OrgIdentifier `json:"-"` | ||
Name string `json:"name"` | ||
Sql string `json:"sql"` | ||
} | ||
|
||
func SavedViewCreate(c context.Context, auth Auth, input SavedViewCreateInput) (out types.MessageResponse, err error) { | ||
err = makeRequest(c, POST, auth, input, &out, "/v1/organizations/%s/saved_views/create_or_replace", input.OrgIdentifier) | ||
return | ||
} | ||
|
||
type SavedViewDeleteInput struct { | ||
OrgIdentifier types.OrgIdentifier `json:"-"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func SavedViewDelete(c context.Context, auth Auth, input SavedViewDeleteInput) (out types.MessageResponse, err error) { | ||
err = makeRequest(c, POST, auth, input, &out, "/v1/organizations/%v/saved_views/delete", input.OrgIdentifier) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"github.com/urfave/cli/v2" | ||
"github.com/webhookdb/webhookdb-cli/appcontext" | ||
"github.com/webhookdb/webhookdb-cli/client" | ||
) | ||
|
||
var savedViewsCmd = &cli.Command{ | ||
Name: "saved-view", | ||
Aliases: []string{"saved-views", "view", "views"}, | ||
Usage: "Create, replace, and drop views in your database.", | ||
Subcommands: []*cli.Command{ | ||
{ | ||
Name: "create-or-replace", | ||
Usage: "Create or replace a view.", | ||
Aliases: []string{"create", "replace", "new", "update"}, | ||
Flags: []cli.Flag{ | ||
orgFlag(), | ||
&cli.StringFlag{ | ||
Name: "name", | ||
Aliases: []string{"n"}, | ||
Usage: "The name of the view. Must be a database identifier (alphanumeric, spaces, and underscores).", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "sql", | ||
Usage: "SQL SELECT statement to run. Must include the 'CREATE VIEW' part of the query.", | ||
}, | ||
}, | ||
Action: cliAction(func(c *cli.Context, ac appcontext.AppContext, ctx context.Context) error { | ||
input := client.SavedViewCreateInput{ | ||
OrgIdentifier: getOrgFlag(c, ac.Prefs), | ||
Name: c.String("name"), | ||
Sql: c.String("sql"), | ||
} | ||
out, err := client.SavedViewCreate(ctx, ac.Auth, input) | ||
if err != nil { | ||
return err | ||
} | ||
printlnif(c, out.Message, false) | ||
return nil | ||
}), | ||
}, | ||
{ | ||
Name: "list", | ||
Usage: "List all saved views.", | ||
Flags: []cli.Flag{ | ||
orgFlag(), | ||
formatFlag(), | ||
}, | ||
Action: cliAction(func(c *cli.Context, ac appcontext.AppContext, ctx context.Context) error { | ||
input := client.SavedViewListInput{ | ||
OrgIdentifier: getOrgFlag(c, ac.Prefs), | ||
} | ||
out, err := client.SavedViewList(ctx, ac.Auth, input) | ||
if err != nil { | ||
return err | ||
} | ||
printlnif(c, out.Message(), true) | ||
return getFormatFlag(c).WriteCollection(c.App.Writer, out) | ||
}), | ||
}, | ||
{ | ||
Name: "delete", | ||
Usage: "Delete the saved view.", | ||
Flags: []cli.Flag{savedViewNameFlag()}, | ||
Action: cliAction(func(c *cli.Context, ac appcontext.AppContext, ctx context.Context) error { | ||
out, err := client.SavedViewDelete(ctx, ac.Auth, client.SavedViewDeleteInput{ | ||
OrgIdentifier: getOrgFlag(c, ac.Prefs), | ||
Name: getSavedViewNameArgOrFlag(c), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
printlnif(c, out.Message, false) | ||
return nil | ||
}), | ||
}, | ||
}, | ||
} | ||
|
||
func savedViewNameFlag() *cli.StringFlag { | ||
return &cli.StringFlag{ | ||
Name: "name", | ||
Aliases: s1("n"), | ||
Usage: usage("Name of the view. Run `webhookdb view list` to see a list of all your saved views."), | ||
} | ||
} | ||
|
||
func getSavedViewNameArgOrFlag(c *cli.Context) string { | ||
return requireFlagOrArg(c, "name", "Use `webhookdb view list` to see available saved views.") | ||
} |