Google Groupes

Re: Local repository for go modules


thepud...@gmail.com 16 août 2018 16:04
Envoyé au groupe : golang-nuts
   "There is no VCS, only a tree in my own file system."

Hi Ignazio,

I think you could go down the Athens path if you wanted to, but I don't think you need to do so.

I suspect the 'replace' directive I described in my earlier post in this thread might be sufficient for what you describe, because it lets you map from an import path like "example.com/me/foo" to something on your local filesystem.

Here is a simple example. This is a 'hello' module that imports a trivial 'goodbye' module, both of which reside on my local filesystem. Neither is checked in to a VCS. The 'hello' module also imports "rsc.io/quote" (just to show a normal import grabbed from the Internet).

Here is the file structure on my local system, all outside of GOPATH:

    /tmp/playground/hello
    |-- go.mod
    `-- hello.go
    /tmp/playground/goodbye
    |-- go.mod
    `-- goodbye.go

Here is the 'go.mod' file for the main package showing a sample use of the 'replace' directive to translate an import path of "example.com/me/goodbye" to a relative filesystem path on my local computer:

    ==> /tmp/playground/hello/go.mod <==
    

    require (
     example.com/me/goodbye v0.0.0
     rsc.io/quote v1.5.2
    )

    replace example.com/me/goodbye => ../goodbye

That's the most interesting bit. 

That 'go.mod' also happens shows a require for "rsc.io/quote" because I happened to use that as well, where that code is obtained behind the scenes from GitHub.

And to round out the example, here are the rest of the files:

    ==> /tmp/playground/hello/hello.go <==
    package main

    import (
     "fmt"

     "rsc.io/quote"
    )

    func main() {
     fmt.Println(quote.Hello())
     fmt.Println(goodbye.Goodbye())
    }

    
    ==> /tmp/playground/goodbye/go.mod <==


    ==> /tmp/playground/goodbye/goodbye.go <==
    package goodbye

    func  Goodbye() string {
          return "Goodbye"
    }

And running it from  /tmp/playground/hello/hello.go shows:

    $ go run .
   Hello, world.
   Goodbye

Hope that helps, or is at least food for thought,
--thepudds


On Thursday, August 16, 2018 at 5:41:01 PM UTC-4, Ignazio Di Napoli wrote:
On Thursday, August 16, 2018 at 8:20:10 PM UTC+2, thepud...@gmail.com wrote:
Could you say a few more words about your use case?

Thank you. Looking at design docs, I think Athens can do what I'm looking for, but maybe it is a little "too much", and either docs are incomplete or I'm unable to find them (e.g.: how do I configure the proxy to tell where to find private modules?).

Let me explain my needs better.
I have developed a fairly big library I can't/don't want to publish. It could be mapped in several modules, maybe twenty. 
There is no VCS, only a tree in my own file system.

Now, I want the programs I develop to became module based and use those private modules along with other public modules.

So my idea was: I put my modules as they where in non-existant example.com.
Then, configure a proxy so that if host is example.com it returns the local file. If it is from any other url, get the remote file (optionally using a proxy). 
Is my idea correct? Has anyone already configured something like it?

Another solution could be to run a local webserver serving the files, then configure hosts file adding `127.0.0.1 example.com`, and configure a proxy exception. 
But I though a more specific solution was available (like, in effect, Athens).

Thank you.