diff --git a/cmd/login.go b/cmd/login.go index 8283509..206135d 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -14,10 +14,7 @@ var loginCmd = &cobra.Command{ Short: "Log in to a Portainer instance", Run: func(cmd *cobra.Command, args []string) { // Get auth token - config := common.GetDefaultClientConfig() - config.DoNotUseToken = true - - client, err := common.NewClient(common.GetDefaultHttpClient(), config) + client, err := common.GetDefaultClient() common.CheckError(err) authToken, err := client.Authenticate() diff --git a/common/client.go b/common/client.go index 164d9e9..2ff3494 100644 --- a/common/client.go +++ b/common/client.go @@ -77,9 +77,7 @@ func (n *PortainerClient) do(uri, method string, request io.Reader, requestType if !n.doNotUseToken { if n.token == "" { - clientClone := n.Clone() - clientClone.doNotUseToken = true - n.token, err = clientClone.Authenticate() + n.token, err = n.Authenticate() if err != nil { return } @@ -144,11 +142,16 @@ func (n *PortainerClient) Authenticate() (token string, err error) { respBody := AuthenticateUserResponse{} + previousDoNotUseTokenValue := n.doNotUseToken + n.doNotUseToken = true + err = n.doJSON("auth", http.MethodPost, &reqBody, &respBody) if err != nil { return } + n.doNotUseToken = previousDoNotUseTokenValue + token = respBody.Jwt return @@ -258,20 +261,6 @@ func (n *PortainerClient) GetStatus() (status Status, err error) { return } -// Get a clone of the client -func (n *PortainerClient) Clone() (c *PortainerClient) { - c = &PortainerClient{ - httpClient: n.httpClient, - url: n.url, - user: n.user, - password: n.password, - token: n.token, - doNotUseToken: n.doNotUseToken, - } - - return -} - // Create a new client func NewClient(httpClient *http.Client, config ClientConfig) (c *PortainerClient, err error) { apiUrl, err := url.Parse(strings.TrimRight(config.Url, "/") + "/api/")