Skip to content

Commit

Permalink
Fix #19
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelreyna committed Aug 4, 2020
1 parent b28df50 commit bbb33ea
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
8 changes: 8 additions & 0 deletions cmd/conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ type Conf struct {

NoUnixNorm bool

WaitForEOF bool

cmdFlagSet *pflag.FlagSet

sstlsLoc string
credFileLoc string

randUser bool
randPass bool

stdinBufLoc string //location of tempfile
}

func NewConf(cmd *cobra.Command) *Conf {
Expand Down Expand Up @@ -123,3 +127,7 @@ func (c *Conf) Mode() uint8 {
}
return DownloadMode
}

func (c *Conf) StdinBufferLocation() string {
return c.stdinBufLoc
}
18 changes: 17 additions & 1 deletion cmd/conf/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"github.com/raphaelreyna/oneshot/internal/file"
"github.com/raphaelreyna/oneshot/internal/handlers"
"github.com/raphaelreyna/oneshot/internal/server"
"io/ioutil"
"math/rand"
)

func (c *Conf) setuDownloadRoute(args []string, srvr *server.Server) (*server.Route, error) {
func (c *Conf) setupDownloadRoute(args []string, srvr *server.Server) (*server.Route, error) {
var filePath string
if len(args) >= 1 {
filePath = args[0]
Expand All @@ -23,6 +25,20 @@ func (c *Conf) setuDownloadRoute(args []string, srvr *server.Server) (*server.Ro
if c.ArchiveMethod != "zip" && c.ArchiveMethod != "tar.gz" {
c.ArchiveMethod = "tar.gz"
}

if filePath == "" && c.WaitForEOF {
tdir, err := ioutil.TempDir("", "oneshot")
if err != nil {
return nil, err
}

if c.FileName == "" {
c.FileName = fmt.Sprintf("%0-x", rand.Int31())
}
filePath = filepath.Join(tdir, c.FileName, c.FileExt)
c.stdinBufLoc = filePath
}

file := &file.FileReader{
Path: filePath,
Name: c.FileName,
Expand Down
5 changes: 5 additions & 0 deletions cmd/conf/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,9 @@ Setting both this flag and --upload-file is equivalent to setting the -u, --uplo
For more information see the -u, --upload flag documentation.
See also: --upload-file; -u, --upload; -L, --no-unix-eol-norm`,
)

flags.BoolVarP(&c.WaitForEOF, "wait-for-eof", "J", false, `Wait for EOF before starting HTTP(S) server if serving from stdin.
This flag does noting if not serving from stdin.
`,
)
}
2 changes: 1 addition & 1 deletion cmd/conf/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c *Conf) SetupServer(srvr *server.Server, args []string, ips []string) err
var route *server.Route
switch c.Mode() {
case DownloadMode:
route, err = c.setuDownloadRoute(args, srvr)
route, err = c.setupDownloadRoute(args, srvr)
case CGIMode:
route, err = c.setupCGIRoute(args, srvr)
case UploadMode:
Expand Down
33 changes: 33 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import (

"github.com/raphaelreyna/oneshot/internal/server"
"github.com/spf13/cobra"
"io"
"path/filepath"
"strings"
)

var version string
var date string

const stdinBufferSize = 1024

func (a *App) Run(cmd *cobra.Command, args []string) {
returnCode := 1
defer func() { os.Exit(returnCode) }()
Expand Down Expand Up @@ -65,6 +69,35 @@ func (a *App) Run(cmd *cobra.Command, args []string) {
shouldExitChan := make(chan struct{})
a.handleSignal(srvr, make(chan os.Signal), shouldExitChan)

// Should we wait until we get an EOF on stdin to start serving HTTP?
if loc := c.StdinBufferLocation(); loc != "" {
sf, err := os.Create(loc)

// Clean up on exit
defer os.RemoveAll(filepath.Dir(loc))
if err != nil {
log.Println(err)
return
}

// Read in from stdin into temp file
si := os.Stdin
b := make([]byte, stdinBufferSize)
for n, e := si.Read(b); e == nil; n, e = si.Read(b) {
sf.Write(b[0:n])
}
if err != nil && err != io.EOF {
log.Println(err)
return
}

err = sf.Close()
if err != nil {
log.Println(err)
return
}
}

// Start the HTTP(S) server
go func() {
err := srvr.Serve()
Expand Down

0 comments on commit bbb33ea

Please sign in to comment.