9
9

More than 5 years have passed since last update.

go-latestを使ってみた

Last updated at Posted at 2015-04-10

@deeeetさんが開発して話題のgo-latestを早速使ってみた.
go-latestはGitHubのタグなどと比較して,使用中のツールが最新版かチェックすることができる.
今回はGo製Sensu CLIのohgiに入れて使ってみた.

go-latestの使い方

go getでインストールして,ohgiのversionコマンドに組み込んでみた.
main.goからはohgi.Version("v0.1.0")のように呼び出す.

ohgi/ohgi/version.go
package ohgi

import (
    "fmt"
    "github.com/tcnksm/go-latest"
)

func Version(version string) string {
    var result []byte
    result = append(result, fmt.Sprintf("ohgi version %s\n", version)...)

    fixFunc := latest.DeleteFrontV()
    githubTag := &latest.GithubTag{
        Owner:             "hico-horiuchi",
        Repository:        "ohgi",
        FixVersionStrFunc: fixFunc,
    }

    res, _ := latest.Check(githubTag, fixFunc(version))
    if res.Outdated {
        result = append(result, fmt.Sprintf("Latest version of ohgi is %s, please update it\n", res.Current)...)
    }

    return string(result)
}

ここでは,latest.GithubTagを使い,OwnerRepositoryを指定してCheck()を実行する.
バージョンが古い場合は,res.Outdatedtrueになる.
(使用バージョンの方が新しいと,res.Newtrueになるようだ.)
res.Currentに最新のバージョンが文字列で格納されている.

便利なFixVersionStrFunc

バージョンの形式が0.1.0ではなくv0.1.0の場合のために,関数が用意されているようだ.
標準では何もしないが,latest.DeleteFrontV()を使うことで,頭のvを取り除ける.
必要なら,これをlatest.GithubTagFixVersionFuncに渡してやると良いようだ.

おわりに

go-latestを使うことで,バージョンが古い場合は以下のように通知が出せるようになった.
今後,Go言語製のツールでGitHubで管理するものは,go-latestが必須になる気がする.
(更新が必要な場合は,GitHubのURLを表示すると良いかも.)

$ ohgi version
ohgi version 0.1.4
Latest version of ohgi is 0.1.5, please update it
9
9
1

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
9
9