2017-12-02 14:25:54 +00:00
|
|
|
package main
|
|
|
|
|
2017-12-02 18:37:29 +00:00
|
|
|
//go:generate make pack
|
|
|
|
|
2017-12-02 14:25:54 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
2017-12-29 22:35:57 +00:00
|
|
|
"errors"
|
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"
|
|
|
|
|
|
|
|
"github.com/Luzifer/rconfig"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfg = struct {
|
2017-12-29 22:48:36 +00:00
|
|
|
BaseURL string `flag:"base-url" env:"BASE_URL" default:"" description:"URL to prepend before filename"`
|
|
|
|
BasePath string `flag:"base-path" env:"BASE_PATH" default:"file/{{ printf \"%.2s\" .Hash }}/{{.Hash}}" description:"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"`
|
2017-12-29 22:48:36 +00:00
|
|
|
Bucket string `flag:"bucket" env:"BUCKET" default:"" description:"S3 bucket to upload files to" validate:"nonzero"`
|
2018-09-19 09:55:25 +00:00
|
|
|
ContentType string `flag:"content-type,c" default:"" description:"Force content-type to be set to this value"`
|
2017-12-29 22:48:36 +00:00
|
|
|
Listen string `flag:"listen" env:"LISTEN" default:"" description:"Enable HTTP server if set to IP/Port (e.g. ':3000')"`
|
2017-12-02 14:25:54 +00:00
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
version = "dev"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-09-19 09:55:25 +00:00
|
|
|
rconfig.AutoEnv(true)
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
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 {
|
|
|
|
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 == "-" {
|
|
|
|
inFileName = "stdin"
|
|
|
|
if cfg.ContentType == "" {
|
|
|
|
// If we don't have an explicitly set content-type assume stdin contains text
|
|
|
|
cfg.ContentType = "text/plain"
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return fmt.Errorf("Unable to open source file: %s", err)
|
|
|
|
}
|
|
|
|
defer inFileHandle.Close()
|
|
|
|
inFile = inFileHandle
|
2017-12-02 14:25:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 09:55:25 +00:00
|
|
|
url, err := executeUpload(inFileName, inFile, true, cfg.ContentType)
|
2017-12-02 14:25:54 +00:00
|
|
|
if err != nil {
|
2017-12-29 22:35:57 +00:00
|
|
|
return fmt.Errorf("Unable to upload file: %s", err)
|
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 {
|
|
|
|
for _, asset := range []string{"index.html", "app.js"} {
|
2018-09-19 09:55:25 +00:00
|
|
|
if _, err := executeUpload(asset, bytes.NewReader(MustAsset("frontend/"+asset)), false, ""); err != nil {
|
2017-12-29 22:35:57 +00:00
|
|
|
return fmt.Errorf("Unable to upload bootstrap asset %q: %s", asset, err)
|
|
|
|
}
|
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
|
|
|
}
|