From 4aac69d4250773d9374287988e1b24a0ed43503a Mon Sep 17 00:00:00 2001
From: Thomas von Dein
Your upload is available at '
- +response.uploads[0].url+' for download
Your upload is available for download.'); $('#UploadForm').hide(); }else{ $('.statusMsg').html('
'+response.message+'
'); diff --git a/upctl/Makefile b/upctl/Makefile index 636da5d..f5316a0 100644 --- a/upctl/Makefile +++ b/upctl/Makefile @@ -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)'" diff --git a/upctl/cmd/formcommands.go b/upctl/cmd/formcommands.go index a140cfd..3c18847 100644 --- a/upctl/cmd/formcommands.go +++ b/upctl/cmd/formcommands.go @@ -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 +} diff --git a/upctl/cmd/maincommands.go b/upctl/cmd/maincommands.go index 180bc01..c0a2ed6 100644 --- a/upctl/cmd/maincommands.go +++ b/upctl/cmd/maincommands.go @@ -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) }, } diff --git a/upctl/lib/client.go b/upctl/lib/client.go index 93483f8..f9ab95e 100644 --- a/upctl/lib/client.go +++ b/upctl/lib/client.go @@ -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) diff --git a/upctl/lib/client_test.go b/upctl/lib/client_test.go index 15de2ed..cd743c1 100644 --- a/upctl/lib/client_test.go +++ b/upctl/lib/client_test.go @@ -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)) } } diff --git a/upctl/lib/output.go b/upctl/lib/output.go index 8f01620..ef250c0 100644 --- a/upctl/lib/output.go +++ b/upctl/lib/output.go @@ -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 } diff --git a/t/t1 b/upctl/t/t1 similarity index 100% rename from t/t1 rename to upctl/t/t1 diff --git a/upctl/upctl.hcl b/upctl/upctl.hcl index fc989e6..4031105 100644 --- a/upctl/upctl.hcl +++ b/upctl/upctl.hcl @@ -1,2 +1,3 @@ endpoint = "http://localhost:8080/v1" apikey = "970b391f22f515d96b3e9b86a2c62c627968828e47b356994d2e583188b4190a" +notify = "root@localhost"