我正在创建一个程序,它正在处理和计算open-source个存储库和库的大小,并将数据保存到数据库以供进一步分析。

github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1github.com/\!azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/home/username/dev/glass/tmp/pkg/mod/github.com/\!azure/go-ansiterm@v0.0.0-20210617225240-d185dfc1b5a1gocloc
/home/username/dev/glass/tmp/pkg/mod/github.com/\!azure/go-ansiterm@v0.0.0-20210617225240-d185dfc1b5a1/home/username/dev/glass/tmp/pkg/mod/github.com/\\!azure/go-ansiterm@v0.0.0-20210617225240-d185dfc1b5a1
gocloc

有没有办法解决这个问题?对我来说,一个想法是在我想做的事情上创建一个shell脚本

github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1github.com/\!azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1
func parseUrlToVendorDownloadFormat(input string) string {
    // Split the input string on the first space character
    parts := strings.SplitN(input, " ", 2)
    if len(parts) != 2 {
        return ""
    }

    // Split the package name on the '/' character
    packageNameParts := strings.Split(parts[0], "/")

    // Add the '\!' prefix and lowercase each part of the package name
    for i, part := range packageNameParts {
        if hasUppercase(part) {
            packageNameParts[i] = "\\!" + strings.ToLower(part)
        }
    }

    // Join the modified package name parts with '/' characters
    packageName := strings.Join(packageNameParts, "/")

    return strings.ReplaceAll(packageName+"@"+parts[1], `\\!`, `\!`)
}
/home/username/dev/glass/tmp/pkg/mod/github.com/\!azure/go-ansiterm@v0.0.0-20210617225240-d185dfc1b5a1

传递给该函数:

// Alternative goCloc - command.
func linesOfCode(dir string) (int, error) {
    // Run the `gocloc` command in the specified directory and get the output
    cmd := exec.Command("gocloc", dir)
    output, err := cmd.Output()
    if err != nil {
        return 0, err
    }

    lines, err := parseTotalLines(string(output))
    if err != nil {
        return 0, err
    }

    return lines, nil
}

使用此解析函数的:

// Parse from the GoCloc response.
func parseTotalLines(input string) (int, error) {
    // Split the input string into lines
    lines := strings.Split(input, "\n")

    // Find the line containing the "TOTAL" row
    var totalLine string
    for _, line := range lines {
        if strings.Contains(line, "TOTAL") {
            totalLine = line
            break
        }
    }

    // If the "TOTAL" line was not found, return an error
    if totalLine == "" {
        return 0, fmt.Errorf("could not find TOTAL line in input")
    }

    // Split the "TOTAL" line into fields
    fields := strings.Fields(totalLine)

    // If the "TOTAL" line doesn't have enough fields, return an error
    if len(fields) < 4 {
        return 0, fmt.Errorf("invalid TOTAL line: not enough fields")
    }

    // Get the fourth field (the code column)
    codeStr := fields[3]

    // Remove any commas from the code column
    codeStr = strings.Replace(codeStr, ",", "", -1)

    // Parse the code column as an integer
    code, err := strconv.Atoi(codeStr)
    if err != nil {
        return 0, err
    }

    return code, nil
}

我尝试过的:

  • 使用gocloc作为库,但无法使用。
  • 使用单引号而不是转义,这并没有起到作用,但我认为可能有什么。

解决这个问题的一种方法,可能是创建单独的shell脚本,并将dir作为参数传递给它,然后消除那里的转义,我不知道。。。

enrichWithLibraryData()github.com/\!azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1

有什么想法吗?如何继续?提前感谢!