Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 76d88c220d | |||
|
|
40e015373f | ||
| 50c226b726 | |||
| 070ffb9dff | |||
| 5ac05bd298 | |||
| cf1edf2d31 | |||
|
|
03e8958dd7 | ||
|
|
6cef585071 |
@@ -4,7 +4,7 @@ go:
|
|||||||
- tip
|
- tip
|
||||||
- 1.9
|
- 1.9
|
||||||
script:
|
script:
|
||||||
- go get github.com/golang/lint/golint
|
- go get golang.org/x/lint/golint
|
||||||
- go get golang.org/x/tools/cmd/cover
|
- go get golang.org/x/tools/cmd/cover
|
||||||
- go get github.com/kisom/goutils/...
|
- go get github.com/kisom/goutils/...
|
||||||
- go test -cover github.com/kisom/goutils/...
|
- go test -cover github.com/kisom/goutils/...
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ Note that for packaging purposes, the goutils-pkg repo should be used: it
|
|||||||
pins the library versions to working copies and vendors all depdencies. See
|
pins the library versions to working copies and vendors all depdencies. See
|
||||||
https://github.com/kisom/goutils-pkg for more details.
|
https://github.com/kisom/goutils-pkg for more details.
|
||||||
|
|
||||||
|
The goutils-pkg repo [1] has stable versions of the command line
|
||||||
|
utilities here, along with a vendored snapshot of any dependencies.
|
||||||
|
|
||||||
Contents:
|
Contents:
|
||||||
|
|
||||||
ahash/ Provides hashes from string algorithm specifiers.
|
ahash/ Provides hashes from string algorithm specifiers.
|
||||||
@@ -62,3 +65,5 @@ Each program should have a small README in the directory with more
|
|||||||
information.
|
information.
|
||||||
|
|
||||||
All code here is licensed under the MIT license.
|
All code here is licensed under the MIT license.
|
||||||
|
|
||||||
|
[1] https://github.com/kisom/goutils-pkg/
|
||||||
|
|||||||
9
cmd/parts/README
Normal file
9
cmd/parts/README
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
parts: simple parts database for electronic components
|
||||||
|
|
||||||
|
Usage: parts [id] -- query the database for a part
|
||||||
|
parts [-c class] [id] [description] -- store a part in the database
|
||||||
|
|
||||||
|
Options:
|
||||||
|
-f path Path to parts database (default is
|
||||||
|
/home/kyle/.parts.json).
|
||||||
|
|
||||||
142
cmd/parts/main.go
Normal file
142
cmd/parts/main.go
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/kisom/goutils/die"
|
||||||
|
)
|
||||||
|
|
||||||
|
const dbVersion = "1"
|
||||||
|
|
||||||
|
var dbFile = filepath.Join(os.Getenv("HOME"), ".parts.json")
|
||||||
|
var partsDB = &database{Version: dbVersion}
|
||||||
|
|
||||||
|
type part struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Class string `json:"class,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p part) String() string {
|
||||||
|
return fmt.Sprintf("%s: %s", p.Name, p.Description)
|
||||||
|
}
|
||||||
|
|
||||||
|
type database struct {
|
||||||
|
Version string `json:"version"`
|
||||||
|
LastUpdate int64 `json:"json"`
|
||||||
|
Parts map[string]part `json:"parts"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func help(w io.Writer) {
|
||||||
|
fmt.Fprintf(w, `Usage: parts [id] -- query the database for a part
|
||||||
|
parts [-c class] [id] [description] -- store a part in the database
|
||||||
|
|
||||||
|
Options:
|
||||||
|
-f path Path to parts database (default is
|
||||||
|
%s).
|
||||||
|
|
||||||
|
`, dbFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadDatabase() {
|
||||||
|
data, err := ioutil.ReadFile(dbFile)
|
||||||
|
if err != nil && os.IsNotExist(err) {
|
||||||
|
partsDB = &database{
|
||||||
|
Version: dbVersion,
|
||||||
|
Parts: map[string]part{},
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
die.If(err)
|
||||||
|
|
||||||
|
err = json.Unmarshal(data, partsDB)
|
||||||
|
die.If(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func findPart(partName string) {
|
||||||
|
partName = strings.ToLower(partName)
|
||||||
|
for name, part := range partsDB.Parts {
|
||||||
|
if strings.Contains(strings.ToLower(name), partName) {
|
||||||
|
fmt.Println(part.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeDB() {
|
||||||
|
data, err := json.Marshal(partsDB)
|
||||||
|
die.If(err)
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(dbFile, data, 0644)
|
||||||
|
die.If(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func storePart(name, class, description string) {
|
||||||
|
p, exists := partsDB.Parts[name]
|
||||||
|
if exists {
|
||||||
|
fmt.Printf("warning: replacing part %s\n", name)
|
||||||
|
fmt.Printf("\t%s\n", p.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
partsDB.Parts[name] = part{
|
||||||
|
Name: name,
|
||||||
|
Class: class,
|
||||||
|
Description: description,
|
||||||
|
}
|
||||||
|
|
||||||
|
writeDB()
|
||||||
|
}
|
||||||
|
|
||||||
|
func listParts() {
|
||||||
|
parts := make([]string, 0, len(partsDB.Parts))
|
||||||
|
for partName := range partsDB.Parts {
|
||||||
|
parts = append(parts, partName)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(parts)
|
||||||
|
for _, partName := range parts {
|
||||||
|
fmt.Println(partsDB.Parts[partName].String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var class string
|
||||||
|
var helpFlag bool
|
||||||
|
|
||||||
|
flag.StringVar(&class, "c", "", "device class")
|
||||||
|
flag.StringVar(&dbFile, "f", dbFile, "`path` to database")
|
||||||
|
flag.BoolVar(&helpFlag, "h", false, "Print a help message.")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if helpFlag {
|
||||||
|
help(os.Stdout)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
loadDatabase()
|
||||||
|
|
||||||
|
switch flag.NArg() {
|
||||||
|
case 0:
|
||||||
|
help(os.Stdout)
|
||||||
|
return
|
||||||
|
case 1:
|
||||||
|
partName := flag.Arg(0)
|
||||||
|
if partName == "list" {
|
||||||
|
listParts()
|
||||||
|
} else {
|
||||||
|
findPart(flag.Arg(0))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
description := strings.Join(flag.Args()[1:], " ")
|
||||||
|
storePart(flag.Arg(0), class, description)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
46
cmd/sprox/main.go
Normal file
46
cmd/sprox/main.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/kisom/goutils/die"
|
||||||
|
)
|
||||||
|
|
||||||
|
func proxy(conn net.Conn, inside string) error {
|
||||||
|
proxyConn, err := net.Dial("tcp", inside)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer proxyConn.Close()
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
io.Copy(conn, proxyConn)
|
||||||
|
}()
|
||||||
|
_, err = io.Copy(proxyConn, conn)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var outside, inside string
|
||||||
|
flag.StringVar(&outside, "f", "8080", "outside port")
|
||||||
|
flag.StringVar(&inside, "p", "4000", "inside port")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
l, err := net.Listen("tcp", "0.0.0.0:"+outside)
|
||||||
|
die.If(err)
|
||||||
|
|
||||||
|
for {
|
||||||
|
conn, err := l.Accept()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
go proxy(conn, "127.0.0.1:"+inside)
|
||||||
|
}
|
||||||
|
}
|
||||||
76
dbg/dbg.go
Normal file
76
dbg/dbg.go
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
// Package dbg implements a debug printer.
|
||||||
|
package dbg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A DebugPrinter is a drop-in replacement for fmt.Print*, and also acts as
|
||||||
|
// an io.WriteCloser when enabled.
|
||||||
|
type DebugPrinter struct {
|
||||||
|
// If Enabled is false, the print statements won't do anything.
|
||||||
|
Enabled bool
|
||||||
|
out io.WriteCloser
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close satisfies the Closer interface.
|
||||||
|
func (dbg *DebugPrinter) Close() error {
|
||||||
|
return dbg.out.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write satisfies the Writer interface.
|
||||||
|
func (dbg *DebugPrinter) Write(p []byte) (int, error) {
|
||||||
|
if dbg.Enabled {
|
||||||
|
return dbg.out.Write(p)
|
||||||
|
}
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns a new DebugPrinter on os.Stdout.
|
||||||
|
func New() *DebugPrinter {
|
||||||
|
return &DebugPrinter{
|
||||||
|
out: os.Stdout,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToFile sets up a new DebugPrinter to a file, truncating it if it exists.
|
||||||
|
func ToFile(path string) (*DebugPrinter, error) {
|
||||||
|
file, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &DebugPrinter{
|
||||||
|
out: file,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// To sets up a new DebugPrint to an io.WriteCloser.
|
||||||
|
func To(w io.WriteCloser) *DebugPrinter {
|
||||||
|
return &DebugPrinter{
|
||||||
|
out: w,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print calls fmt.Print if Enabled is true.
|
||||||
|
func (dbg DebugPrinter) Print(v ...interface{}) {
|
||||||
|
if dbg.Enabled {
|
||||||
|
fmt.Fprint(dbg.out, v...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Println calls fmt.Println if Enabled is true.
|
||||||
|
func (dbg DebugPrinter) Println(v ...interface{}) {
|
||||||
|
if dbg.Enabled {
|
||||||
|
fmt.Fprintln(dbg.out, v...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Printf calls fmt.Printf if Enabled is true.
|
||||||
|
func (dbg DebugPrinter) Printf(format string, v ...interface{}) {
|
||||||
|
if dbg.Enabled {
|
||||||
|
fmt.Fprintf(dbg.out, format, v...)
|
||||||
|
}
|
||||||
|
}
|
||||||
120
dbg/dbg_test.go
Normal file
120
dbg/dbg_test.go
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
package dbg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kisom/goutils/testio"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNew(t *testing.T) {
|
||||||
|
buf := testio.NewBufCloser(nil)
|
||||||
|
dbg := New()
|
||||||
|
dbg.out = buf
|
||||||
|
|
||||||
|
dbg.Print("hello")
|
||||||
|
dbg.Println("hello")
|
||||||
|
dbg.Printf("hello %s", "world")
|
||||||
|
require.Equal(t, 0, buf.Len())
|
||||||
|
|
||||||
|
dbg.Enabled = true
|
||||||
|
dbg.Print("hello") // +5
|
||||||
|
dbg.Println("hello") // +6
|
||||||
|
dbg.Printf("hello %s", "world") // +11
|
||||||
|
require.Equal(t, 22, buf.Len())
|
||||||
|
|
||||||
|
err := dbg.Close()
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTo(t *testing.T) {
|
||||||
|
buf := testio.NewBufCloser(nil)
|
||||||
|
dbg := To(buf)
|
||||||
|
|
||||||
|
dbg.Print("hello")
|
||||||
|
dbg.Println("hello")
|
||||||
|
dbg.Printf("hello %s", "world")
|
||||||
|
require.Equal(t, 0, buf.Len())
|
||||||
|
|
||||||
|
dbg.Enabled = true
|
||||||
|
dbg.Print("hello") // +5
|
||||||
|
dbg.Println("hello") // +6
|
||||||
|
dbg.Printf("hello %s", "world") // +11
|
||||||
|
|
||||||
|
require.Equal(t, 22, buf.Len())
|
||||||
|
|
||||||
|
err := dbg.Close()
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToFile(t *testing.T) {
|
||||||
|
testFile, err := ioutil.TempFile("", "dbg")
|
||||||
|
require.NoError(t, err)
|
||||||
|
err = testFile.Close()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
testFileName := testFile.Name()
|
||||||
|
defer os.Remove(testFileName)
|
||||||
|
|
||||||
|
dbg, err := ToFile(testFileName)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
dbg.Print("hello")
|
||||||
|
dbg.Println("hello")
|
||||||
|
dbg.Printf("hello %s", "world")
|
||||||
|
|
||||||
|
stat, err := os.Stat(testFileName)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.EqualValues(t, 0, stat.Size())
|
||||||
|
|
||||||
|
dbg.Enabled = true
|
||||||
|
dbg.Print("hello") // +5
|
||||||
|
dbg.Println("hello") // +6
|
||||||
|
dbg.Printf("hello %s", "world") // +11
|
||||||
|
|
||||||
|
stat, err = os.Stat(testFileName)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.EqualValues(t, 22, stat.Size())
|
||||||
|
|
||||||
|
err = dbg.Close()
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriting(t *testing.T) {
|
||||||
|
data := []byte("hello, world")
|
||||||
|
buf := testio.NewBufCloser(nil)
|
||||||
|
dbg := To(buf)
|
||||||
|
|
||||||
|
n, err := dbg.Write(data)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.EqualValues(t, 0, n)
|
||||||
|
|
||||||
|
dbg.Enabled = true
|
||||||
|
n, err = dbg.Write(data)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.EqualValues(t, 12, n)
|
||||||
|
|
||||||
|
err = dbg.Close()
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToFileError(t *testing.T) {
|
||||||
|
testFile, err := ioutil.TempFile("", "dbg")
|
||||||
|
require.NoError(t, err)
|
||||||
|
err = testFile.Chmod(0400)
|
||||||
|
require.NoError(t, err)
|
||||||
|
err = testFile.Close()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
testFileName := testFile.Name()
|
||||||
|
|
||||||
|
_, err = ToFile(testFileName)
|
||||||
|
require.Error(t, err)
|
||||||
|
|
||||||
|
err = os.Remove(testFileName)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
// +build freebsd darwin netbsd
|
// +build freebsd darwin,386 netbsd
|
||||||
|
|
||||||
package lib
|
package lib
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -194,6 +194,11 @@ func (buf *BufCloser) Bytes() []byte {
|
|||||||
return buf.buf.Bytes()
|
return buf.buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Len returns the length of the buffer.
|
||||||
|
func (buf *BufCloser) Len() int {
|
||||||
|
return buf.buf.Len()
|
||||||
|
}
|
||||||
|
|
||||||
// NewBufCloser creates and initializes a new BufCloser using buf as
|
// NewBufCloser creates and initializes a new BufCloser using buf as
|
||||||
// its initial contents. It is intended to prepare a BufCloser to read
|
// its initial contents. It is intended to prepare a BufCloser to read
|
||||||
// existing data. It can also be used to size the internal buffer for
|
// existing data. It can also be used to size the internal buffer for
|
||||||
|
|||||||
Reference in New Issue
Block a user