1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Style changes in auth.Login

This commit is contained in:
shin- 2013-04-24 09:11:29 -07:00
parent 6644a3c78a
commit 23953e7d67

View file

@ -3,7 +3,6 @@ package auth
import ( import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -16,7 +15,7 @@ import (
const CONFIGFILE = ".dockercfg" const CONFIGFILE = ".dockercfg"
// the registry server we want to login against // the registry server we want to login against
const INDEX_SERVER = "https://index.docker.io" const INDEX_SERVER = "https://indexstaging-docker.dotcloud.com"
type AuthConfig struct { type AuthConfig struct {
Username string `json:"username"` Username string `json:"username"`
@ -103,28 +102,24 @@ func Login(authConfig *AuthConfig) (string, error) {
storeConfig := false storeConfig := false
reqStatusCode := 0 reqStatusCode := 0
var status string var status string
var errMsg string
var reqBody []byte var reqBody []byte
jsonBody, err := json.Marshal(authConfig) jsonBody, err := json.Marshal(authConfig)
if err != nil { if err != nil {
errMsg = fmt.Sprintf("Config Error: %s", err) return "", fmt.Errorf("Config Error: %s", err)
return "", errors.New(errMsg)
} }
// using `bytes.NewReader(jsonBody)` here causes the server to respond with a 411 status. // using `bytes.NewReader(jsonBody)` here causes the server to respond with a 411 status.
b := strings.NewReader(string(jsonBody)) b := strings.NewReader(string(jsonBody))
req1, err := http.Post(INDEX_SERVER+"/v1/users", "application/json; charset=utf-8", b) req1, err := http.Post(INDEX_SERVER+"/v1/users", "application/json; charset=utf-8", b)
if err != nil { if err != nil {
errMsg = fmt.Sprintf("Server Error: %s", err) return "", fmt.Errorf("Server Error: %s", err)
return "", errors.New(errMsg)
} }
reqStatusCode = req1.StatusCode reqStatusCode = req1.StatusCode
defer req1.Body.Close() defer req1.Body.Close()
reqBody, err = ioutil.ReadAll(req1.Body) reqBody, err = ioutil.ReadAll(req1.Body)
if err != nil { if err != nil {
errMsg = fmt.Sprintf("Server Error: [%#v] %s", reqStatusCode, err) return "", fmt.Errorf("Server Error: [%#v] %s", reqStatusCode, err)
return "", errors.New(errMsg)
} }
if reqStatusCode == 201 { if reqStatusCode == 201 {
@ -149,16 +144,13 @@ func Login(authConfig *AuthConfig) (string, error) {
status = "Login Succeeded\n" status = "Login Succeeded\n"
storeConfig = true storeConfig = true
} else { } else {
status = fmt.Sprintf("Login: %s", body) return "", fmt.Errorf("Login: %s", body)
return "", errors.New(status)
} }
} else { } else {
status = fmt.Sprintf("Registration: %s", reqBody) return "", fmt.Errorf("Registration: %s", reqBody)
return "", errors.New(status)
} }
} else { } else {
status = fmt.Sprintf("[%s] : %s", reqStatusCode, reqBody) return "", fmt.Errorf("Unexpected status code [%d] : %s", reqStatusCode, reqBody)
return "", errors.New(status)
} }
if storeConfig { if storeConfig {
authStr := EncodeAuth(authConfig) authStr := EncodeAuth(authConfig)