15 commands = map[string]func(){
26 if len(credsDir) == 0 {
27 credsDir = os.Getenv("CREDSDIR")
32 if argsize := len(os.Args); argsize != 2 {
33 fmt.Fprintf(os.Stderr, "wrong number of args: %d\n", argsize)
41 fmt.Fprintf(os.Stderr, "bad cmd: %s\n", arg)
49 scanner := bufio.NewScanner(os.Stdin)
50 creds := map[string]string{}
52 line := scanner.Text()
53 parts := strings.SplitN(line, "=", 2)
55 fmt.Fprintf(os.Stderr, "bad line: %s\n", line)
59 fmt.Fprintf(os.Stderr, "CREDS RECV: %s\n", line)
60 creds[parts[0]] = strings.TrimSpace(parts[1])
63 if err := scanner.Err(); err != nil {
64 fmt.Fprintf(os.Stderr, "reading standard input: %v", err)
68 hostPieces := strings.SplitN(creds["host"], ":", 2)
69 user, pass, err := credsForHostAndPath(hostPieces[0], creds["path"])
71 fmt.Fprintln(os.Stderr, err.Error())
76 if _, ok := creds["username"]; !ok {
77 creds["username"] = user
80 if _, ok := creds["password"]; !ok {
81 creds["password"] = pass
85 for key, value := range creds {
86 fmt.Fprintf(os.Stderr, "CREDS SEND: %s=%s\n", key, value)
87 fmt.Fprintf(os.Stdout, "%s=%s\n", key, value)
91 func credsForHostAndPath(host, path string) (string, string, error) {
92 hostFilename := filepath.Join(credsDir, host)
95 pathFilename := fmt.Sprintf("%s--%s", hostFilename, strings.Replace(path, "/", "-", -1))
96 u, p, err := credsFromFilename(pathFilename)
102 return credsFromFilename(hostFilename)
105 func credsFromFilename(file string) (string, string, error) {
106 userPass, err := ioutil.ReadFile(file)
108 return "", "", fmt.Errorf("Error opening %q: %s", file, err)
110 credsPieces := strings.SplitN(strings.TrimSpace(string(userPass)), ":", 2)
111 return credsPieces[0], credsPieces[1], nil