- had to add a Type field to interface DbEntry so that db.List()
is able to distinguish between Upload and Form properly.
- added form describe and delete commands
- added --query parameter to form+upload list for filtering
This commit is contained in:
2023-03-30 10:22:57 +02:00
parent 26f2b25e22
commit 8a791d8017
13 changed files with 208 additions and 40 deletions

View File

@@ -44,6 +44,9 @@ type Config struct {
// required to intercept requests using httpmock in tests
Mock bool
// used to filter lists
Query string
// required for forms
Description string
Notify string

View File

@@ -18,6 +18,7 @@ package cmd
import (
//"errors"
"errors"
"github.com/spf13/cobra"
"github.com/tlinden/ephemerup/common"
"github.com/tlinden/ephemerup/upctl/cfg"
@@ -45,6 +46,8 @@ func FormCommand(conf *cfg.Config) *cobra.Command {
formCmd.AddCommand(FormCreateCommand(conf))
formCmd.AddCommand(FormListCommand(conf))
formCmd.AddCommand(FormDeleteCommand(conf))
formCmd.AddCommand(FormDescribeCommand(conf))
return formCmd
}
@@ -88,9 +91,57 @@ func FormListCommand(conf *cfg.Config) *cobra.Command {
// options
listCmd.PersistentFlags().StringVarP(&conf.Apicontext, "apicontext", "", "", "Filter by given API context")
listCmd.PersistentFlags().StringVarP(&conf.Query, "query", "q", "", "Filter by given query regexp")
listCmd.Aliases = append(listCmd.Aliases, "ls")
listCmd.Aliases = append(listCmd.Aliases, "l")
return listCmd
}
func FormDeleteCommand(conf *cfg.Config) *cobra.Command {
var deleteCmd = &cobra.Command{
Use: "delete [options] <id>",
Short: "Delete an form",
Long: `Delete an form identified by its id`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("No id specified to delete!")
}
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
return lib.Delete(os.Stdout, conf, args, common.TypeForm)
},
}
deleteCmd.Aliases = append(deleteCmd.Aliases, "rm")
deleteCmd.Aliases = append(deleteCmd.Aliases, "d")
return deleteCmd
}
func FormDescribeCommand(conf *cfg.Config) *cobra.Command {
var listCmd = &cobra.Command{
Use: "describe [options] form-id",
Long: "Show detailed informations about an form object.",
Short: `Describe an form.`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("No id specified to delete!")
}
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
return lib.Describe(os.Stdout, conf, args, common.TypeForm)
},
}
listCmd.Aliases = append(listCmd.Aliases, "des")
listCmd.Aliases = append(listCmd.Aliases, "info")
listCmd.Aliases = append(listCmd.Aliases, "i")
return listCmd
}

View File

@@ -56,7 +56,7 @@ func ListCommand(conf *cfg.Config) *cobra.Command {
var listCmd = &cobra.Command{
Use: "list [options] [file ..]",
Short: "List uploads",
Long: `List uploads.`,
Long: `List uploads`,
RunE: func(cmd *cobra.Command, args []string) error {
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
@@ -67,6 +67,7 @@ func ListCommand(conf *cfg.Config) *cobra.Command {
// options
listCmd.PersistentFlags().StringVarP(&conf.Apicontext, "apicontext", "", "", "Filter by given API context")
listCmd.PersistentFlags().StringVarP(&conf.Query, "query", "q", "", "Filter by given query regexp")
listCmd.Aliases = append(listCmd.Aliases, "ls")
listCmd.Aliases = append(listCmd.Aliases, "l")
@@ -87,7 +88,7 @@ func DeleteCommand(conf *cfg.Config) *cobra.Command {
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
return lib.Delete(os.Stdout, conf, args)
return lib.Delete(os.Stdout, conf, args, common.TypeUpload)
},
}
@@ -101,7 +102,7 @@ func DescribeCommand(conf *cfg.Config) *cobra.Command {
var listCmd = &cobra.Command{
Use: "describe [options] upload-id",
Long: "Show detailed informations about an upload object.",
Short: `Describe an upload.`,
Short: `Describe an upload`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("No id specified to delete!")
@@ -110,7 +111,7 @@ func DescribeCommand(conf *cfg.Config) *cobra.Command {
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
return lib.Describe(os.Stdout, conf, args)
return lib.Describe(os.Stdout, conf, args, common.TypeUpload)
},
}
@@ -125,7 +126,7 @@ func DownloadCommand(conf *cfg.Config) *cobra.Command {
var listCmd = &cobra.Command{
Use: "download [options] upload-id",
Long: "Download the file associated with an upload object.",
Short: `Download a file.`,
Short: `Download a file`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("No id specified to delete!")

View File

@@ -48,6 +48,7 @@ type Request struct {
type ListParams struct {
Apicontext string `json:"apicontext"`
Query string `json:"query"`
}
const Maxwidth = 12
@@ -216,7 +217,7 @@ func List(w io.Writer, c *cfg.Config, args []string, typ int) error {
rq = Setup(c, "/forms")
}
params := &ListParams{Apicontext: c.Apicontext}
params := &ListParams{Apicontext: c.Apicontext, Query: c.Query}
resp, err := rq.R.
SetBodyJsonMarshal(params).
Get(rq.Url)
@@ -239,9 +240,18 @@ func List(w io.Writer, c *cfg.Config, args []string, typ int) error {
return nil
}
func Delete(w io.Writer, c *cfg.Config, args []string) error {
func Delete(w io.Writer, c *cfg.Config, args []string, typ int) error {
for _, id := range args {
rq := Setup(c, "/uploads/"+id+"/")
var rq *Request
caption := "Upload"
switch typ {
case common.TypeUpload:
rq = Setup(c, "/uploads/"+id)
case common.TypeForm:
rq = Setup(c, "/forms/"+id)
caption = "Form"
}
resp, err := rq.R.Delete(rq.Url)
@@ -253,20 +263,27 @@ func Delete(w io.Writer, c *cfg.Config, args []string) error {
return err
}
fmt.Fprintf(w, "Upload %s successfully deleted.\n", id)
fmt.Fprintf(w, "%s %s successfully deleted.\n", caption, id)
}
return nil
}
func Describe(w io.Writer, c *cfg.Config, args []string) error {
func Describe(w io.Writer, c *cfg.Config, args []string, typ int) error {
if len(args) == 0 {
return errors.New("No id provided!")
}
var rq *Request
id := args[0] // we describe only 1 object
rq := Setup(c, "/uploads/"+id)
switch typ {
case common.TypeUpload:
rq = Setup(c, "/uploads/"+id)
case common.TypeForm:
rq = Setup(c, "/forms/"+id)
}
resp, err := rq.R.Get(rq.Url)
if err != nil {

View File

@@ -295,7 +295,7 @@ func TestDescribe(t *testing.T) {
var w bytes.Buffer
unit.route += unit.files[0]
Intercept(unit)
Check(t, unit, &w, Describe(&w, conf, unit.files))
Check(t, unit, &w, Describe(&w, conf, unit.files, common.TypeUpload))
}
}
@@ -345,9 +345,9 @@ func TestDelete(t *testing.T) {
for _, unit := range tests {
var w bytes.Buffer
unit.route += unit.files[0] + "/"
unit.route += unit.files[0]
Intercept(unit)
Check(t, unit, &w, Delete(&w, conf, unit.files))
Check(t, unit, &w, Delete(&w, conf, unit.files, common.TypeUpload))
}
}