解决golang import private 仓库的问题 | 您所在的位置:网站首页 › go导入私有仓库 › 解决golang import private 仓库的问题 |
今天花了大概两个多小时时间去搞定一个看起来挺简单的问题:import一个位于private repository上的包。 比如这样: Import “GitHub.ibm.com/fakegroup/fakeproduce/fakepackage” 但是Compile的时候报错了: fatal: could not read Username for 'https://github.ibm.com': terminal prompts disabled OK, 那就enable terminal prompts, 使用命令: env GIT_TERMINAL_PROMPT=1 但是很快发现早go build的时候prompts出让输入username和password的提示被下一条download信息淹没,根本没机会输入,并且在runtime build的时候,不可能在CI/CD中也没法输入这些信息,得另谋出路。 很快发现了另一个办法: git config \ --global \ url."https://${user}:${personal_access_token}@github.com".insteadOf \ "https://github.com"好办法,只要在全局.gitconfig中配置一个replacemeng就可以了, 上面命令运行后效果是这样的: $ cat ~/.gitconfig [url "https://[email protected]:[email protected]"] insteadOf = https://github.ibm.com看起来挺好,但是编译的时候包错: unable to access “https://[email protected]:[email protected]:xxxxx/yyyyyy.git':Port number ended with ‘a’ 这个就扯了,一番搜索。发现了问题: 注意看replaement的格式: ${user}:${personal_access_token}@github.com 最后是有个符号@用于分隔password和repo地址的,好死不死我的用户名是[email protected]这就让git判断错误,认为第一个@之后的就是repo地址,并且错然后后面的符号:跟着的应该是port,所以在看到本应是numeric的所谓port中出现了第一个非数字字符:a就报了这样的错:Port number ended with ‘a’最后是有个符号@用于分隔password和repo地址的,好死不死我的用户名是[email protected]这就让git判断错误,认为第一个@之后的就是repo地址,并且错然后后面的符号:跟着的应该是port,所以在看到本应是numeric的所谓port中出现了第一个非数字字符:a就报了这样的错:Port number ended with ‘a’ 真是无话可说,后来又采用了补救措施,比如用%40替换@,并没有用,也考虑到密码也可能出现一些这样的符号,所以这个办法并不能解决问题。真是无话可说,后来又采用了补救措施,比如用%40替换@,并没有用,也考虑到密码也可能出现一些这样的符号,所以这个办法并不能解决问题。
接着想,既然git clone有https和ssd两种方式,为啥golang在下载依赖的时候不能用ssd呢,这样就不用用户名密码了啊? 还是使用git global config: $ cat ~/.gitconfig [url "[email protected]:"] insteadOf = https://github.ibm.com/因为使用ssd的方式提前已经把public key加入到github的可信key中了,所以不需要user/password 另外在过程中还出现了这样的错: https://sum.golang.org/lookup/bitbucket.org/compay/[email protected]: 410 Gone
但是在复盘中却再也不出现了,不知道具体原因,但是也把解决方案放在这里,以防再次出现: https://medium.com/mabar/today-i-learned-fix-go-get-private-repository-return-error-reading-sum-golang-org-lookup-93058a058dd8 就这样搞定了。
参考: https://medium.com/cloud-native-the-gathering/go-modules-with-private-git-repositories-dfe795068db4 https://stackoverflow.com/questions/10054318/how-do-i-provide-a-username-and-password-when-running-git-clone-gitremote-git https://stackoverflow.com/questions/63629771/go-get-using-ssh-instead-of-https-not-on-github https://gist.github.com/dmitshur/6927554 https://stackoverflow.com/questions/32232655/go-get-results-in-terminal-prompts-disabled-error-for-github-private-repo https://stackoverflow.com/questions/53682247/how-to-point-go-module-dependency-in-go-mod-to-a-latest-commit-in-a-repo https://medium.com/mabar/today-i-learned-fix-go-get-private-repository-return-error-reading-sum-golang-org-lookup-93058a058dd8 https://stackoverflow.com/questions/62974985/go-module-latest-found-but-does-not-contain-package https://stackoverflow.com/questions/45385673/how-to-troubleshoot-a-url-error-with-port-number-ended-with-y https://stackoverflow.com/questions/6172719/escape-character-in-git-proxy-password https://stackoverflow.com/questions/48233797/git-clone-illegal-port-number https://stackoverflow.com/questions/62974985/go-module-latest-found-but-does-not-contain-package https://stackoverflow.com/questions/53682247/how-to-point-go-module-dependency-in-go-mod-to-a-latest-commit-in-a-repo |
CopyRight 2018-2019 实验室设备网 版权所有 |