需要一种方法将Docker参数中的单个字符串转换为多个参数。

huangapple go评论91阅读模式
英文:

Need a way to convert a single string in docker args to many args

问题

我编写了一个小的 Golang 程序,从 GitHub PR 中获取 N 个参数(包含 K8s 的 resourceQuota 请求的文件),并将请求的 CPU 和内存总量写入一个文件,然后在下一步中将其作为 GHA 输出导出。

由于在我们的自托管运行程序中遇到了一个问题,无法执行 go run main.go /path/to/file1 /path/to/file2,所以我不得不将其容器化。

我对这些都很陌生,所以我的同事告诉我从一个自制的 GitHub action 导入 docker 程序,当只有一个文件在 PR 中更改时(只有一个参数需要处理)时,一切都很顺利。

问题是当传递了两个或更多的参数时,因为我使用的 action:tj-actions/changed-files 会输出一个包含所有文件名的字符串,我真的不知道如何解决这个问题。

这是我调用自制 action 的方式:

      - name: Capture request values actions
        uses: ./goCapacityCheck
        with:
          files: ${{ steps.changed-files.outputs.all_changed_files }}

goCapacityCheck action.yml

name: 'goCapacityCheck'
description: '从 PR 中获取所有文件的请求 CPU 和内存值'
inputs:
  files:
    description: '在 PR 中更改的文件。'
    required: false

runs:
  using: 'docker'
  image: './Dockerfile'
  args:
    - ${{ inputs.files }}

有没有办法在传递给 action、Docker 或其他地方时拆分该字符串?

当我遇到这个问题时,我还没有尝试过太多,但我期望这个看起来像 "/path/to/file1 /path/to/file2" 的字符串在某个时刻被拆分,以便能够执行 docker run --name mygocap gocap /path/to/file1 /path/to/file2

英文:

I coded a small Golang program, that takes N arguments (files with resourceQuota requests for K8s) from a GitHub PR, and writes a file with the total amount of CPU and Memory requested that is then exported as GHA output in the next step.

can't do go run main.go /path/to/file1 /path/to/file2 because Apparently i hit a bug in actions/setup-go with our self-hosted runner, so i had to containerise that.

I'm new to all of these, so my colleagues told me to import the docker program from a self-made GitHub action, all works like a charm when only 1 file is changed in the PR (only 1 arg to handle).

Problem is when 2 or more args are passed, because the action im using: tj-actions/changed-files outputs a single string with all the files names and i'm really clueless on how to work around it.

this is how i call the self-made action:

      - name: Capture request values actions
        uses: ./goCapacityCheck
        with:
          files: ${{ steps.changed-files.outputs.all_changed_files }}

goCapacityCheck action.yml

name: 'goCapacityCheck'
description: 'Capture requests CPU and Memory values from all files in the PR'
inputs:
  files:
    description: 'files changed in the PR.'
    required: false

runs:
  using: 'docker'
  image: './Dockerfile'
  args:
    - ${{ inputs.files }}

is there a way to split that string when passing to the action? or to Docker or something?

I haven't tried much when i hit this issue, but i'd expect that the sting that looks like "/path/to/file1 /path/to/file2" to be split at some point in order to be able to do
docker run --name mygocap gocap /path/to/file1 /path/to/file2

答案1

得分: 1

我不会直接翻译代码,但我可以解释一下代码的功能。这段代码是一个Go程序,主要实现了以下功能:

  1. 通过os.Args[]获取命令行参数,并调用getFileNames()函数来处理参数,将处理后的结果保存在filenames变量中。
  2. 遍历filenames,对每个文件进行处理(这部分代码没有给出具体实现),并打印文件名。
  3. getFileNames()函数接受一个字符串切片作为参数,将其中的文件名提取出来,并返回一个字符串切片。

希望这能帮到你!如果你有其他问题,请随时提问。

英文:

I ended up having to handle it inside the go program because i would never now how many arguments would come in, so from os.Args[] i called getFileNames() and then i did the rest of the routine with filenames instead of os.Args[1:] directly.

func main() {
	filenames := getFileNames(os.Args[1:]) {
	for _, file := range filenames {
		// handling of the file data
		fmt.Println(file)
}
    
func getFileNames(s []string) []string {
	var filenames []string
	for _, file := range s {
		if strings.Contains(file, " ") {
			filenames = append(filenames, strings.Split(file, " ")...)
		} else {
			filenames = append(filenames, file)
		}
	}
	return filenames
}

答案2

得分: 0

你可以尝试使用一些bash技巧来拆分字符串,例如:

string="/path/to/file1 /path/to/file2"
echo "$(echo $string | cut -d' ' -f1)" "$(echo $string | cut -d' ' -f2)"

将会输出

"/path/to/file1" "/path/to/file2"

所以你可以在GitHub action的工作流中尝试类似这样的操作:

      - name: run
        run: |
          string=${{ steps.changed-files.outputs.all_changed_files }}
          first=$(echo $string | cut -d' ' -f1)
          second=$(echo $string | cut -d' ' -f2)
          docker run --name mygocap gocap $first $second

英文:

You could try to split with some bash tricks, as example:


string="/path/to/file1 /path/to/file2"
echo \"$(echo $string | cut -d' ' -f1)\" \"$(echo $string | cut -d' ' -f2)\"

will print

"/path/to/file1" "/path/to/file2"

So you could try something like this in a GitHub action workflow:

      - name: run
        run: |
          string=${{ steps.changed-files.outputs.all_changed_files }}
          first=$(echo $string | cut -d' ' -f1)
          second=$(echo $string | cut -d' ' -f2)
          docker run --name mygocap gocap $first $second

huangapple
  • 本文由 发表于 2022年11月8日 21:44:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/74361756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定