1
0
Fork 0
mirror of https://github.com/Luzifer/share.git synced 2024-12-20 18:41:17 +00:00
share/main.go

94 lines
2.4 KiB
Go
Raw Normal View History

2017-12-02 14:25:54 +00:00
package main
//go:generate make pack
2017-12-02 14:25:54 +00:00
import (
"bytes"
"errors"
2017-12-02 14:25:54 +00:00
"fmt"
"os"
"github.com/Luzifer/rconfig"
log "github.com/sirupsen/logrus"
)
var (
cfg = struct {
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"`
Bootstrap bool `flag:"bootstrap" default:"false" description:"Upload frontend files into bucket"`
Bucket string `flag:"bucket" env:"BUCKET" default:"" description:"S3 bucket to upload files to" validate:"nonzero"`
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() {
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() {
switch {
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
case cfg.Listen != "":
if err := doListen(); err != nil {
log.WithError(err).Fatal("HTTP server ended unclean")
}
2017-12-02 15:34:21 +00:00
default:
if err := doCLIUpload(); err != nil {
log.WithError(err).Fatal("Upload failed")
}
}
}
func doCLIUpload() error {
if len(rconfig.Args()) == 1 {
return errors.New("Missing argument: File to upload")
2017-12-02 14:25:54 +00:00
}
if cfg.BaseURL == "" {
log.Warn("No BaseURL configured, output will be no complete URL")
2017-12-02 14:25:54 +00:00
}
inFileName := rconfig.Args()[1]
inFileHandle, err := os.Open(inFileName)
if err != nil {
return fmt.Errorf("Unable to open source file: %s", err)
2017-12-02 14:25:54 +00:00
}
defer inFileHandle.Close()
2017-12-02 14:25:54 +00:00
url, err := executeUpload(inFileName, inFileHandle, true)
2017-12-02 14:25:54 +00:00
if err != nil {
return fmt.Errorf("Unable to upload file: %s", err)
2017-12-02 14:25:54 +00:00
}
fmt.Println(url)
return nil
2017-12-02 14:25:54 +00:00
}
func doBootstrap() error {
for _, asset := range []string{"index.html", "app.js"} {
if _, err := executeUpload(asset, bytes.NewReader(MustAsset("frontend/"+asset)), false); err != nil {
return fmt.Errorf("Unable to upload bootstrap asset %q: %s", asset, err)
}
2017-12-02 14:25:54 +00:00
}
return nil
2017-12-02 14:25:54 +00:00
}