Skip to content

Commit

Permalink
Add srv tool
Browse files Browse the repository at this point in the history
  • Loading branch information
frioux committed Apr 4, 2019
1 parent 5a09c22 commit b5f5d26
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.mdwn
Expand Up @@ -307,6 +307,16 @@ rating:
$ sm-list | jq -r '[.Score, .Title, .URL ] | @tsv' | sort -n | tail -10
```

### `srv`

Serve a directory over http; takes a single optional parameter which is the dir
to serve, default is `.`:

```bash
$ srv ~
Serving /home/frew on [::]:21873
```

### `ssh-quote`

Prepares the arguments such that they can be pasted into an ssh command safely.
Expand Down
2 changes: 2 additions & 0 deletions cmd/leatherman/main.go
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/frioux/leatherman/internal/tool/replaceunzip"
"github.com/frioux/leatherman/internal/tool/rss"
"github.com/frioux/leatherman/internal/tool/smlist"
"github.com/frioux/leatherman/internal/tool/srv"
"github.com/frioux/leatherman/internal/tool/sshquote"
"github.com/frioux/leatherman/internal/tool/toml"
"github.com/frioux/leatherman/internal/tool/undefer"
Expand Down Expand Up @@ -73,6 +74,7 @@ func main() {
"rss": rss.Run,
"ssh-quote": sshquote.Run,
"sm-list": smlist.Run,
"srv": srv.Serve,
"toml2json": toml.ToJSON,
"undefer": undefer.Run,
"uni": uni.Describe,
Expand Down
31 changes: 31 additions & 0 deletions internal/tool/srv/srv.go
@@ -0,0 +1,31 @@
package srv // import "github.com/frioux/leatherman/internal/tool/srv"

import (
"fmt"
"io"
"net"
"net/http"
"os"

"github.com/pkg/errors"
)

func Serve(args []string, _ io.Reader) error {
dir := "."
if len(args) > 1 {
dir = args[1]
}

return serve(dir, os.Stderr)
}

func serve(dir string, log io.Writer) error {
listener, err := net.Listen("tcp", ":0")
if err != nil {
return errors.Wrap(err, "net.Listen")
}

fmt.Fprintf(log, "Serving %s on %s\n", dir, listener.Addr())

return http.Serve(listener, http.FileServer(http.Dir(dir)))
}
61 changes: 61 additions & 0 deletions internal/tool/srv/srv_test.go
@@ -0,0 +1,61 @@
package srv

import (
"bytes"
"io/ioutil"
"math/rand"
"net/http"
"os"
"regexp"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestServe(t *testing.T) {
buf := &bytes.Buffer{}
go serve(".", buf)

sock := regexp.MustCompile("^Serving . on (.+)\n")

var resp *http.Response
var err error
for i := 1; i < 11; i++ {
m := sock.FindStringSubmatch(buf.String())
if len(m) == 0 {
time.Sleep(time.Millisecond * time.Duration(rand.Intn(i)))
continue
}

resp, err = http.Get("http://" + m[1] + "/srv.go")
if err != nil {
t.Fatalf("Couldn't fetch srv.go: %s", err)
}
break
}

if resp == nil {
t.Fatalf("couldn't get response from server within timeout")
}

f, err := os.Open("./srv.go")
if err != nil {
t.Fatalf("Couldn't open ./srv.go: %s", err)
}
expected, err := ioutil.ReadAll(f)
if err != nil {
t.Fatalf("Couldn't read ./srv.go: %s", err)
}

got, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Couldn't read response: %s", err)
}

if len(expected) == 0 {
t.Fatal("Somehow got empty test case")
}

assert.Equal(t, expected, got)
}

0 comments on commit b5f5d26

Please sign in to comment.