github.com/scaleway/scaleway-cli@v1.11.1/pkg/commands/test.go (about)

     1  package commands
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/Sirupsen/logrus"
    10  	"github.com/moul/anonuuid"
    11  	"github.com/scaleway/scaleway-cli/pkg/api"
    12  	"github.com/scaleway/scaleway-cli/pkg/config"
    13  	"github.com/scaleway/scaleway-cli/pkg/scwversion"
    14  )
    15  
    16  func shouldBeAnUUID(actual interface{}, expected ...interface{}) string {
    17  	input := actual.(string)
    18  	input = strings.TrimSpace(input)
    19  	if err := anonuuid.IsUUID(input); err != nil {
    20  		return fmt.Sprintf("%q should be an UUID", actual)
    21  	}
    22  	return ""
    23  }
    24  
    25  func getScopedCtx(sessionCtx *CommandContext) (*CommandContext, *bytes.Buffer, *bytes.Buffer) {
    26  	stdout := bytes.Buffer{}
    27  	stderr := bytes.Buffer{}
    28  
    29  	var newCtx CommandContext
    30  	newCtx = *sessionCtx
    31  	newCtx.Stdout = &stdout
    32  	newCtx.Stderr = &stderr
    33  
    34  	return &newCtx, &stdout, &stderr
    35  }
    36  
    37  // RealAPIContext returns a CommandContext with a configured API
    38  func RealAPIContext() *CommandContext {
    39  	if os.Getenv("TEST_WITH_REAL_API") == "0" {
    40  		return nil
    41  	}
    42  	config, err := config.GetConfig()
    43  	if err != nil {
    44  		logrus.Warnf("RealAPIContext: failed to call config.GetConfig(): %v", err)
    45  		return nil
    46  	}
    47  
    48  	apiClient, err := api.NewScalewayAPI(config.Organization, config.Token, scwversion.UserAgent(), "par1")
    49  	if err != nil {
    50  		logrus.Warnf("RealAPIContext: failed to call api.NewScalewayAPI(): %v", err)
    51  		return nil
    52  	}
    53  
    54  	stdout := bytes.Buffer{}
    55  	stderr := bytes.Buffer{}
    56  
    57  	ctx := CommandContext{
    58  		Streams: Streams{
    59  			Stdin:  os.Stdin,
    60  			Stdout: &stdout,
    61  			Stderr: &stderr,
    62  		},
    63  		Env: []string{
    64  			"HOME" + os.Getenv("HOME"),
    65  		},
    66  		RawArgs: []string{},
    67  		API:     apiClient,
    68  	}
    69  	return &ctx
    70  }