// Common function to run dependencies command for list or graph commands
Common function to run dependencies command for list or graph commands
// Common function to run dependencies command for list or graph commands
func runDependenciesCmd(projectDir string, commandArgs []string, log Log) (output string, err error) {
	log.Info(fmt.Sprintf("Running 'go %s' in %s", strings.Join(commandArgs, " "), projectDir))
	if projectDir == "" {
		projectDir, err = GetProjectRoot()
		if err != nil {
			return "", err
		}
	}
	modFileContent, modFileStat, err := GetFileContentAndInfo(filepath.Join(projectDir, "go.mod"))
	if err != nil {
		log.Info("Dependencies were not collected for this build, since go.mod could not be found in", projectDir)
		return "", nil
	}
	sumFileContent, sumFileStat, err := GetFileContentAndInfo(filepath.Join(projectDir, "go.sum"))
	if err != nil && !os.IsNotExist(err) {
		return "", err
	}
	if err == nil {
		defer func() {
			e := ioutil.WriteFile(filepath.Join(projectDir, "go.sum"), sumFileContent, sumFileStat.Mode())
			if err == nil {
				err = e
			}
		}()
	}
	goCmd, err := newCmd()
	if err != nil {
		return "", err
	}
	goCmd.Command = commandArgs
	goCmd.Dir = projectDir
	err = prepareGlobalRegExp()
	if err != nil {
		return "", err
	}
	performPasswordMask, err := shouldMaskPassword()
	if err != nil {
		return "", err
	}
	var executionError error
	if performPasswordMask {
		output, _, _, executionError = gofrogcmd.RunCmdWithOutputParser(goCmd, true, protocolRegExp)
	} else {
		output, _, _, executionError = gofrogcmd.RunCmdWithOutputParser(goCmd, true)
	}
	if len(output) != 0 {
		log.Debug(output)
	}
	if executionError != nil {
		errorString := fmt.Sprintf("Failed running Go command: 'go %s' in %s with error: '%s'", strings.Join(commandArgs, " "), projectDir, executionError.Error())
		return "", errors.New(errorString)
	}
	err = ioutil.WriteFile(filepath.Join(projectDir, "go.mod"), modFileContent, modFileStat.Mode())
	if err != nil {
		return "", err
	}
	return output, err
}
func runDependenciesCmd ( projectDir string , commandArgs [ ] string , log Log ) ( output string , err error ) { log . Info ( fmt . Sprintf ( " " , strings . Join ( commandArgs , " " ) , projectDir ) ) if projectDir == " " { projectDir , err = GetProjectRoot ( ) if err != nil { return " " , err } } modFileContent , modFileStat , err := GetFileContentAndInfo ( filepath . Join ( projectDir , " " ) ) if err != nil { log . Info ( " " , projectDir ) return " " , nil } sumFileContent , sumFileStat , err := GetFileContentAndInfo ( filepath . Join ( projectDir , " " ) ) if err != nil && ! os . IsNotExist ( err ) { return " " , err } if err == nil { defer func ( ) { e := ioutil . WriteFile ( filepath . Join ( projectDir , " " ) , sumFileContent , sumFileStat . Mode ( ) ) if err == nil { err = e } } ( ) } goCmd , err := newCmd ( ) if err != nil { return " " , err } goCmd . Command = commandArgs goCmd . Dir = projectDir err = prepareGlobalRegExp ( ) if err != nil { return " " , err } performPasswordMask , err := shouldMaskPassword ( ) if err != nil { return " " , err } var executionError error if performPasswordMask { output , _ , _ , executionError = gofrogcmd . RunCmdWithOutputParser ( goCmd , true , protocolRegExp ) } else { output , _ , _ , executionError = gofrogcmd . RunCmdWithOutputParser ( goCmd , true ) } if len ( output ) != 0 { log . Debug ( output ) } if executionError != nil { errorString := fmt . Sprintf ( " " , strings . Join ( commandArgs , " " ) , projectDir , executionError . Error ( ) ) return " " , errors . New ( errorString ) } err = ioutil . WriteFile ( filepath . Join ( projectDir , " " ) , modFileContent , modFileStat . Mode ( ) ) if err != nil { return " " , err } return output , err }
Common function to run dependencies command for list or graph commands
Common function to run dependencies command for list or graph commands
// Read and store the details of the go.mod and go.sum files, // because they may change by the 'go mod graph' or 'go list' commands. // If the command fails, the mod stays the same, therefore, don't need to be restored. // Restore the go.mod and go.sum files, to make sure they stay the same as before // running the "go mod graph" command.