0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

go get対象がgithub.comにある時に、新パッケージへのリダイレクトを自動追跡してgo getする

Last updated at Posted at 2019-04-23

こんなシェル関数を.bashrcなりに設定して読み込んで使えばOK

function go-get {
  local opts=" "
  local pkg=""

  while true
  do
    if (( $# > 1 ))
    then
      opts="${opts}${1} "
      shift
    elif (( $# == 1 ))
    then
      pkg=${1}
      shift
    else
      break
    fi
  done

  cmd="go get${opts}$(curl -Ls -o /dev/null -w %{url_effective} https://${pkg} | sed -e 's/https:\/\///g')"
  echo ${cmd}
  eval ${cmd}
}

curlの処理は

  • -L でリダイレクトを追跡
  • 取得結果は不要なので、 -o /dev/null で捨てて、
  • -w %{url_effective} の部分でリダイレクト先のURL表示を指定

という事をしている。このcurlの処理結果が https://<指定したパッケージ> という文字列になるので、この文字列からsed等でhttps://を除去すれば新パッケージが取得できる。

使用例

github.com/go-delve/delve は旧パッケージパスが github.com/derekparker/delve だった。ブラウザでアクセスしたらリダイレクトしてくれるが、go get で旧パッケージパスを指定するとエラーになる

$ go get -u github.com/derekparker/delve
can't load package: package github.com/derekparker/delve (以下略

上記シェル関数を使った場合、その新パッケージに差し替えてgo getする ※オプションやパッケージの指定の仕方はgo getと同じでOK

$ go-get -u github.com/derekparker/delve
go get -u github.com/go-delve/delve
: (以下略

See

linux - Get url after curl is redirected - Stack Overflow

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?