2222 credsDir = ""
2323)
2424
25+ type credential struct {
26+ authtype string
27+ username string
28+ password string
29+ credential string
30+ skip bool
31+ }
32+
33+ func (c * credential ) Serialize (capabilities map [string ]struct {}) map [string ][]string {
34+ creds := make (map [string ][]string )
35+ if c .skip {
36+ // Do nothing.
37+ } else if _ , ok := capabilities ["authtype" ]; ok && len (c .authtype ) != 0 && len (c .credential ) != 0 {
38+ creds ["authtype" ] = []string {c .authtype }
39+ creds ["credential" ] = []string {c .credential }
40+ } else if len (c .username ) != 0 && len (c .password ) != 0 {
41+ creds ["username" ] = []string {c .username }
42+ creds ["password" ] = []string {c .password }
43+ }
44+ return creds
45+ }
46+
2547func init () {
2648 if len (credsDir ) == 0 {
2749 credsDir = os .Getenv ("CREDSDIR" )
@@ -70,35 +92,14 @@ func fill() {
7092 }
7193
7294 hostPieces := strings .SplitN (firstEntryForKey (creds , "host" ), ":" , 2 )
73- authtype , user , cred , err := credsForHostAndPath (hostPieces [0 ], firstEntryForKey (creds , "path" ))
95+ credentials , err := credsForHostAndPath (hostPieces [0 ], firstEntryForKey (creds , "path" ))
7496 if err != nil {
7597 fmt .Fprintln (os .Stderr , err .Error ())
7698 os .Exit (1 )
7799 }
78100
79101 capas := discoverCapabilities (creds )
80-
81- switch authtype {
82- case "skip" :
83- case "" :
84- if _ , ok := creds ["username" ]; ! ok {
85- creds ["username" ] = []string {user }
86- }
87-
88- if _ , ok := creds ["password" ]; ! ok {
89- creds ["password" ] = []string {cred }
90- }
91- default :
92- if _ , ok := capas ["authtype" ]; ok {
93- if _ , ok := creds ["authtype" ]; ! ok {
94- creds ["authtype" ] = []string {authtype }
95- }
96-
97- if _ , ok := creds ["credential" ]; ! ok {
98- creds ["credential" ] = []string {cred }
99- }
100- }
101- }
102+ creds = cred .Serialize (capas )
102103
103104 mode := os .Getenv ("LFS_TEST_CREDS_WWWAUTH" )
104105 wwwauth := firstEntryForKey (creds , "wwwauth[]" )
@@ -145,7 +146,7 @@ func discoverCapabilities(creds map[string][]string) map[string]struct{} {
145146 return capas
146147}
147148
148- func credsForHostAndPath (host , path string ) (string , string , string , error ) {
149+ func credsForHostAndPath (host , path string ) (credential , error ) {
149150 var hostFilename string
150151
151152 // We need hostFilename to end in a slash so that our credentials all
@@ -160,25 +161,31 @@ func credsForHostAndPath(host, path string) (string, string, string, error) {
160161
161162 if len (path ) > 0 {
162163 pathFilename := fmt .Sprintf ("%s--%s" , hostFilename , strings .Replace (path , "/" , "-" , - 1 ))
163- authtype , u , cred , err := credsFromFilename (pathFilename )
164+ cred , err := credsFromFilename (pathFilename )
164165 if err == nil {
165- return authtype , u , cred , err
166+ return cred , err
166167 }
167168 }
168169
169170 return credsFromFilename (hostFilename )
170171}
171172
172- func credsFromFilename (file string ) (string , string , string , error ) {
173- credential , err := os .ReadFile (file )
173+ func credsFromFilename (file string ) (credential , error ) {
174+ fileContents , err := os .ReadFile (file )
174175 if err != nil {
175- return "" , "" , "" , fmt .Errorf ("Error opening %q: %s" , file , err )
176+ return credential {} , fmt .Errorf ("Error opening %q: %s" , file , err )
176177 }
177- credsPieces := strings .SplitN (strings .TrimSpace (string (credential )), ":" , 3 )
178+ credsPieces := strings .SplitN (strings .TrimSpace (string (fileContents )), ":" , 3 )
178179 if len (credsPieces ) != 3 {
179- return "" , "" , "" , fmt .Errorf ("Invalid data %q while reading %q" , string (credential ), file )
180+ return credential {}, fmt .Errorf ("Invalid data %q while reading %q" , string (fileContents ), file )
181+ }
182+ if credsPieces [0 ] == "skip" {
183+ return credential {skip : true }, nil
184+ } else if len (credsPieces [0 ]) == 0 {
185+ return credential {username : credsPieces [1 ], password : credsPieces [2 ]}, nil
186+ } else {
187+ return credential {authtype : credsPieces [0 ], credential : credsPieces [2 ]}, nil
180188 }
181- return credsPieces [0 ], credsPieces [1 ], credsPieces [2 ], nil
182189}
183190
184191func log () {
0 commit comments