github.com/jfrog/jfrog-cli-core/v2@v2.51.0/utils/golang/utils.go (about)

     1  package goutils
     2  
     3  import (
     4  	"github.com/jfrog/build-info-go/utils"
     5  	"github.com/jfrog/jfrog-cli-core/v2/utils/config"
     6  	"github.com/jfrog/jfrog-client-go/auth"
     7  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
     8  	"github.com/jfrog/jfrog-client-go/utils/log"
     9  	"io"
    10  	"net/url"
    11  	"os/exec"
    12  )
    13  
    14  type GoCmdConfig struct {
    15  	Go           string
    16  	Command      []string
    17  	CommandFlags []string
    18  	Dir          string
    19  	StrWriter    io.WriteCloser
    20  	ErrWriter    io.WriteCloser
    21  }
    22  
    23  func NewGoCmdConfig() (*GoCmdConfig, error) {
    24  	execPath, err := exec.LookPath("go")
    25  	if err != nil {
    26  		return nil, errorutils.CheckError(err)
    27  	}
    28  	return &GoCmdConfig{Go: execPath}, nil
    29  }
    30  
    31  func (config *GoCmdConfig) GetCmd() (cmd *exec.Cmd) {
    32  	var cmdStr []string
    33  	cmdStr = append(cmdStr, config.Go)
    34  	cmdStr = append(cmdStr, config.Command...)
    35  	cmdStr = append(cmdStr, config.CommandFlags...)
    36  	cmd = exec.Command(cmdStr[0], cmdStr[1:]...)
    37  	cmd.Dir = config.Dir
    38  	return
    39  }
    40  
    41  func (config *GoCmdConfig) GetEnv() map[string]string {
    42  	return map[string]string{}
    43  }
    44  
    45  func (config *GoCmdConfig) GetStdWriter() io.WriteCloser {
    46  	return config.StrWriter
    47  }
    48  
    49  func (config *GoCmdConfig) GetErrWriter() io.WriteCloser {
    50  	return config.ErrWriter
    51  }
    52  
    53  func LogGoVersion() error {
    54  	version, err := utils.GetParsedGoVersion()
    55  	if err != nil {
    56  		return errorutils.CheckError(err)
    57  	}
    58  	log.Info("Using go:", version.GetVersion())
    59  	return nil
    60  }
    61  
    62  func GetGoModCachePath() (string, error) {
    63  	path, err := utils.GetGoModCachePath()
    64  	if err != nil {
    65  		return "", errorutils.CheckError(err)
    66  	}
    67  	return path, nil
    68  }
    69  
    70  func GetProjectRoot() (string, error) {
    71  	path, err := utils.GetProjectRoot()
    72  	if err != nil {
    73  		return "", errorutils.CheckError(err)
    74  	}
    75  	return path, nil
    76  }
    77  
    78  func GetModuleName(projectDir string) (string, error) {
    79  	path, err := utils.GetModuleNameByDir(projectDir, log.Logger)
    80  	if err != nil {
    81  		return "", errorutils.CheckError(err)
    82  	}
    83  	return path, nil
    84  }
    85  
    86  func GetDependenciesList(projectDir string) (map[string]bool, error) {
    87  	deps, err := utils.GetDependenciesList(projectDir, log.Logger)
    88  	if err != nil {
    89  		return nil, errorutils.CheckError(err)
    90  	}
    91  	return deps, nil
    92  }
    93  
    94  func GetDependenciesGraph(projectDir string) (map[string][]string, error) {
    95  	deps, err := utils.GetDependenciesGraph(projectDir, log.Logger)
    96  	if err != nil {
    97  		return nil, errorutils.CheckError(err)
    98  	}
    99  	return deps, nil
   100  }
   101  
   102  func GetArtifactoryRemoteRepoUrl(serverDetails *config.ServerDetails, repo string) (string, error) {
   103  	authServerDetails, err := serverDetails.CreateArtAuthConfig()
   104  	if err != nil {
   105  		return "", err
   106  	}
   107  	return getArtifactoryApiUrl(repo, authServerDetails)
   108  }
   109  
   110  // Gets the URL of the specified repository Go API in Artifactory.
   111  // The URL contains credentials (username and access token or password).
   112  func getArtifactoryApiUrl(repoName string, details auth.ServiceDetails) (string, error) {
   113  	rtUrl, err := url.Parse(details.GetUrl())
   114  	if err != nil {
   115  		return "", errorutils.CheckError(err)
   116  	}
   117  
   118  	username := details.GetUser()
   119  	password := details.GetPassword()
   120  
   121  	// Get credentials from access-token if exists.
   122  	if details.GetAccessToken() != "" {
   123  		log.Debug("Using proxy with access-token.")
   124  		if username == "" {
   125  			username = auth.ExtractUsernameFromAccessToken(details.GetAccessToken())
   126  		}
   127  		password = details.GetAccessToken()
   128  	}
   129  	if password != "" {
   130  		rtUrl.User = url.UserPassword(username, password)
   131  	}
   132  	rtUrl.Path += "api/go/" + repoName
   133  	return rtUrl.String(), nil
   134  }