2015-05-21 17:04:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2015-08-04 16:06:30 +00:00
|
|
|
"io/ioutil"
|
2015-05-21 17:04:59 +00:00
|
|
|
"os"
|
2015-08-04 16:06:30 +00:00
|
|
|
"path/filepath"
|
2015-05-21 17:04:59 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
commands = map[string]func(){
|
|
|
|
"get": fill,
|
|
|
|
"store": noop,
|
|
|
|
"erase": noop,
|
|
|
|
}
|
|
|
|
|
2015-08-28 19:38:56 +00:00
|
|
|
delim = '\n'
|
2015-08-11 20:18:30 +00:00
|
|
|
credsDir = ""
|
2015-05-21 17:04:59 +00:00
|
|
|
)
|
|
|
|
|
2015-08-11 20:18:30 +00:00
|
|
|
func init() {
|
|
|
|
if len(credsDir) == 0 {
|
|
|
|
credsDir = os.Getenv("CREDSDIR")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 17:04:59 +00:00
|
|
|
func main() {
|
|
|
|
if argsize := len(os.Args); argsize != 2 {
|
|
|
|
fmt.Fprintf(os.Stderr, "wrong number of args: %d\n", argsize)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
arg := os.Args[1]
|
|
|
|
cmd := commands[arg]
|
|
|
|
|
|
|
|
if cmd == nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "bad cmd: %s\n", arg)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd()
|
|
|
|
}
|
|
|
|
|
|
|
|
func fill() {
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
creds := map[string]string{}
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
parts := strings.SplitN(line, "=", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
fmt.Fprintf(os.Stderr, "bad line: %s\n", line)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2015-08-04 15:27:31 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "CREDS RECV: %s\n", line)
|
2015-05-21 17:04:59 +00:00
|
|
|
creds[parts[0]] = strings.TrimSpace(parts[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "reading standard input: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2015-08-04 16:06:30 +00:00
|
|
|
hostPieces := strings.SplitN(creds["host"], ":", 2)
|
|
|
|
user, pass, err := credsForHostAndPath(hostPieces[0], creds["path"])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
2015-05-21 17:04:59 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 19:38:56 +00:00
|
|
|
if user != "skip" {
|
|
|
|
if _, ok := creds["username"]; !ok {
|
|
|
|
creds["username"] = user
|
|
|
|
}
|
2015-05-21 17:04:59 +00:00
|
|
|
|
2015-08-28 19:38:56 +00:00
|
|
|
if _, ok := creds["password"]; !ok {
|
|
|
|
creds["password"] = pass
|
|
|
|
}
|
2015-05-21 17:04:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range creds {
|
2015-08-04 15:27:31 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "CREDS SEND: %s=%s\n", key, value)
|
2015-05-21 17:04:59 +00:00
|
|
|
fmt.Fprintf(os.Stdout, "%s=%s\n", key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 16:06:30 +00:00
|
|
|
func credsForHostAndPath(host, path string) (string, string, error) {
|
|
|
|
hostFilename := filepath.Join(credsDir, host)
|
|
|
|
|
|
|
|
if len(path) > 0 {
|
2015-08-04 17:13:46 +00:00
|
|
|
pathFilename := fmt.Sprintf("%s--%s", hostFilename, strings.Replace(path, "/", "-", -1))
|
|
|
|
u, p, err := credsFromFilename(pathFilename)
|
2015-08-04 16:06:30 +00:00
|
|
|
if err == nil {
|
|
|
|
return u, p, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return credsFromFilename(hostFilename)
|
|
|
|
}
|
|
|
|
|
|
|
|
func credsFromFilename(file string) (string, string, error) {
|
|
|
|
userPass, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("Error opening %q: %s", file, err)
|
|
|
|
}
|
|
|
|
credsPieces := strings.SplitN(strings.TrimSpace(string(userPass)), ":", 2)
|
|
|
|
return credsPieces[0], credsPieces[1], nil
|
|
|
|
}
|
|
|
|
|
2015-05-21 17:04:59 +00:00
|
|
|
func noop() {}
|