从Gradle Exec任务运行cURL以从GitHub API获取文件,在终端中按预期运行时返回404

juzqafwq  于 2023-05-18  发布在  Git
关注(0)|答案(1)|浏览(137)

我有以下Gradle Exec任务,使用经典的个人访问令牌(启用所有repo范围)从GitHub下载文件:

tasks.register<Exec>("downloadScript") {

    val cmdLine = mutableListOf<String>("curl", "-H", "'Authorization: Bearer ${gh_token}'", "-H", "'Accept: application/vnd.github.raw'", "--remote-name", "-L", "https://api.github.com/repos/my_org/my_repo/contents/scripts/script.py")

    commandLine(cmdLine)
}

当我运行这个任务时,我得到了这样的输出:

> Task :downloadScript FAILED
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0   123    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (22) The requested URL returned error: 404

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':downloadScript'.
> Process 'command 'curl'' finished with non-zero exit value 22

这里最有趣的部分是,在终端(IntelliJ本身或MacOS终端)中运行相同的命令通过,文件被下载,同时将${gh_token}替换为真实的的令牌。curl -H 'Authorization: Bearer token_string' -H 'Accept: application/vnd.github.raw' --remote-name -L https://api.github.com/repos/my_org/my_repo/contents/scripts/script.py
为什么使用变量将字符串传递给commandline()会导致命令行插值失败,导致它失败?

stszievb

stszievb1#

对于任何人到达这里,我已经找到了解决方案,这是非常简单的。
AuthorizationAccept头文件周围的'[header]'(撇号)会扰乱commandLine()的插值。
一旦我删除它们,一切都如预期的那样工作。
这是正确的任务:

tasks.register<Exec>("downloadScript") {

    val cmdLine = mutableListOf<String>("curl", "-H", "Authorization: Bearer ${gh_token}", "-H", "Accept: application/vnd.github.raw", "--remote-name", "-L", "https://api.github.com/repos/my_org/my_repo/contents/scripts/script.py")

    commandLine(cmdLine)
}

相关问题