github.com/pluralsh/plural-cli@v0.9.5/pkg/bundle/tests/git.go (about)

     1  package tests
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/go-git/go-git/v5/plumbing/transport"
     8  	"github.com/pluralsh/plural-cli/pkg/api"
     9  	"github.com/pluralsh/plural-cli/pkg/manifest"
    10  	"github.com/pluralsh/plural-cli/pkg/utils/git"
    11  )
    12  
    13  func testGit(ctx *manifest.Context, test *api.RecipeTest) error {
    14  	args := collectArguments(test.Args, ctx)
    15  	auth, err := authMethod(args)
    16  	if err != nil {
    17  		return err
    18  	}
    19  	url := args["url"].Val.(string)
    20  	dir, err := os.MkdirTemp("", "repo")
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	defer os.RemoveAll(dir)
    26  	fmt.Println("~~> Attempting to clone repo in a temporary directory...")
    27  	_, err = git.Clone(auth, url, dir)
    28  	return err
    29  }
    30  
    31  func authMethod(args map[string]*ContextValue) (transport.AuthMethod, error) {
    32  	if arg, ok := args["password"]; ok && arg.Present {
    33  		if pass, ok := arg.Val.(string); ok && pass != "" {
    34  			if user, ok := args["username"].Val.(string); ok {
    35  				return git.BasicAuth(user, pass)
    36  			}
    37  			return nil, fmt.Errorf("No valid username/password pair for basic auth")
    38  		}
    39  	}
    40  
    41  	urlArg := args["url"]
    42  	if !urlArg.Present {
    43  		return nil, fmt.Errorf("requires a git url")
    44  	}
    45  
    46  	url, ok := urlArg.Val.(string)
    47  	if !ok {
    48  		return nil, fmt.Errorf("No valid git url")
    49  	}
    50  
    51  	privateKeyArg := args["private_key"]
    52  	if !privateKeyArg.Present {
    53  		return nil, fmt.Errorf("requires a ssh private key for authentication")
    54  	}
    55  
    56  	pk, ok := privateKeyArg.Val.(string)
    57  	if !ok {
    58  		return nil, fmt.Errorf("No valid git ssh private key")
    59  	}
    60  
    61  	passphrase := ""
    62  	if passArg, ok := args["passphrase"]; ok && passArg.Present {
    63  		if pass, ok := passArg.Val.(string); ok {
    64  			passphrase = pass
    65  		}
    66  	}
    67  
    68  	user, _, _, _, err := git.UrlComponents(url)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	return git.SSHAuth(user, pk, passphrase)
    73  }