mirror of
https://codeberg.org/scip/ephemerup.git
synced 2025-12-18 04:51:06 +01:00
put shared code into own mod (common), + apicontext env vars
This commit is contained in:
@@ -4,13 +4,15 @@ go 1.18
|
||||
|
||||
require (
|
||||
github.com/imroc/req/v3 v3.32.0
|
||||
github.com/olekukonko/tablewriter v0.0.5
|
||||
github.com/schollz/progressbar/v3 v3.13.1
|
||||
github.com/spf13/cobra v1.6.1
|
||||
github.com/spf13/pflag v1.0.5
|
||||
github.com/spf13/viper v1.15.0
|
||||
github.com/tlinden/cenophane/common v0.0.0-00010101000000-000000000000
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/alecthomas/repr v0.2.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
||||
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
|
||||
github.com/golang/mock v1.6.0 // indirect
|
||||
@@ -23,7 +25,6 @@ require (
|
||||
github.com/mattn/go-runewidth v0.0.14 // indirect
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||
github.com/onsi/ginkgo/v2 v2.2.0 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
|
||||
github.com/quic-go/qpack v0.4.0 // indirect
|
||||
@@ -32,7 +33,6 @@ require (
|
||||
github.com/quic-go/qtls-go1-20 v0.1.0 // indirect
|
||||
github.com/quic-go/quic-go v0.32.0 // indirect
|
||||
github.com/rivo/uniseg v0.2.0 // indirect
|
||||
github.com/schollz/progressbar/v3 v3.13.1 // indirect
|
||||
github.com/spf13/afero v1.9.3 // indirect
|
||||
github.com/spf13/cast v1.5.0 // indirect
|
||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||
@@ -48,3 +48,5 @@ require (
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
replace github.com/tlinden/cenophane/common => ../common
|
||||
|
||||
@@ -38,8 +38,6 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
|
||||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
|
||||
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||
@@ -154,7 +152,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
|
||||
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
@@ -367,8 +364,6 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
|
||||
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
//"github.com/alecthomas/repr"
|
||||
"github.com/imroc/req/v3"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
"github.com/tlinden/cenophane/common"
|
||||
"github.com/tlinden/up/upctl/cfg"
|
||||
"mime"
|
||||
"os"
|
||||
@@ -47,23 +48,6 @@ type ListParams struct {
|
||||
Apicontext string `json:"apicontext"`
|
||||
}
|
||||
|
||||
type Upload struct {
|
||||
Id string `json:"id"`
|
||||
Expire string `json:"expire"`
|
||||
File string `json:"file"` // final filename (visible to the downloader)
|
||||
Members []string `json:"members"` // contains multiple files, so File is an archive
|
||||
Uploaded Timestamp `json:"uploaded"`
|
||||
Context string `json:"context"`
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
type Uploads struct {
|
||||
Entries []*Upload `json:"uploads"`
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
Code int `json:"code"`
|
||||
}
|
||||
|
||||
const Maxwidth = 10
|
||||
|
||||
func Setup(c *cfg.Config, path string) *Request {
|
||||
@@ -279,7 +263,7 @@ func Download(c *cfg.Config, args []string) error {
|
||||
return fmt.Errorf("No filename provided!")
|
||||
}
|
||||
|
||||
cleanfilename, _ := Untaint(filename, regexp.MustCompile(`[^a-zA-Z0-9\-\._]`))
|
||||
cleanfilename, _ := common.Untaint(filename, regexp.MustCompile(`[^a-zA-Z0-9\-\._]`))
|
||||
|
||||
if err := os.Rename(id, cleanfilename); err != nil {
|
||||
os.Remove(id)
|
||||
@@ -290,26 +274,3 @@ func Download(c *cfg.Config, args []string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
Untaint user input, that is: remove all non supported chars.
|
||||
|
||||
wanted is a regexp matching chars we shall leave. Everything else
|
||||
will be removed. Eg:
|
||||
|
||||
untainted := Untaint(input, `[^a-zA-Z0-9\-]`)
|
||||
|
||||
Returns a new string and an error if the input string has been
|
||||
modified. It's the callers choice to decide what to do about
|
||||
it. You may ignore the error and use the untainted string or bail
|
||||
out.
|
||||
*/
|
||||
func Untaint(input string, wanted *regexp.Regexp) (string, error) {
|
||||
untainted := wanted.ReplaceAllString(input, "")
|
||||
|
||||
if len(untainted) != len(input) {
|
||||
return untainted, errors.New("Invalid input string!")
|
||||
}
|
||||
|
||||
return untainted, nil
|
||||
}
|
||||
|
||||
@@ -23,17 +23,18 @@ import (
|
||||
"fmt"
|
||||
"github.com/imroc/req/v3"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/tlinden/cenophane/common"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// make a human readable version of the expire setting
|
||||
func prepareExpire(expire string, start Timestamp) string {
|
||||
func prepareExpire(expire string, start common.Timestamp) string {
|
||||
switch expire {
|
||||
case "asap":
|
||||
return "On first access"
|
||||
default:
|
||||
return time.Unix(start.Unix()+int64(duration2int(expire)), 0).Format("2006-01-02 15:04:05")
|
||||
return time.Unix(start.Unix()+int64(common.Duration2int(expire)), 0).Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
return ""
|
||||
@@ -62,7 +63,7 @@ func WriteTable(headers []string, data [][]string) {
|
||||
}
|
||||
|
||||
// output like psql \x
|
||||
func WriteExtended(uploads *Uploads) {
|
||||
func WriteExtended(uploads *common.Uploads) {
|
||||
format := fmt.Sprintf("%%%ds: %%s\n", Maxwidth)
|
||||
|
||||
// we shall only have 1 element, however, if we ever support more, here we go
|
||||
@@ -78,9 +79,9 @@ func WriteExtended(uploads *Uploads) {
|
||||
}
|
||||
}
|
||||
|
||||
// extract an Uploads{} struct from json response
|
||||
func GetUploadsFromResponse(resp *req.Response) (*Uploads, error) {
|
||||
uploads := Uploads{}
|
||||
// extract an common.Uploads{} struct from json response
|
||||
func GetUploadsFromResponse(resp *req.Response) (*common.Uploads, error) {
|
||||
uploads := common.Uploads{}
|
||||
|
||||
if err := json.Unmarshal([]byte(resp.String()), &uploads); err != nil {
|
||||
return nil, errors.New("Could not unmarshall JSON response: " + err.Error())
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
/*
|
||||
Copyright © 2023 Thomas von Dein
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package lib
|
||||
|
||||
// FIXME: import from upd!!!!
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// https://gist.github.com/rhcarvalho/9338c3ff8850897c68bc74797c5dc25b
|
||||
|
||||
// Timestamp is like time.Time, but knows how to unmarshal from JSON
|
||||
// Unix timestamp numbers or RFC3339 strings, and marshal back into
|
||||
// the same JSON representation.
|
||||
type Timestamp struct {
|
||||
time.Time
|
||||
rfc3339 bool
|
||||
}
|
||||
|
||||
func (t Timestamp) MarshalJSON() ([]byte, error) {
|
||||
if t.rfc3339 {
|
||||
return t.Time.MarshalJSON()
|
||||
}
|
||||
return t.formatUnix()
|
||||
}
|
||||
|
||||
func (t *Timestamp) UnmarshalJSON(data []byte) error {
|
||||
err := t.Time.UnmarshalJSON(data)
|
||||
if err != nil {
|
||||
return t.parseUnix(data)
|
||||
}
|
||||
t.rfc3339 = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Timestamp) formatUnix() ([]byte, error) {
|
||||
sec := float64(t.Time.UnixNano()) * float64(time.Nanosecond) / float64(time.Second)
|
||||
return strconv.AppendFloat(nil, sec, 'f', -1, 64), nil
|
||||
}
|
||||
|
||||
func (t *Timestamp) parseUnix(data []byte) error {
|
||||
f, err := strconv.ParseFloat(string(data), 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t.Time = time.Unix(0, int64(f*float64(time.Second/time.Nanosecond)))
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
We could use time.ParseDuration(), but this doesn't support days.
|
||||
|
||||
We could also use github.com/xhit/go-str2duration/v2, which does
|
||||
the job, but it's just another dependency, just for this little
|
||||
gem. And we don't need a time.Time value.
|
||||
|
||||
Convert a duration into seconds (int).
|
||||
Valid time units are "s", "m", "h" and "d".
|
||||
*/
|
||||
func duration2int(duration string) int {
|
||||
re := regexp.MustCompile(`(\d+)([dhms])`)
|
||||
seconds := 0
|
||||
|
||||
for _, match := range re.FindAllStringSubmatch(duration, -1) {
|
||||
if len(match) == 3 {
|
||||
v, _ := strconv.Atoi(match[1])
|
||||
switch match[2][0] {
|
||||
case 'd':
|
||||
seconds += v * 86400
|
||||
case 'h':
|
||||
seconds += v * 3600
|
||||
case 'm':
|
||||
seconds += v * 60
|
||||
case 's':
|
||||
seconds += v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return seconds
|
||||
}
|
||||
Reference in New Issue
Block a user