<p>Hello.</p>
<p>So I've been looking at using <a href="https://github.com/src-d/go-git" rel="nofollow">go-git</a> and can't clone my repo, because I need authorisation as well. So, there's the function "git.PlainClone" which takes a <a href="https://godoc.org/gopkg.in/src-d/go-git.v4#CloneOptions" rel="nofollow">"git.CloneOptions"</a> type struct. This "CloneOptions" has the property "Auth" and it expects a <a href="https://godoc.org/gopkg.in/src-d/go-git.v4/plumbing/transport#AuthMethod" rel="nofollow">"transport.AuthMethod"</a>. I just can't seem to be able to figure out what that really means. How to I correctly pass anything here? The method "Name()" isn't even implemented. Should I do that? I'm just lost... thanks.</p>
<p>EDIT: Solution found. Leaving this here for posterity and maybe even for my forgetful future self. I was unable (just too lazy) to find the implementation of the interface. One example can be seen <a href="https://github.com/src-d/go-git/blob/master/plumbing/transport/http/common.go#L175" rel="nofollow">here</a>. So, just basically <code>bauth = NewBasicAuth("uname","secret")</code> and pass that by way of <code>Auth: http.bauth</code> (since http is a part of the <code>transport</code> package and implements <code>AuthMethod</code> interface thus conforming to <code>transport.AuthMethod</code>)</p>
<hr/>**评论:**<br/><br/>Sythe2o0: <pre><p>You give it a struct with a <code>String() string</code> function (what's behind <code>fmt.Stringer</code>) and a <code>Name() string</code> function, and that struct implicitly satisfies the <code>AuthMethod</code> interface. </p></pre>blackcomb-pc: <pre><p>I'm not sure I follow you. So, I have to create some <code>type myauth struct</code> for which I implement the <code>Stringer()</code> and <code>Name()</code> functions? And then I'd create a variable of the type <code>myauth</code> and then I'd pass that? I mean there's no <code>Name()</code> implementation anywhere. And what would I write anyway, like <code>return "ssh"</code>? Totally confused. I guess I don't understand what to pass here:</p>
<pre><code> _, err := git.PlainClone("git/repo", false, &git.CloneOptions{
URL: "muh.git",
// Auth: transport.AuthMethod, <----- ???
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
Progress: os.Stdout,
})
</code></pre></pre>Sythe2o0: <pre><p>OK I dug through files and can be a bit more helpful.</p>
<p>Auth is finally used in places like <a href="https://github.com/src-d/go-git/blob/v4/plumbing/transport/git/common.go#L22" rel="nofollow">this</a>. Git does not support ANY auth, is what it is saying, so it wants you to not populate that field. </p>
<p>HTTP on the other hand expects you to pass in its own private <code>AuthMethod</code> interface, like <a href="https://github.com/src-d/go-git/blob/v4/plumbing/transport/http/common.go#L158" rel="nofollow">BasicAuth</a></p></pre>blackcomb-pc: <pre><p>Thanks for the enlightening me! That is exactly what I needed to see. Will edit the post.</p></pre>LimEJET: <pre><p>The ssh package has <a href="https://godoc.org/golang.org/x/crypto/ssh#AuthMethod" rel="nofollow">a set of <code>AuthMethod</code>s that you can use</a>. Try passing one of those.</p></pre>kemitche: <pre><p>That's a distinct interface from the transport.AuthMethod in OP's question, unfortunately.</p></pre>LimEJET: <pre><p>That seems odd, since git works over ssh. You sure? I can't really check on my phone.</p></pre>kemitche: <pre><p>Yup, I'm sure. OPs linked interface has exported methods, your linked interface only has unexported methods.</p></pre>LimEJET: <pre><p>But aren't they in the same package? Or is it a different <code>transport</code>?</p></pre>kemitche: <pre><p>Different. OP is looking at <code>github.com/src-d/go-git/blob/master/plumbing/transport.AuthMethod</code>, you're looking at <code>golang.org/x/crypto/ssh.AuthMethod</code></p></pre>blackcomb-pc: <pre><p>That's right. Still trying to get used to orient myself within the source file layout of a typical go project. Thanks for the input tho!</p></pre>blackcomb-pc: <pre><p>Thanks, man, but I figured it out. See Post edit.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传