JanDeDobbeleer / homebrew-oh-my-posh

Homebrew formula for Oh my Posh
14 stars 9 forks source link

Build with GOPROXY #4

Closed gadams999 closed 2 years ago

gadams999 commented 2 years ago

Hi,

Loving oh-my-posh! I've run into an issue that I think is related to how brew manages environment variables in general, but then related to golang.

I work in environments where proxy.golang.org is not available. In those situations I set go env -w GOPROXY=direct and can build local go apps (pulls are done via GitHub directly). It seems that brew doesn't use these settings during build as I get the follow errors (related to blocking the proxy):

==> Installing oh-my-posh from jandedobbeleer/oh-my-posh
==> go build -o=oh-my-posh -ldflags="-X 'main.Version=5.9.0'"
Last 15 lines from /Users/gavinaws/Library/Logs/Homebrew/oh-my-posh/01.go:
image.go:34:2: github.com/golang/freetype@v0.0.0-20170609003504-e2365dfdc4a0: Get "https://proxy.golang.org/github.com/golang/freetype/@v/v0.0.0-20170609003504-e2365dfdc4a0.zip": dial tcp: i/o timeout
...

With my proxy set to direct:

➜  go go env GOPROXY                                                                                                                                         1
direct

I can get oh-my-posh to successfully build.

I'm not familiar with brew's build process or best practices, but could an environment variable check be added to the formula to set GOPROXY if injected? E.g., something like:

➜  HOMEBREW_GOPROXY=direct brew install oh-my-posh

Then that value can set GOPROXY used by the build system? I haven't found any other examples so far for setting up go inside of a formula to modify proxy settings.

If this a sound approach, I can test locally and submit a PR for review.

gadams999 commented 2 years ago

Confirmed adding environment setting in the formula via brew edit correctly builds and works.

  def install
    ENV["GOPROXY"]= "direct"
    Dir.chdir("src") do
      system("go build -o=oh-my-posh -ldflags=\"-X \'main.Version=5.9.0\'\"")
      bin.install "oh-my-posh"
    end
    mv "themes", prefix
  end

I'm not sure the correct Ruby syntax, but what about adding something like _if HOMEBREW_GOPROXY passed in to brew install, set a local GOPROXY env to that value, otherwise, don't set?_

JanDeDobbeleer commented 2 years ago

I'm going to have a look tomorrow, been following along but have no experience in that regard + I'll need to check what homebrew advises in such events.

gadams999 commented 2 years ago

Sounds good. I wonder if this would work (unable to test right now):

ENV["GOPROXY"] = h.has_key?('HOMEBREW_GOPROXY') ? h['HOMEBREW_GOPROXY'] : ''
lewis-yeung commented 2 years ago

@JanDeDobbeleer Adding a support of using user-defined HOMEBREW_GOPROXY env can make it friendly to users who don't have access to proxy.golang.org:

ENV["GOPROXY"] = ENV.has_key?("HOMEBREW_GOPROXY") ? ENV["HOMEBREW_GOPROXY"] : ""

Tested and it worked correctly.

JanDeDobbeleer commented 2 years ago

@lewis-yeung please create a PR! Lost track of this one.

lewis-yeung commented 2 years ago

@JanDeDobbeleer See #9.

lewis-yeung commented 2 years ago

Thank you @JanDeDobbeleer and Thanks to @gadams999 who has already given an almost correct solution. 😄