[MM-49989] Pass a context.Context to Client4 methods (#22922)

* Migrate all method in model/client4.go to accept a context.Context

* Fix th.*Client

* Fix remaining issues

* Empty commit to triger CI

* Fix test

* Add cancellation test

* Test that returned error is context.Canceled

* Fix bad merge

* Update mmctl code

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Ben Schumacher
2023-06-06 23:29:29 +02:00
коммит произвёл GitHub
родитель 7116e9267a
Коммит 6c82605df0
140 изменённых файлов: 7516 добавлений и 7333 удалений

Просмотреть файл

@@ -5,6 +5,7 @@ package commands
import (
"bufio"
"context"
"fmt"
"os"
"sort"
@@ -166,6 +167,8 @@ func loginCmdF(cmd *cobra.Command, args []string) error {
url := strings.TrimRight(args[0], "/")
method := MethodPassword
ctx := context.TODO()
if name == "" {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("Connection name: ")
@@ -203,10 +206,10 @@ func loginCmdF(cmd *cobra.Command, args []string) error {
var c *model.Client4
var err error
if mfaToken != "" {
c, _, err = InitClientWithMFA(username, password, mfaToken, url, allowInsecureSHA1, allowInsecureTLS)
c, _, err = InitClientWithMFA(ctx, username, password, mfaToken, url, allowInsecureSHA1, allowInsecureTLS)
method = MethodMFA
} else {
c, _, err = InitClientWithUsernameAndPassword(username, password, url, allowInsecureSHA1, allowInsecureTLS)
c, _, err = InitClientWithUsernameAndPassword(ctx, username, password, url, allowInsecureSHA1, allowInsecureTLS)
}
if err != nil {
return fmt.Errorf("could not initiate client: %w", err)
@@ -219,7 +222,7 @@ func loginCmdF(cmd *cobra.Command, args []string) error {
InstanceURL: url,
AuthToken: accessToken,
}
if _, _, err := InitClientWithCredentials(&credentials, allowInsecureSHA1, allowInsecureTLS); err != nil {
if _, _, err := InitClientWithCredentials(ctx, &credentials, allowInsecureSHA1, allowInsecureTLS); err != nil {
return fmt.Errorf("could not initiate client: %w", err)
}
}
@@ -350,6 +353,8 @@ func renewCmdF(cmd *cobra.Command, args []string) error {
return err
}
ctx := context.TODO()
if (credentials.AuthMethod == MethodPassword || credentials.AuthMethod == MethodMFA) && password == "" {
if password == "" {
fmt.Printf("Password: ")
@@ -363,7 +368,7 @@ func renewCmdF(cmd *cobra.Command, args []string) error {
switch credentials.AuthMethod {
case MethodPassword:
c, _, err := InitClientWithUsernameAndPassword(credentials.Username, password, credentials.InstanceURL, allowInsecureSHA1, allowInsecureTLS)
c, _, err := InitClientWithUsernameAndPassword(ctx, credentials.Username, password, credentials.InstanceURL, allowInsecureSHA1, allowInsecureTLS)
if err != nil {
return err
}
@@ -376,7 +381,7 @@ func renewCmdF(cmd *cobra.Command, args []string) error {
}
credentials.AuthToken = accessToken
if _, _, err := InitClientWithCredentials(credentials, allowInsecureSHA1, allowInsecureTLS); err != nil {
if _, _, err := InitClientWithCredentials(ctx, credentials, allowInsecureSHA1, allowInsecureTLS); err != nil {
return err
}
@@ -385,7 +390,7 @@ func renewCmdF(cmd *cobra.Command, args []string) error {
return errors.New("requires the --mfa-token parameter to be set")
}
c, _, err := InitClientWithMFA(credentials.Username, password, mfaToken, credentials.InstanceURL, allowInsecureSHA1, allowInsecureTLS)
c, _, err := InitClientWithMFA(ctx, credentials.Username, password, mfaToken, credentials.InstanceURL, allowInsecureSHA1, allowInsecureTLS)
if err != nil {
return err
}