- added form list
- added sort by time
- fixed hanging channel in zip writer
- removed download url from form template
- added upload description
- added linter to Makefiles
- added missing test file
This commit is contained in:
2023-03-29 19:04:01 +02:00
parent c6f2b3f57c
commit 4aac69d425
16 changed files with 142 additions and 30 deletions

View File

@@ -28,9 +28,14 @@ COMMIT = $(shell git rev-parse --short=8 HEAD)
BUILD = $(shell date +%Y.%m.%d.%H%M%S)
VERSION := $(if $(filter $(BRANCH), development),$(version)-$(BRANCH)-$(COMMIT)-$(BUILD),$(version))
HAVE_POD := $(shell pod2text -h 2>/dev/null)
HAVE_LINT:= $(shell golangci-lint -h 2>/dev/null)
all: buildlocal
all: lint buildlocal
lint:
ifdef HAVE_LINT
golangci-lint run
endif
buildlocal:
go build -ldflags "-X 'github.com/tlinden/ephemerup/upctl/cfg.VERSION=$(VERSION)'"

View File

@@ -19,6 +19,7 @@ package cmd
import (
//"errors"
"github.com/spf13/cobra"
"github.com/tlinden/ephemerup/common"
"github.com/tlinden/ephemerup/upctl/cfg"
"github.com/tlinden/ephemerup/upctl/lib"
"os"
@@ -43,6 +44,7 @@ func FormCommand(conf *cfg.Config) *cobra.Command {
formCmd.Aliases = append(formCmd.Aliases, "f")
formCmd.AddCommand(FormCreateCommand(conf))
formCmd.AddCommand(FormListCommand(conf))
return formCmd
}
@@ -70,3 +72,25 @@ func FormCreateCommand(conf *cfg.Config) *cobra.Command {
return formCreateCmd
}
func FormListCommand(conf *cfg.Config) *cobra.Command {
var listCmd = &cobra.Command{
Use: "list [options]",
Short: "List formss",
Long: `List formss.`,
RunE: func(cmd *cobra.Command, args []string) error {
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
return lib.List(os.Stdout, conf, nil, common.TypeForm)
},
}
// options
listCmd.PersistentFlags().StringVarP(&conf.Apicontext, "apicontext", "", "", "Filter by given API context")
listCmd.Aliases = append(listCmd.Aliases, "ls")
listCmd.Aliases = append(listCmd.Aliases, "l")
return listCmd
}

View File

@@ -19,6 +19,7 @@ package cmd
import (
"errors"
"github.com/spf13/cobra"
"github.com/tlinden/ephemerup/common"
"github.com/tlinden/ephemerup/upctl/cfg"
"github.com/tlinden/ephemerup/upctl/lib"
"os"
@@ -43,6 +44,7 @@ func UploadCommand(conf *cfg.Config) *cobra.Command {
// options
uploadCmd.PersistentFlags().StringVarP(&conf.Expire, "expire", "e", "", "Expire setting: asap or duration (accepted shortcuts: dmh)")
uploadCmd.PersistentFlags().StringVarP(&conf.Description, "description", "D", "", "Description of the form")
uploadCmd.Aliases = append(uploadCmd.Aliases, "up")
uploadCmd.Aliases = append(uploadCmd.Aliases, "u")
@@ -59,7 +61,7 @@ func ListCommand(conf *cfg.Config) *cobra.Command {
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
return lib.List(os.Stdout, conf, args)
return lib.List(os.Stdout, conf, args, common.TypeUpload)
},
}

View File

@@ -190,7 +190,8 @@ func UploadFiles(w io.Writer, c *cfg.Config, args []string) error {
// actual post w/ settings
resp, err := rq.R.
SetFormData(map[string]string{
"expire": c.Expire,
"expire": c.Expire,
"description": c.Description,
}).
Post(rq.Url)
@@ -205,8 +206,15 @@ func UploadFiles(w io.Writer, c *cfg.Config, args []string) error {
return RespondExtended(w, resp)
}
func List(w io.Writer, c *cfg.Config, args []string) error {
rq := Setup(c, "/uploads")
func List(w io.Writer, c *cfg.Config, args []string, typ int) error {
var rq *Request
switch typ {
case common.TypeUpload:
rq = Setup(c, "/uploads")
case common.TypeForm:
rq = Setup(c, "/forms")
}
params := &ListParams{Apicontext: c.Apicontext}
resp, err := rq.R.
@@ -221,7 +229,14 @@ func List(w io.Writer, c *cfg.Config, args []string) error {
return err
}
return UploadsRespondTable(w, resp)
switch typ {
case common.TypeUpload:
return UploadsRespondTable(w, resp)
case common.TypeForm:
return FormsRespondTable(w, resp)
}
return nil
}
func Delete(w io.Writer, c *cfg.Config, args []string) error {
@@ -332,10 +347,10 @@ func CreateForm(w io.Writer, c *cfg.Config) error {
// actual post w/ settings
resp, err := rq.R.
SetFormData(map[string]string{
"expire": c.Expire,
"description": c.Description,
"notify": c.Notify,
SetBody(&common.Form{
Expire: c.Expire,
Description: c.Description,
Notify: c.Notify,
}).
Post(rq.Url)

View File

@@ -22,6 +22,7 @@ import (
"bytes"
"fmt"
"github.com/jarcoal/httpmock"
"github.com/tlinden/ephemerup/common"
"github.com/tlinden/ephemerup/upctl/cfg"
"io/ioutil"
"net/http"
@@ -232,7 +233,7 @@ func TestList(t *testing.T) {
for _, unit := range tests {
var w bytes.Buffer
Intercept(unit)
Check(t, unit, &w, List(&w, conf, []string{}))
Check(t, unit, &w, List(&w, conf, []string{}, common.TypeUpload))
}
}

View File

@@ -21,10 +21,12 @@ import (
"encoding/json"
"errors"
"fmt"
//"github.com/alecthomas/repr"
"github.com/imroc/req/v3"
"github.com/olekukonko/tablewriter"
"github.com/tlinden/ephemerup/common"
"io"
"sort"
"strings"
"time"
)
@@ -77,7 +79,8 @@ func WriteExtended(w io.Writer, response *common.Response) {
// we shall only have 1 element, however, if we ever support more, here we go
for _, entry := range response.Uploads {
expire := prepareExpire(entry.Expire, entry.Created)
fmt.Fprintf(w, format, "Id", entry.Id)
fmt.Fprintf(w, format, "Upload-Id", entry.Id)
fmt.Fprintf(w, format, "Description", entry.Id)
fmt.Fprintf(w, format, "Expire", expire)
fmt.Fprintf(w, format, "Context", entry.Context)
fmt.Fprintf(w, format, "Created", entry.Created)
@@ -88,18 +91,18 @@ func WriteExtended(w io.Writer, response *common.Response) {
for _, entry := range response.Forms {
expire := prepareExpire(entry.Expire, entry.Created)
fmt.Fprintf(w, format, "Id", entry.Id)
fmt.Fprintf(w, format, "Form-Id", entry.Id)
fmt.Fprintf(w, format, "Description", entry.Description)
fmt.Fprintf(w, format, "Expire", expire)
fmt.Fprintf(w, format, "Context", entry.Context)
fmt.Fprintf(w, format, "Created", entry.Created)
fmt.Fprintf(w, format, "Description", entry.Description)
fmt.Fprintf(w, format, "Notify", entry.Notify)
fmt.Fprintf(w, format, "Url", entry.Url)
fmt.Fprintln(w)
}
}
// extract an common.Uploads{} struct from json response
// extract an common.Response{} struct from json response
func GetResponse(resp *req.Response) (*common.Response, error) {
response := common.Response{}
@@ -125,15 +128,49 @@ func UploadsRespondTable(w io.Writer, resp *req.Response) error {
fmt.Fprintln(w, response.Message)
}
sort.SliceStable(response.Uploads, func(i, j int) bool {
return response.Uploads[i].Created.Time.Unix() < response.Uploads[j].Created.Time.Unix()
})
// tablewriter
data := [][]string{}
for _, entry := range response.Uploads {
data = append(data, []string{
entry.Id, entry.Expire, entry.Context, entry.Created.Format("2006-01-02 15:04:05"),
entry.Id, entry.Description, entry.Expire, entry.Context,
entry.Created.Format("2006-01-02 15:04:05"), entry.File,
})
}
WriteTable(w, []string{"ID", "EXPIRE", "CONTEXT", "CREATED"}, data)
WriteTable(w, []string{"UPLOAD-ID", "DESCRIPTION", "EXPIRE", "CONTEXT", "CREATED", "FILE"}, data)
return nil
}
// turn the Forms{} struct into a table and print it
func FormsRespondTable(w io.Writer, resp *req.Response) error {
response, err := GetResponse(resp)
if err != nil {
return err
}
if response.Message != "" {
fmt.Fprintln(w, response.Message)
}
sort.SliceStable(response.Forms, func(i, j int) bool {
return response.Forms[i].Created.Time.Unix() < response.Forms[j].Created.Time.Unix()
})
// tablewriter
data := [][]string{}
for _, entry := range response.Forms {
data = append(data, []string{
entry.Id, entry.Description, entry.Expire, entry.Context,
entry.Created.Format("2006-01-02 15:04:05"), entry.Notify,
})
}
WriteTable(w, []string{"FORM-ID", "DESCRIPTION", "EXPIRE", "CONTEXT", "CREATED", "NOTIFY"}, data)
return nil
}

1
upctl/t/t1 Normal file
View File

@@ -0,0 +1 @@
Wed Mar 29 03:01:21 PM CEST 2023

View File

@@ -1,2 +1,3 @@
endpoint = "http://localhost:8080/v1"
apikey = "970b391f22f515d96b3e9b86a2c62c627968828e47b356994d2e583188b4190a"
notify = "root@localhost"