Skip to content

Commit

Permalink
Add support for saved views (beta feature)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgalanakis committed Feb 11, 2024
1 parent 7a525ad commit 3cdcb9b
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
36 changes: 36 additions & 0 deletions client/saved_views.go
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
}
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Documentation: <https://docs.webhookdb.com/docs/cli-reference.md>
organizationsCmd,
replayCmd,
savedQueriesCmd,
savedViewsCmd,
servicesCmd,
subscriptionsCmd,
syncCmd(dbSyncType),
Expand Down
93 changes: 93 additions & 0 deletions cmd/cmd_saved_views.go
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.")
}

0 comments on commit 3cdcb9b

Please sign in to comment.