Home
Slides
Blog
slide.seike460.com
Home
Slides
Blog
Home
Slides
Fukuoka LT 2019
Fukuoka LT 2019
Fukuoka LT 2019
2019年12月20日
General
LT
Testing
CI/CD
Keyboard Shortcuts
←
→
Navigate slides
Space
Next slide
F
Fullscreen
ESC
Exit fullscreen
Home
First slide
End
Last slide
B
.
Pause
S
Speaker notes
?
Reveal.js help
Close
## Goで公開するOSSツール Fukuoka.LT 株式会社Fusic  清家史郎(@seike460) 
# Who? ###### Fusic Co., Ltd.  ###### 清家史郎 ([@seike460](https://twitter.com/seike460))  ###### Evangelist / Engineer ###### FullStack Backend (PHP、Go)
# 今回お話する内容 - GoでのCLI実装 - goroutine - CI/CD - 公開手順
## GoでCLIを作成する
# GoでCLIを作成する 2つのCLIを作成しました - ディレクトリ高速探索 - ディレクトリのみを高速に出力 - AWS S3 CLI のリッチ版 - 定常的に行うS3のオペレーションを便利にする
# ディレクトリ高速探索 GitHubのリポジトリに移動したい -> pecoを駆使して実施! ``` $ alias g= 'cd ~/src/github.com/$(find ~/src/github.com -mindepth 2 -maxdepth 2 -type d | sed "s/\/Users\/seike460\/src\/github.com\///g" | peco)' ```
# ディレクトリ高速探索 このディレクトリ配下のあそこに移動したい -> pecoを駆使して実施! ``` $ alias cdf='cd $(find . -type d | peco)' ``` ファイルがいっぱいある場所で行うと少し時間がかかる このマルチコア時代に1CPUでファイル探してるのがナンセンス
###### Gorutineだ!!!!!  ## shunkanidou
# goroutine ``` var writer = os.Stdout var cpus = runtime.NumCPU() func main() { runtime.GOMAXPROCS(cpus) dirwalk(string(".")) } ``` ``` func dirwalk(dir string) { files, err := ioutil.ReadDir(dir) if err != nil { log.Fatal(err) } ch := make(chan int, cpus) wg := sync.WaitGroup{} for _, file := range files { if file.IsDir() { wg.Add(1) go deepDirwalk(dir, file, &wg, ch) } } wg.Wait() } func deepDirwalk(dir string, file os.FileInfo, wg *sync.WaitGroup, ch chan int) { defer wg.Done() ch <- 1 path := filepath.Join(dir, file.Name()) fmt.Fprintln(writer, path) dirwalk(path) <-ch } ```
# 25万ファイルで試す - time find . -type d > /dev/null - 62.10 real - 1.67 user - 30.52 sys - time shunkanidou > /dev/null - 23.41 real - 36.20 user - 137.08 sys find 62秒 shunkanidou 23秒 爆速!!!!
# CPU使用率 ###### find . -type d  ###### shunkanidou  ~~公開はやめておこうと思いました~~
## s3ry
# s3ry AWSのStorageであるS3の操作を行うツール ~~こっちは~~ 効果が高そうだと思って公開することにしました  公開するならある程度テストしたい
# Goにおけるテスト xxx_test.goというファイルを作成してテストを記述 go test 【テスト対象のファイル】 を実行するとテストが実行される 僕は[Watch](https://github.com/eaburns/Watch)コマンドを利用して、 常にテストを回しながら実行しています ``` $ Watch -t go test ./... -v ``` ファイルが更新される度にテストが走る
## CI/CD
# CI/CD - CircleCI ``` general: artifacts: - "coverage.out" jobs: build: docker: - image: circleci/golang:1.9 working_directory: /go/src/github.com/【アカウント】/【リポジトリ】 steps: - checkout - run: go get -u golang.org/x/lint/golint github.com/golang/dep/cmd/dep github.com/haya14busa/goverage - run: golint ./... - run: go vet ./... - run: dep ensure - run: goverage -v -coverprofile=coverage.out ./... - run: go tool cover -html coverage.out -o coverage.html - store_artifacts: path: coverage.out destination: coverage.out - store_artifacts: path: coverage.html destination: coverage.html ``` - golint - コードのコーディングスタイルの問題を検出する go vetとは、goのコードを静的解析し - go vet - goのコードを静的解析し、問題を検出
GitHubにコミットしたタイミングでCircleCIが動作 
カバレッジを取ってくれる 
## 公開手順 - クロスコンパイル - GitHub と brewに公開
# クロスコンパイル 通常は`GOOS=xxx` `GOARCH=xxx`指定してBuildします $ GOOS=darwin GOARCH=amd64 go build 全てコンパイルするのは手間なので、[goxc](https://github.com/laher/goxc) を利用しています ``` $ go get github.com/laher/goxc $ goxc [goxc:clean-destination] 2019/12/09 16:45:45 Task clean-destination succeeded [goxc:xc] 2019/12/09 16:45:45 Parallelizing xc for 6 platforms, using max 7 of 8 processors [goxc:xc] 2019/12/09 16:46:15 Task xc succeeded ~省略~ [goxc:archive-zip] 2019/12/09 16:46:16 Artifact(s) archived to /Users/seike460/src/github.com/seike460/s3ry/dist/snapshot/s3ry_darwin_amd64.zip [goxc:archive-zip] 2019/12/09 16:46:16 Artifact(s) archived to /Users/seike460/src/github.com/seike460/s3ry/dist/snapshot/s3ry_windows_amd64.zip [goxc:archive-zip] 2019/12/09 16:46:16 Artifact(s) archived to /Users/seike460/src/github.com/seike460/s3ry/dist/snapshot/s3ry_linux_amd64.zip [goxc:archive-zip] 2019/12/09 16:46:16 Task archive-zip succeeded [goxc:rmbin] 2019/12/09 16:46:16 Task rmbin succeeded ``` ``` seike460@seike460-fmbp s3ry % ll dist/snapshot/ total 84256 -rw-r--r-- 1 seike460 staff 7152989 12 9 16:46 s3ry_linux_amd64.zip -rw-r--r-- 1 seike460 staff 7046341 12 9 16:46 s3ry_windows_amd64.zip -rw-r--r-- 1 seike460 staff 7105704 12 9 16:46 s3ry_darwin_amd64.zip -rw-r--r-- 1 seike460 staff 6877762 12 9 16:46 s3ry_darwin_386.zip -rw-r--r-- 1 seike460 staff 6753685 12 9 16:46 s3ry_windows_386.zip -rw-r--r-- 1 seike460 staff 6901613 12 9 16:46 s3ry_linux_386.zip ```
# GitHubにアップロード Githubへのアップロードを行ってくれる [ghr](https://github.com/tcnksm/ghr) を利用しています ``` $ ghr タグ dist/snapshot/ ``` 0.1.3をアップロードする場合 ``` $ ghr 0.1.3 dist/snapshot/ ```
# brewにてインストール出来るように - [Formula Cookbook](https://github.com/Homebrew/brew/blob/master/docs/Formula-Cookbook.md) ``` $ brew create 【実際のダウンロードパス】 ``` ``` # Documentation: https://docs.brew.sh/Formula-Cookbook # https://rubydoc.brew.sh/Formula # PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST! class S3ry < Formula desc "Amazon S3 CLI Tool by using promptui" homepage "https://godoc.org/github.com/seike460/s3ry" url "https://github.com/seike460/s3ry/releases/download/0.1.2/s3ry_darwin_amd64.zip" sha256 "3f290d732f06cb6d0f7e29bbd62b1cb7594efe6bd47d8a156ddc7915f0e6d1e7" # depends_on "cmake" => :build def install ┆ # ENV.deparallelize # if your formula fails when building in parallel ┆ # Remove unrecognized options if warned by configure ┆ system "./configure", "--disable-debug", ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ "--disable-dependency-tracking", ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ "--disable-silent-rules", ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ "--prefix=#{prefix}" ┆ # system "cmake", ".", *std_cmake_args end test do ┆ # `test do` will create, run in and delete a temporary directory. ┆ # ┆ # This test will fail and we won't accept that! For Homebrew/homebrew-core ┆ # this will need to be a test that verifies the functionality of the ┆ # software. Run the test with `brew test s3ry`. Options passed ┆ # to `brew install` such as `--HEAD` also need to be provided to `brew test`. ┆ # ┆ # The installed folder is not in the path, so use the entire path to any ┆ # executables being tested: `system "#{bin}/program", "do", "something"`. ┆ system "false" end end ``` ``` Please run `brew audit --new-formula s3ry` before submitting, thanks. ```
# おもむろに brew audit --new-formula ``` s3ry: \* C: 1: col 1: Please remove default template comments \* C: 3: col 1: Please remove default template comments \* C: 9: col 3: Commented-out dependency "cmake" =\> :build \* C: 12: col 5: Please remove default template comments \* C: 13: col 5: Please remove default template comments \* C: 18: col 5: Please remove default template comments \* C: 19: col 30: Please remove default template comments \* https://github.com/seike460/s3ry/releases/download/0.1.3/s3ry\_darwin\_amd64.zip looks like a binary package, not a source archive. The `core` tap is source-only. \* GitHub repository not notable enough (\<30 forks, \<30 watchers and \<75 stars) Error: 9 problems in 1 formula detected ``` デフォルトコメント削除してくれって言われてる GitHubでそんなにfolkされてないし、人気もないよって言われてる
[How to Create and Maintain a Tap](https://github.com/Homebrew/brew/blob/master/docs/How-to-Create-and-Maintain-a-Tap.md) Tapsを利用して公開する事が出来ます [github.com/seike460/homebrew-s3ry](https://github.com/seike460/homebrew-s3ry/blob/master/s3ry.rb) ``` class S3ry < Formula desc "Amazon S3 CLI Tool by using promptui" homepage "https://godoc.org/github.com/seike460/s3ry" if Hardware::CPU.is_64_bit? url "https://github.com/seike460/s3ry/releases/download/0.1.2/s3ry_darwin_amd64.zip" sha256 "3f290d732f06cb6d0f7e29bbd62b1cb7594efe6bd47d8a156ddc7915f0e6d1e7" else url "https://github.com/seike460/s3ry/releases/download/0.1.2/s3ry_darwin_386.zip" sha256 "f3123b256aec5d1c375b0d1f1da80f824d471131ad581e6e0a00a5645eb934a4" end version "0.1.2" def install bin.install 's3ry' end test do system "true" end end ``` ``` $ brew tap seike460/s3ry $ brew install s3ry ```
# まとめ - Goは gorutineを利用することで、並列プログラムを実行できる - 発行しすぎると、逆に遅くなることも - CI環境も CircleCIを利用して無料でOSSをCI - brewを使えば、ユーザーフレンドリーな形でダウンロード可能
We are Hiring! Thank you!  https://fusic.github.io
Swipe to navigate
Previous
Next
Related Slides
kixs vol.8
2019/10/26
View
ちびでべろっぱぁ〜ず #11
2019/8/3
View
Life Tech #0
2019/7/27
View