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

155 lines
4.4 KiB
Go
Raw Normal View History

2017-12-02 14:25:54 +00:00
package main
import (
"bytes"
"embed"
2017-12-02 14:25:54 +00:00
"fmt"
"io"
2017-12-02 14:25:54 +00:00
"os"
"strings"
2017-12-02 14:25:54 +00:00
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/Luzifer/rconfig/v2"
2017-12-02 14:25:54 +00:00
)
var (
cfg = struct {
BaseURL string `flag:"base-url" default:"" description:"URL to prepend before filename"`
Bootstrap bool `flag:"bootstrap" default:"false" description:"Upload frontend files into bucket"`
Bucket string `flag:"bucket" default:"" description:"S3 bucket to upload files to" validate:"nonzero"`
ContentType string `flag:"content-type,c" default:"" description:"Force content-type to be set to this value"`
Endpoint string `flag:"endpoint" default:"" description:"Override AWS S3 endpoint (i.e. to use MinIO)"`
FileTemplate string `flag:"file-template" vardefault:"file_template" description:"Full name template of the uploaded file"`
Listen string `flag:"listen" default:"" description:"Enable HTTP server if set to IP/Port (e.g. ':3000')"`
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
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"`
}{}
//go:embed frontend/*
frontend embed.FS
2017-12-02 14:25:54 +00:00
version = "dev"
)
func initApp() (err error) {
rconfig.AutoEnv(true)
rconfig.SetVariableDefaults(map[string]string{
"file_template": `file/{{ printf "%.6s" .Hash }}/{{ .SafeFileName }}`,
})
if err = rconfig.ParseAndValidate(&cfg); err != nil {
return fmt.Errorf("parsing CLI options: %w", err)
2017-12-02 14:25:54 +00:00
}
l, err := logrus.ParseLevel(cfg.LogLevel)
if err != nil {
return fmt.Errorf("parsing log-level: %w", err)
}
logrus.SetLevel(l)
return nil
2017-12-02 14:25:54 +00:00
}
func main() {
var err error
if err = initApp(); err != nil {
logrus.WithError(err).Fatal("initializing app")
}
if cfg.VersionAndExit {
fmt.Printf("share %s\n", version) //nolint:forbidigo // Fine for version info
os.Exit(0)
}
switch {
case cfg.Bootstrap:
if err := doBootstrap(); err != nil {
logrus.WithError(err).Fatal("bootstrapping resources")
}
logrus.Info("Bucket bootstrap finished: Frontend uploaded")
2017-12-02 14:25:54 +00:00
case cfg.Listen != "":
logrus.WithFields(logrus.Fields{
"addr": cfg.Listen,
"version": version,
}).Info("share HTTP server started")
if err := doListen(); err != nil {
logrus.WithError(err).Fatal("running HTTP server")
}
2017-12-02 15:34:21 +00:00
default:
if err := doCLIUpload(); err != nil {
logrus.WithError(err).Fatal("uploading file")
}
}
}
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 == "" {
logrus.Warn("No BaseURL configured, output will be no complete URL")
2017-12-02 14:25:54 +00:00
}
var inFile io.ReadSeeker
inFileName := rconfig.Args()[1]
if inFileName == "-" {
if cfg.ContentType == "" {
// If we don't have an explicitly set content-type assume stdin contains text
inFileName = "stdin"
cfg.ContentType = "text/plain"
} else if ext, err := mimeResolver.ExtensionsByType(cfg.ContentType); err == nil {
inFileName = strings.Join([]string{"stdin", ext}, "")
}
// Stdin is not seekable, so we need to buffer it
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, os.Stdin); err != nil {
logrus.WithError(err).Fatal("reading stdin")
}
inFile = bytes.NewReader(buf.Bytes())
} else {
inFileHandle, err := os.Open(inFileName) //#nosec:G304 // Inentional read of arbitrary file
if err != nil {
return errors.Wrap(err, "opening source file")
}
defer inFileHandle.Close() //nolint:errcheck // Irrelevant, file is closed by process exit
inFile = inFileHandle
2017-12-02 14:25:54 +00:00
}
url, err := executeUpload(inFileName, inFile, true, cfg.ContentType, false)
2017-12-02 14:25:54 +00:00
if err != nil {
return errors.Wrap(err, "uploading file")
2017-12-02 14:25:54 +00:00
}
fmt.Println(url) //nolint:forbidigo // Intended as programmatic payload
return nil
2017-12-02 14:25:54 +00:00
}
func doBootstrap() error {
files, err := frontend.ReadDir("frontend")
if err != nil {
return fmt.Errorf("listing embedded files: %w", err)
}
for _, asset := range files {
content, err := frontend.ReadFile(strings.Join([]string{"frontend", asset.Name()}, "/"))
if err != nil {
return errors.Wrap(err, "reading baked asset")
}
if _, err := executeUpload(asset.Name(), bytes.NewReader(content), false, "", true); err != nil {
return errors.Wrapf(err, "uploading bootstrap asset %q", asset)
}
2017-12-02 14:25:54 +00:00
}
return nil
2017-12-02 14:25:54 +00:00
}