Imported Upstream version 2.4.2
[scm/test.git] / t / cmd / git-credential-lfstest.go
1 // +build testtools
2
3 package main
4
5 import (
6         "bufio"
7         "fmt"
8         "io/ioutil"
9         "os"
10         "path/filepath"
11         "strings"
12 )
13
14 var (
15         commands = map[string]func(){
16                 "get":   fill,
17                 "store": noop,
18                 "erase": noop,
19         }
20
21         delim    = '\n'
22         credsDir = ""
23 )
24
25 func init() {
26         if len(credsDir) == 0 {
27                 credsDir = os.Getenv("CREDSDIR")
28         }
29 }
30
31 func main() {
32         if argsize := len(os.Args); argsize != 2 {
33                 fmt.Fprintf(os.Stderr, "wrong number of args: %d\n", argsize)
34                 os.Exit(1)
35         }
36
37         arg := os.Args[1]
38         cmd := commands[arg]
39
40         if cmd == nil {
41                 fmt.Fprintf(os.Stderr, "bad cmd: %s\n", arg)
42                 os.Exit(1)
43         }
44
45         cmd()
46 }
47
48 func fill() {
49         scanner := bufio.NewScanner(os.Stdin)
50         creds := map[string]string{}
51         for scanner.Scan() {
52                 line := scanner.Text()
53                 parts := strings.SplitN(line, "=", 2)
54                 if len(parts) != 2 {
55                         fmt.Fprintf(os.Stderr, "bad line: %s\n", line)
56                         os.Exit(1)
57                 }
58
59                 fmt.Fprintf(os.Stderr, "CREDS RECV: %s\n", line)
60                 creds[parts[0]] = strings.TrimSpace(parts[1])
61         }
62
63         if err := scanner.Err(); err != nil {
64                 fmt.Fprintf(os.Stderr, "reading standard input: %v", err)
65                 os.Exit(1)
66         }
67
68         hostPieces := strings.SplitN(creds["host"], ":", 2)
69         user, pass, err := credsForHostAndPath(hostPieces[0], creds["path"])
70         if err != nil {
71                 fmt.Fprintln(os.Stderr, err.Error())
72                 os.Exit(1)
73         }
74
75         if user != "skip" {
76                 if _, ok := creds["username"]; !ok {
77                         creds["username"] = user
78                 }
79
80                 if _, ok := creds["password"]; !ok {
81                         creds["password"] = pass
82                 }
83         }
84
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)
88         }
89 }
90
91 func credsForHostAndPath(host, path string) (string, string, error) {
92         hostFilename := filepath.Join(credsDir, host)
93
94         if len(path) > 0 {
95                 pathFilename := fmt.Sprintf("%s--%s", hostFilename, strings.Replace(path, "/", "-", -1))
96                 u, p, err := credsFromFilename(pathFilename)
97                 if err == nil {
98                         return u, p, err
99                 }
100         }
101
102         return credsFromFilename(hostFilename)
103 }
104
105 func credsFromFilename(file string) (string, string, error) {
106         userPass, err := ioutil.ReadFile(file)
107         if err != nil {
108                 return "", "", fmt.Errorf("Error opening %q: %s", file, err)
109         }
110         credsPieces := strings.SplitN(strings.TrimSpace(string(userPass)), ":", 2)
111         return credsPieces[0], credsPieces[1], nil
112 }
113
114 func noop() {}