尋常でないもふもふ

a software engineer blog

プライベートリポジトリをgo getする方法

自分のプライベートリポジトリをgo getすると以下のようなエラーが発生する。

$ go get github.com/jnst/my-private-repo
# cd .; git clone https://github.com/jnst/my-private-repo /Users/jnst/go/src/github.com/jnst/my-private-repo
Cloning into '/Users/jnst/go/src/github.com/jnst/my-private-repo'...
fatal: could not read Username for 'https://github.com': terminal prompts disabled
package github.com/jnst/my-private-repo: exit status 128

原因

普段は GitHubSSH Keys に公開鍵を登録して SSH 認証しているので git@github.com:jnst/my-private-repo.git のような URL を使ってプッシュしたりしてると思うけど、go get の場合は HTTP 経由で行わないとダメらしい。
Personal access tokens を生成して HTTP の URL にくっつけてあげれば OAuth 認証(?)可能になるため、go get も使うことができる。

やり方

https://github.com/settings/tokens

ここから repo 権限にチェックつけて(Organization のリポジトリなら admin:org も必要のはず)トークンを生成してコピーして下記コマンドを実行。

$ git config --global url."https://c61axxxxxxxxxxxxxxx:x-oauth-basic@github.com/".insteadOf "https://github.com/"

c61axxxxxxxxxxxxxxx の部分に生成したトークンを貼っ付ける。
そうすると実行した内容が ~/.gitconfig に追記されて、この状態なら go get github.com/jnst/my-private-repo が可能になっている。

$ go get github.com/jnst/my-private-repo
# cd /Users/jnst/go/src/github.com/jnst/my-private-repo; git show-ref
package github.com/jnst/my-private-repo: exit status 1

参考

Rubyでarray.each_index.injectしたときの括弧は配列展開

こういうやつ

animals = {
  a1: { name: '', type: '哺乳類' },
  a2: { name: '', type: '哺乳類' },
  a3: { name: '蜥蜴', type: '爬虫類' }
}
mammals = animals.reduce([]) do |arr, (id, animal)| # <- ここの丸括弧
  (animal[:type] == '哺乳類') ? (arr << animal) : arr
end

実用例

「プレイヤーID」と「スコア」を持つハッシュがあるとして

player = { id: 1, score: 100 }

あるチームに所属する「プレイヤー毎のスコア」の合計値を独自に算出して「チームのスコア(≒チームの強さ)」を表したい。スコアの計算方法は、Clash of Clan のクランポイント計算と同様に、チーム内で1〜10位は各プレイヤーのスコアの50%、11〜30位は25%といった感じで係数が変わる。
これを Ruby で実装すると配列をイテレートしながら計算するために inject を使うことになるが、プログラムを書く上で配列のインデックスが必要な場合は each_with_index を挟むことになる。

# スコアが高い順にソート
players.sort_by { |player| -player[:score] }
# チーム内で上位にあるほどスコアを高く評価、51位以下は無視
players.each_with_index.inject(0) do |sum, (player, i)|
  sum +=
    case i + 1
    when  1..10 then (score * 0.50).round
    when 11..20 then (score * 0.25).round
    when 21..30 then (score * 0.12).round
    when 31..40 then (score * 0.10).round
    when 41..50 then (score * 0.03).round
    end
end

このとき引数に sum, (player, i) という謎の丸括弧がでてくるが、これは配列を意味する。

players.each_with_index.inject(0) do |sum, arr|
  sum, player = arr

このコードと同じ意味となり、引数内で展開してくれているだけ。

JavaScript でいうところの 分割代入(Destructuring assignment)

Macの環境構築自動化 2016年10月版

概要

Mac の環境構築するときに大半の時間を占めるインストール作業を半自動化する系の話。

  • Homebrew - Mac用パッケージ管理ツール
  • Homebrew Cask - Homebrew を使ってソフトウェアを一括インストールするための拡張
  • Homebrew Bundle - 依存関係を Brewfile に書き出し/読み込みできるツール
  • mas-cli - App Store のアプリを一括インストール

macOS Sierra を入れるタイミングでクリーンインストールしたいので改めて整理した。

歴史的な流れ

  1. 2013年末〜2014年初夏にかけて homebrew-cask と homebrew-bundle による自動化が流行る
  2. 2014年7月に homebrew-bundle が Warning: brew bundle is unsupported と出て使えなくなる
  3. オワコンと叫んだり、シェルスクリプトに退化したり、血迷って Ansible 使う人が増える
  4. 2015年4月に homebrew-brewdler となって帰ってきた
  5. 2015年7月に mas-cli が産声をあげる
  6. 2016年初頭、brewdler は bundle という名前に戻った
  7. 世界に平和が訪れた

構成管理ツールのAnsible は仕事でつかってたけど、わざわざ Mac セットアップのために設定ファイルの書き方思い出したり、調べなおしたりして構築していくのは想像しただけで目眩がする。無駄な苦労を楽しめる人にはいいかもだけど、一般人はついていけない。

これから自動化はじめたい人

brew 自体はすでに入ってるなら tap で homebrew-bundle を使えるようにする

$ brew tap Homebrew/bundle

App Store 経由でインストールしたアプリも管理したいので mas-cli もいれる

$ brew install mas

dump で現在インストールされているパッケージを Brewfile として書き出すことができる

$ brew bundle dump 

ただし、100%全部は書き出せないようなので、/Applications フォルダとにらめっこしながら漏れがないか確認して Brewfile に手動で追記していく。

tap 'caskroom/cask'
tap 'homebrew/bundle'
tap 'homebrew/core'
brew 'go'
brew 'mas'
brew 'rbenv'
cask 'atom'
cask 'docker-toolbox'
cask 'google-chrome'
cask 'google-drive'
cask 'google-japanese-ime'
mas 'Alfred (1.2)', id: 405843582
mas 'Slack (2.0.3)', id: 803453959
mas 'Xcode (8.0)', id: 497799835

brew search chrome とか brew search sourcetree とかすれば記述すべき homebrew-cask 上の正式名称がわかる。AppStore のアプリの場合は、mas list するか、AppStore のアプリのページのリンクをコピーすれば URL に id が含まれてるのでわかる。
完成した Brewfile は自分の GitHub リポジトリの dotfiles にでも置いておけばいい。 dotfiles/Brewfile at master · jnst/dotfiles · GitHub

Mac で環境構築する手順

新品の MacBook を買ったりクリーンインストールしたら、とりあえず Homebrew をインストールする。次に Safari から自分の GitHub リポジトリにアクセスしてクローンなりコピペなりして Brewfile を持ってくる。Brewfile が存在するディレクトリに移動し brew bundle も使えるようにして実行する。

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ git clone git@github.com:jnst/dotfiles.git
$ cd dotfiles
$ brew tap Homebrew/bundle
$ brew bundle

あとはインストールを待つだけ。

最近の homebrew-cask

  • デフォルトで ~/Applications ではなく /Applications にインストールされる
  • パッケージのインストール先が /opt/homebrew-cask/Caskroom から /usr/local/Caskroom に変わった

メンテナンス作業

  • TODO:
    • brew bundle dump --force
    • brew bundle cleanup
    • brew bundle check