2017-12-02 14:25:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-08-25 23:32:20 +00:00
|
|
|
"embed"
|
|
|
|
_ "embed"
|
2017-12-02 14:25:54 +00:00
|
|
|
"fmt"
|
2018-09-19 09:55:25 +00:00
|
|
|
"io"
|
2017-12-02 14:25:54 +00:00
|
|
|
"os"
|
2021-08-25 23:32:20 +00:00
|
|
|
"strings"
|
2017-12-02 14:25:54 +00:00
|
|
|
|
2021-08-25 23:32:20 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-12-02 14:25:54 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-05-12 22:05:06 +00:00
|
|
|
|
2021-01-27 20:11:32 +00:00
|
|
|
"github.com/Luzifer/rconfig/v2"
|
2017-12-02 14:25:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfg = struct {
|
2021-10-19 21:28:40 +00:00
|
|
|
BaseURL string `flag:"base-url" default:"" description:"URL to prepend before filename"`
|
|
|
|
BasePath string `flag:"base-path" default:"" description:"DEPRECATED: Path to upload the file to"`
|
2017-12-02 18:37:29 +00:00
|
|
|
Bootstrap bool `flag:"bootstrap" default:"false" description:"Upload frontend files into bucket"`
|
2021-10-19 21:28:40 +00:00
|
|
|
Bucket string `flag:"bucket" default:"" description:"S3 bucket to upload files to" validate:"nonzero"`
|
2021-10-19 21:42:56 +00:00
|
|
|
ContentType string `flag:"content-type,c" default:"" description:"Force content-type to be set to this value"`
|
|
|
|
FileTemplate string `flag:"file-template" vardefault:"file_template" description:"Full name template of the uploaded file"`
|
2021-10-19 21:28:40 +00:00
|
|
|
Listen string `flag:"listen" default:"" description:"Enable HTTP server if set to IP/Port (e.g. ':3000')"`
|
2021-10-19 21:40:42 +00:00
|
|
|
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
2018-10-11 18:15:12 +00:00
|
|
|
Progress bool `flag:"progress" default:"false" description:"Show progress bar while uploading"`
|
2017-12-02 14:25:54 +00:00
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
|
|
|
}{}
|
|
|
|
|
2021-08-25 23:32:20 +00:00
|
|
|
//go:embed frontend/*
|
|
|
|
frontend embed.FS
|
|
|
|
|
2017-12-02 14:25:54 +00:00
|
|
|
version = "dev"
|
|
|
|
)
|
|
|
|
|
2021-10-19 21:28:40 +00:00
|
|
|
func initApp() {
|
2018-09-19 09:55:25 +00:00
|
|
|
rconfig.AutoEnv(true)
|
2021-10-19 21:28:40 +00:00
|
|
|
rconfig.SetVariableDefaults(map[string]string{
|
|
|
|
"file_template": `file/{{ printf "%.6s" .Hash }}/{{ .SafeFileName }}`,
|
|
|
|
})
|
|
|
|
|
2017-12-02 14:25:54 +00:00
|
|
|
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
|
|
|
log.Fatalf("Unable to parse commandline options: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.VersionAndExit {
|
|
|
|
fmt.Printf("share %s\n", version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2021-10-19 21:28:40 +00:00
|
|
|
|
2021-10-19 21:40:42 +00:00
|
|
|
if l, err := log.ParseLevel(cfg.LogLevel); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to parse log level")
|
|
|
|
} else {
|
|
|
|
log.SetLevel(l)
|
|
|
|
}
|
|
|
|
|
2021-10-19 21:28:40 +00:00
|
|
|
if cfg.BasePath != "" {
|
|
|
|
cfg.FileTemplate = strings.Join([]string{strings.TrimRight(cfg.BasePath, "/"), `{{ .SafeFileName }}`}, "/")
|
|
|
|
log.WithField("file-template", cfg.FileTemplate).Warn("Using deprecated base-path parameter! Using update file-template...")
|
|
|
|
}
|
2017-12-02 14:25:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-10-19 21:28:40 +00:00
|
|
|
initApp()
|
|
|
|
|
2017-12-29 22:35:57 +00:00
|
|
|
switch {
|
2017-12-02 18:37:29 +00:00
|
|
|
|
2017-12-29 22:35:57 +00:00
|
|
|
case cfg.Bootstrap:
|
|
|
|
if err := doBootstrap(); err != nil {
|
|
|
|
log.WithError(err).Fatal("Bootstrap failed")
|
|
|
|
}
|
|
|
|
log.Info("Bucket bootstrap finished: Frontend uploaded")
|
2017-12-02 14:25:54 +00:00
|
|
|
|
2017-12-29 22:35:57 +00:00
|
|
|
case cfg.Listen != "":
|
|
|
|
if err := doListen(); err != nil {
|
|
|
|
log.WithError(err).Fatal("HTTP server ended unclean")
|
|
|
|
}
|
2017-12-02 15:34:21 +00:00
|
|
|
|
2017-12-29 22:35:57 +00:00
|
|
|
default:
|
|
|
|
if err := doCLIUpload(); err != nil {
|
|
|
|
log.WithError(err).Fatal("Upload failed")
|
|
|
|
}
|
2017-12-02 18:37:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-29 22:35:57 +00:00
|
|
|
func doCLIUpload() error {
|
|
|
|
if len(rconfig.Args()) == 1 {
|
2021-08-25 23:32:20 +00:00
|
|
|
return errors.New("missing argument: file to upload")
|
2017-12-02 14:25:54 +00:00
|
|
|
}
|
2017-12-02 18:37:29 +00:00
|
|
|
|
2017-12-29 22:35:57 +00:00
|
|
|
if cfg.BaseURL == "" {
|
|
|
|
log.Warn("No BaseURL configured, output will be no complete URL")
|
2017-12-02 14:25:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 09:55:25 +00:00
|
|
|
var inFile io.ReadSeeker
|
|
|
|
|
2017-12-29 22:35:57 +00:00
|
|
|
inFileName := rconfig.Args()[1]
|
2018-09-19 09:55:25 +00:00
|
|
|
|
|
|
|
if inFileName == "-" {
|
|
|
|
if cfg.ContentType == "" {
|
|
|
|
// If we don't have an explicitly set content-type assume stdin contains text
|
2021-10-19 21:28:40 +00:00
|
|
|
inFileName = "stdin"
|
2018-09-19 09:55:25 +00:00
|
|
|
cfg.ContentType = "text/plain"
|
2021-10-19 21:28:40 +00:00
|
|
|
} else if ext, err := mimeResolver.ExtensionsByType(cfg.ContentType); err == nil {
|
|
|
|
inFileName = strings.Join([]string{"stdin", ext}, "")
|
2018-09-19 09:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stdin is not seekable, so we need to buffer it
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if _, err := io.Copy(buf, os.Stdin); err != nil {
|
|
|
|
log.WithError(err).Fatal("Could not read stdin")
|
|
|
|
}
|
|
|
|
|
|
|
|
inFile = bytes.NewReader(buf.Bytes())
|
|
|
|
} else {
|
|
|
|
inFileHandle, err := os.Open(inFileName)
|
|
|
|
if err != nil {
|
2021-08-25 23:32:20 +00:00
|
|
|
return errors.Wrap(err, "opening source file")
|
2018-09-19 09:55:25 +00:00
|
|
|
}
|
|
|
|
defer inFileHandle.Close()
|
|
|
|
inFile = inFileHandle
|
2017-12-02 14:25:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-12 22:05:06 +00:00
|
|
|
url, err := executeUpload(inFileName, inFile, true, cfg.ContentType, false)
|
2017-12-02 14:25:54 +00:00
|
|
|
if err != nil {
|
2021-08-25 23:32:20 +00:00
|
|
|
return errors.Wrap(err, "uploading file")
|
2017-12-02 14:25:54 +00:00
|
|
|
}
|
2017-12-29 22:35:57 +00:00
|
|
|
|
|
|
|
fmt.Println(url)
|
|
|
|
return nil
|
2017-12-02 14:25:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 22:35:57 +00:00
|
|
|
func doBootstrap() error {
|
2021-08-26 13:29:25 +00:00
|
|
|
for _, asset := range []string{"index.html", "app.js", "bundle.css", "bundle.js"} {
|
2021-08-25 23:32:20 +00:00
|
|
|
content, err := frontend.ReadFile(strings.Join([]string{"frontend", asset}, "/"))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "reading baked asset")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := executeUpload(asset, bytes.NewReader(content), false, "", true); err != nil {
|
|
|
|
return errors.Wrapf(err, "uploading bootstrap asset %q", asset)
|
2017-12-29 22:35:57 +00:00
|
|
|
}
|
2017-12-02 14:25:54 +00:00
|
|
|
}
|
2017-12-29 22:35:57 +00:00
|
|
|
return nil
|
2017-12-02 14:25:54 +00:00
|
|
|
}
|