Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2b6ae03d1a | |||
| ef0f14a512 | |||
| 6ae393ebf2 | |||
| 76d88c220d | |||
|
|
40e015373f | ||
| 50c226b726 |
@@ -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
|
||||
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:
|
||||
|
||||
ahash/ Provides hashes from string algorithm specifiers.
|
||||
@@ -62,3 +65,5 @@ Each program should have a small README in the directory with more
|
||||
information.
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
110
config/config.go
Normal file
110
config/config.go
Normal file
@@ -0,0 +1,110 @@
|
||||
// Package config implements a simple global configuration system that
|
||||
// supports a file with key=value pairs and environment variables. Note
|
||||
// that the config system is global.
|
||||
//
|
||||
// This package is intended to be used for small daemons: some configuration
|
||||
// file is optionally populated at program start, then this is used to
|
||||
// transparently look up configuration values from either that file or the
|
||||
// environment.
|
||||
package config
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NB: Rather than define a singleton type, everything is defined at
|
||||
// the top-level
|
||||
|
||||
var (
|
||||
vars = map[string]string{}
|
||||
prefix = ""
|
||||
)
|
||||
|
||||
// SetEnvPrefix sets the prefix for all environment variables; it's
|
||||
// assumed to not be needed for files.
|
||||
func SetEnvPrefix(pfx string) {
|
||||
prefix = pfx
|
||||
}
|
||||
|
||||
func addLine(line string) {
|
||||
if strings.HasPrefix(line, "#") || line == "" {
|
||||
return
|
||||
}
|
||||
|
||||
lineParts := strings.SplitN(line, "=", 2)
|
||||
if len(lineParts) != 2 {
|
||||
log.Print("skipping line: ", line)
|
||||
return // silently ignore empty keys
|
||||
}
|
||||
|
||||
lineParts[0] = strings.TrimSpace(lineParts[0])
|
||||
lineParts[1] = strings.TrimSpace(lineParts[1])
|
||||
vars[lineParts[0]] = lineParts[1]
|
||||
}
|
||||
|
||||
// LoadFile scans the file at path for key=value pairs and adds them
|
||||
// to the configuration.
|
||||
func LoadFile(path string) error {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
addLine(line)
|
||||
}
|
||||
|
||||
if err = scanner.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get retrieves a value from either a configuration file or the
|
||||
// environment. Note that values from a file will override environment
|
||||
// variables.
|
||||
func Get(key string) string {
|
||||
if v, ok := vars[key]; ok {
|
||||
return v
|
||||
}
|
||||
return os.Getenv(prefix + key)
|
||||
}
|
||||
|
||||
// GetDefault retrieves a value from either a configuration file or
|
||||
// the environment. Note that value from a file will override
|
||||
// environment variables. If a value isn't found (e.g. Get returns an
|
||||
// empty string), the default value will be used.
|
||||
func GetDefault(key, def string) string {
|
||||
if v := Get(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
// Require retrieves a value from either a configuration file or the
|
||||
// environment. If the key isn't present, it will call log.Fatal, printing
|
||||
// the missing key.
|
||||
func Require(key string) string {
|
||||
if v, ok := vars[key]; ok {
|
||||
return v
|
||||
}
|
||||
|
||||
v, ok := os.LookupEnv(prefix + key)
|
||||
if !ok {
|
||||
var envMessage string
|
||||
if prefix != "" {
|
||||
envMessage = " (note: looked for the key " + prefix + key
|
||||
envMessage += " in the local env)"
|
||||
}
|
||||
log.Fatalf("missing required configuration value %s%s", key, envMessage)
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
66
config/config_test.go
Normal file
66
config/config_test.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
testFilePath = "testdata/test.env"
|
||||
|
||||
// Keys
|
||||
kOrder = "ORDER"
|
||||
kSpecies = "SPECIES"
|
||||
kName = "COMMON_NAME"
|
||||
|
||||
// Env
|
||||
eOrder = "corvus"
|
||||
eSpecies = "corvus corax"
|
||||
eName = "northern raven"
|
||||
|
||||
// File
|
||||
fOrder = "stringiformes"
|
||||
fSpecies = "strix aluco"
|
||||
// Name isn't set in the file to test fall through.
|
||||
)
|
||||
|
||||
func init() {
|
||||
os.Setenv(kOrder, eOrder)
|
||||
os.Setenv(kSpecies, eSpecies)
|
||||
os.Setenv(kName, eName)
|
||||
}
|
||||
|
||||
func TestLoadEnvOnly(t *testing.T) {
|
||||
order := Get(kOrder)
|
||||
species := Get(kSpecies)
|
||||
if order != eOrder {
|
||||
t.Errorf("want %s, have %s", eOrder, order)
|
||||
}
|
||||
|
||||
if species != eSpecies {
|
||||
t.Errorf("want %s, have %s", eSpecies, species)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFile(t *testing.T) {
|
||||
err := LoadFile(testFilePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
order := Get(kOrder)
|
||||
species := Get(kSpecies)
|
||||
name := Get(kName)
|
||||
|
||||
if order != fOrder {
|
||||
t.Errorf("want %s, have %s", fOrder, order)
|
||||
}
|
||||
|
||||
if species != fSpecies {
|
||||
t.Errorf("want %s, have %s", fSpecies, species)
|
||||
}
|
||||
|
||||
if name != eName {
|
||||
t.Errorf("want %s, have %s", eName, name)
|
||||
}
|
||||
}
|
||||
2
config/testdata/test.env
vendored
Normal file
2
config/testdata/test.env
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
ORDER=stringiformes
|
||||
SPECIES=strix aluco
|
||||
@@ -1,4 +1,4 @@
|
||||
// +build freebsd darwin netbsd
|
||||
// +build freebsd darwin,386 netbsd
|
||||
|
||||
package lib
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// +build unix linux openbsd
|
||||
// +build unix linux openbsd darwin,amd64
|
||||
|
||||
package lib
|
||||
|
||||
|
||||
Reference in New Issue
Block a user