| 38 | } |
| 39 | |
| 40 | func newGitlabClient() (*gitlab.Client, error) { |
| 41 | switch { |
| 42 | case getCfg("access_token") != "" && getCfg("host_url") != "": |
| 43 | gitlabClient, err := |
| 44 | newOAuthClient(getCfg("access_token"), getCfg("host_url")+"/api/v4") |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | return gitlabClient, nil |
| 49 | |
| 50 | case getCfg("USERNAME") != "" && |
| 51 | getCfg("PASSWORD") != "" && |
| 52 | getCfg("HTTP_URL") != "": |
| 53 | gitlabClient, err := newBasicAuthClient(getCfg("USERNAME"), |
| 54 | getCfg("PASSWORD"), |
| 55 | getCfg("HTTP_URL"), |
| 56 | ) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | return gitlabClient, nil |
| 61 | |
| 62 | case getCfg("PRIVATE_TOKEN") != "" && getCfg("API_HTTP_URL") != "": |
| 63 | gitlabClient, err := |
| 64 | newClient(getCfg("PRIVATE_TOKEN"), getCfg("API_HTTP_URL")) |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | return gitlabClient, nil |
| 69 | |
| 70 | case getCfg("OAUTH_TOKEN") != "" && getCfg("API_HTTP_URL") != "": |
| 71 | gitlabClient, err := |
| 72 | newOAuthClient(getCfg("OAUTH_TOKEN"), getCfg("API_HTTP_URL")) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | return gitlabClient, nil |
| 77 | |
| 78 | default: |
| 79 | return nil, fmt.Errorf("no client was created. "+ |
| 80 | "gitlab configuration was not set properly. \n %s", authDoc) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // getCfg retrieves the value of a token from the config '--config' file |
| 85 | // in 'key=value' format or from any environment variable that starts |