第 10 回 kintone コマンドラインツール(cli-kintone v0)をコンパイルしてみよう

著者名: 江田 篤史 (External link)

目次

information

このチュートリアルは ver. 0.x.x の cli-kintone の使い方を紹介しています。
ver.1.0.0 以降の cli-kintone の使い方を紹介するチュートリアルは次のページを参照してください。
はじめよう cli-kintone

はじめに

第 10 回は、さまざまなプラットフォームで動く cli-kintone 実行ファイルの作成方法を紹介します。

cli-kintone の GitHub リポジトリ (External link) では、次の 4 種類のプラットフォーム向けの実行ファイルを配布しています。

  • Windows(64-bit)
  • Windows(32-bit)
  • Linux(64-bit)
  • macOS(64-bit)

この記事で紹介する方法を用いると、それ以外のプラットフォームでも cli-kintone が利用できます。
ここでは、Ubuntu(64-bit)用、CentOS(32-bit)用の cli-kintone 実行ファイルを Windows で作成する例を紹介します。

cli-kintone で利用されている技術

cli-kintone のソースコードは Go 言語 (External link) というプログラミング言語で書かれています。
Go 言語で書かれたプログラムは、ソースコードに対して「コンパイル」という作業することで実行ファイルを作成できますが、この実行ファイルはプラットフォームごとに異なる形式で作成する必要があります。
そのため、Go 言語には「クロスコンパイル」と呼ばれる機能が備わっています。
この機能によって、コンパイルを行う PC とは異なるプラットフォーム上で実行できるファイルを作ることができます。

今回はこのクロスコンパイルを用いて、各種プラットフォーム用の cli-kintone 実行ファイルを作成してみます。

Go 言語の開発環境を整える

それではまず、Go 言語の開発環境を整えていきましょう。
Go 言語の公式サイト (External link) から、Windows 用のインストーラーをダウンロードして実行します。

インストールが完了したら、コマンドプロンプトを起動してバージョンを確認するコマンドを実行します。
次のようにバージョンが表示されていれば正しくインストールされています。

1
2
3
go version

go version go1.8.3 windows/amd64

ソースコードから cli-kintone の実行ファイルを作成する

他のプラットフォーム用実行ファイルを作成する前に、まずは Windows 用の実行ファイルを作成してみましょう。

Git をインストールする

cli-kintone のソースファイルを入手するために、今回は Git を利用します。
インストールしていない場合は、 Git for Windows (External link) からインストーラーをダウンロードして実行しましょう。

インストールが完了したら、コマンドプロンプトを起動してバージョンを確認するコマンドを実行します。
次のようにバージョンが表示されていれば正しくインストールされています。

1
2
3
git --version

git version 2.13.0.windows.1

実行ファイルの作成に必要なコードを取得する

次に、cli-kintone 実行ファイルの作成に必要なコードを、Go 言語の機能を使って取得します。
以下の 4 つのコマンドを実行しましょう。

1
2
3
4
go get github.com/kintone-labs/go-kintone
go get github.com/howeyc/gopass
go get golang.org/x/text/encoding
go get github.com/jessevdk/go-flags

cli-kintone のソースコードを取得する

ここまで準備ができたら、GitHub から cli-kintone のソースコードを取得します。
今回は、環境変数 GOPATH で指定されているディレクトリー中の src ディレクトリーでソースコードを取得するので、環境変数を確認するコマンドを実行して GOPATH の内容を確認してみましょう。

1
2
3
go env GOPATH

C:\Users\owner\go

内容が確認できたら、GOPATH ディレクトリーの中の「src」に移動します。

1
cd C:\Users\owner\go\src

そこで、Git のクローンコマンドを実行して、cli-kintone のソースコードを取得しましょう。

1
2
3
4
5
6
7
git clone https://github.com/kintone/cli-kintone.git

Cloning into 'cli-kintone'...
remote: Counting objects: 246, done.
remote: Total 246 (delta 0), reused 0 (delta 0), pack-reus         ed 246
Receiving objects: 100% (246/246), 72.06 KiB | 0 bytes/s,          done.
Resolving deltas: 100% (133/133), done.

ソースコードの取得が完了したら、「src」ディレクトリーの中に 3 つのディレクトリーができていることを確認します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
dir

 ドライブ C のボリューム ラベルは ********** です
 ボリューム シリアル番号は ********** です

 C:\Users\owner\go\src のディレクトリ

2017/07/06  17:18    <DIR>          .
2017/07/06  17:18    <DIR>          ..
2017/07/06  17:33    <DIR>          cli-kintone
2017/07/06  17:11    <DIR>          github.com
2017/07/06  17:11    <DIR>          golang.org
               0 個のファイル                   0 バイト
               5 個のディレクトリ  ********** バイトの空き領域

実行ファイルを作成する

ここまでで実行ファイルを作成する準備が整いました。

cli-kintone のソースコードを取得したディレクトリーに移って、ビルドコマンドを実行します。

1
2
cd cli-kintone
go build

コマンドの実行が終了すると、ビルドコマンドを実行したディレクトリーの下に Windows 用の実行ファイル「cli-kintone」が作成されています。

cli-kintone のコマンドを実行して動作確認してみましょう。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
cli-kintone

Usage:
  cli-kintone [OPTIONS]

Application Options:
  -d=            Domain name (specify the FQDN)
  -a=            App ID (default: 0)
  -u=            User's log in name
  -p=            User's password
  -t=            API token
  -g=            Guest Space ID (default: 0)
  -o=            Output format. Specify either 'json' or 'csv' (default: csv)
  -e=            Character encoding (default: utf-8).
                 Only support the encoding below both field code and data
                 itself:
                 'utf-8', 'utf-16', 'utf-16be-with-signature',
                 'utf-16le-with-signature', 'sjis' or 'euc-jp' (default: utf-8)
  -U=            Basic authentication user name
  -P=            Basic authentication password
  -q=            Query string
  -c=            Fields to export (comma separated). Specify the field code name
  -f=            Input file path
  -b=            Attachment file directory
  -D             Delete records before insert. You can specify the deleting
                 record condition by option "-q"
  -l=            Position index of data in the input file (default: 1)
      --import   Import data from stdin. If "-f" is also specified, data is
                 imported from the file instead
      --export   Export kintone data to stdout
  -v, --version  Version of cli-kintone

Help Options:
  -h, --help     Show this help message

以上が、cli-kintone 実行ファイルをソースコードから作成する方法でした。

クロスコンパイルを行う

ここからは、Windows 以外のプラットフォーム用実行ファイルの作成に入ります。
実行ファイルの動作確認は、もう 1 台別の PC を用意するか、あるいは仮想マシンを用意して行います。
今回はクロスコンパイルの例として、64-bit の Ubuntu 用実行ファイルと、32-bit の CentOS 用実行ファイルを作成してみましょう。

STEP1:CPU アーキテクチャの確認

まず、クロスコンパイルする際に CPU アーキテクチャを指定する必要があるので、あらかじめ調べておきます。

linux の場合、CPU アーキテクチャは arch コマンドで確認できます。

1
2
3
arch

x86_64

STEP2:クロスコンパイル

次に、cli-kintone を実行する OS を環境変数の GOOS で、同じく環境変数の GOARCH で CPU アーキテクチャを指定して、実行ファイルを作成します。
指定できる GOOSGOARCH の組み合わせは以下のとおりです。

GOOS GOARCH
darwin 386
darwin amd64
freebsd 386
freebsd amd64
freebsd arm
linux 386
linux amd64
linux arm
netbsd 386
netbsd amd64
netbsd arm
openbsd 386
openbsd amd64
plan9 386
plan9 amd64
windows 386
windows amd64

GOARCH の値は、以下の CPU アーキテクチャを指しています。

GOARCH CPUアーキテクチャ
amd64 x86_64
386 x86_32
arm ARM_32

アーキテクチャが確認できたところで、実行ファイルの作成に移りましょう。

実行ファイルの作成は、下記 3 つのコマンドで行います。

1
2
3
set GOOS={OS}
set GOARCH={CPUアーキテクチャ}
go build
Ubuntu 64-bit 用の実行ファイルの作成

まずはじめに、Ubuntu 64-bit 用の実行ファイルを作成します。
それぞれの環境変数に以下の値を設定して、ビルドコマンドを実行します。

  • GOOS:linux
  • GOARCH:amd64
1
2
3
set GOOS=linux
set GOARCH=amd64
go build -o cli-kintone-ubuntu-64bit

コマンドの実行が終わると「cli-kintone-Ubuntu-64bit」というファイルができます。
これが Ubuntu 64-bit 用の実行ファイルです。

これは linux 用の実行ファイルなので、当然ですが Windows 上では動きません。
念のため、実行して確かめてみましょう。

1
2
3
4
./cli-kintone-ubuntu-64bit

'cli-kintone-ubuntu-64bit' は、内部コマンドまたは外部コマンド、
操作可能なプログラムまたはバッチ ファイルとして認識されていません。
CentOS6 32-bit 用の実行ファイルの作成

続いて、CentOS6 32-bit 用の実行ファイルも作成してみましょう。

それぞれの環境変数に以下の値を設定して、ビルドコマンドを実行します。

  • GOOS:linux
  • GOARCH:386
1
2
3
set GOOS=linux
set GOARCH=386
go build -o cli-kintone-centos-32bit

「cli-kintone-centos-32bit」というファイルが作成されているのを確認できたでしょうか。

STEP3:作成した実行ファイルの動作確認

最後に、作成した実行ファイルの動作確認を行いましょう。
以下では、Ubuntu 64-bit 用の実行ファイルを例として扱いますが、CentOS 用実行ファイルも同様の手順で確認できます。

それでは、先ほど作成した実行ファイル「cli-kintone-Ubuntu-64bit」を Ubuntu 上に配置して実行してみましょう。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
./cli-kintone-ubuntu-64bit

Usage:
  cli-kintone [OPTIONS]

Application Options:
  -d=            Domain name (specify the FQDN)
  -a=            App ID (default: 0)
  -u=            User's log in name
  -p=            User's password
  -t=            API token
  -g=            Guest Space ID (default: 0)
  -o=            Output format. Specify either 'json' or 'csv' (default: csv)
  -e=            Character encoding (default: utf-8).
                 Only support the encoding below both field code and data
                 itself:
                 'utf-8', 'utf-16', 'utf-16be-with-signature',
                 'utf-16le-with-signature', 'sjis' or 'euc-jp' (default: utf-8)
  -U=            Basic authentication user name
  -P=            Basic authentication password
  -q=            Query string
  -c=            Fields to export (comma separated). Specify the field code name
  -f=            Input file path
  -b=            Attachment file directory
  -D             Delete records before insert. You can specify the deleting
                 record condition by option "-q"
  -l=            Position index of data in the input file (default: 1)
      --import   Import data from stdin. If "-f" is also specified, data is
                 imported from the file instead
      --export   Export kintone data to stdout
  -v, --version  Version of cli-kintone

Help Options:
  -h, --help     Show this help message

無事実行できたでしょうか。
今度は CSV ファイルへのエクスポートを行ってみましょう。

1
2
3
4
5
./cli-kintone-ubuntu-64bit --export -a **** -d **** -U **** -P **** -t **** > ./sample.csv
cat sample.csv "$id","$revision","タイトル","本文","画像"

"2","2","タイトル2","本文2本文2本文2本文2本文2本文2本文2本文2本文2本文2","image2.jpg"
"1","2","タイトル1","本文1本文1本文1本文1本文1本文1本文1本文1本文1本文1","image2.jpg"

ご覧のとおり、クロスコンパイルで作成した実行ファイルでも、これまで紹介してきた機能が使えます。

これで、所有しているサーバー上でも cli-kintone を使うことができそうですね。

補足:仮想マシンの作成方法

仮想マシンの作成は、Vagrant と VirtualBox を用いると比較的簡単に行えます。
今回は、これらを用いて Ubuntu 環境を構築する方法を例として紹介します。

まず、Vagrant と VirtualBox のインストーラーをそれぞれ次のサイトからダウンロードし、インストールします。

インストールが済んだら、一度 PC を再起動します。

再起動したら、仮想マシン用のディレクトリーを作成します。
C ドライブ直下に「Vagrant」ディレクトリー、その中に「Ubuntu」ディレクトリーを作成しましょう。
ディレクトリーが作成できたら、コマンドプロンプトでそのディレクトリーに移動します。

1
2
3
4
5
mkdir C:\Vagrant
cd C:\Vagrant

mkdir ubuntu
cd ubuntu

上記ディレクトリーの準備ができたら、利用したいプラットフォームの Box ファイル(仮想マシンのひな型)を Vagrant クラウド (External link) で探します。
Provider は「virtualbox」を選択し、キーワードは「Ubuntu」で検索してみましょう。

「Ubuntu/trusty64」という Ubuntu 64-bit の box をクリックします。

「How to use this box with Vagrant:」と書いてある下に「New」というタブがあります。
このタブをクリックしたときに表示されるコマンドを、上から順に実行します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# コマンド1
vagrant init ubuntu/trusty64

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

# コマンド2
vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
~~~~~~~~~~

~~~~~~~~~~
    default: /vagrant => C:/Vagrant/ubuntu

多少時間がかかりますが、ここまでが仮想マシン構築作業です。

次に、先ほど作成した実行ファイル「cli-kintone-Ubuntu-64bit」を仮想マシン内に配置します。
Vagrant には、Windows と仮想マシンの間で使えるファイル共有のしくみがあるので、これを利用します。

Windows 上の Vagrantfile が置かれているディレクトリーと、仮想マシン上の「/vagrant」が共有ディレクトリーになっています。
まずは、Windows 上からディレクトリーの内容を確認してみましょう。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
dir /a-d

 ドライブ C のボリューム ラベルは TI31289800A です
 ボリューム シリアル番号は D05D-CECF です

 C:\Vagrant\ubuntu のディレクトリ

2017/07/06  17:33         6,614,466 cli-kintone-ubuntu-64bit
2017/07/21  18:21             3,092 Vagrantfile
               2 個のファイル           6,617,558 バイト
               0 個のディレクトリ  15,168,487,424 バイトの空き領域

Windows 上でファイルが確認できたら、仮想マシンにログインするコマンドを実行します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
vagrant ssh

Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-123-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Fri Jul 21 09:33:25 UTC 2017

  System load:  0.9               Processes:           80
  Usage of /:   3.6% of 39.34GB   Users logged in:     0
  Memory usage: 25%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.

New release '16.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

仮想マシンにログインできたら、ディレクトリー「vagrant」に移動して cli-kintone の実行ファイルがあることを確認しましょう。

1
2
3
4
cd /vagrant
ls

cli-kintone-ubuntu-64bit  Vagrantfile

実行ファイルが確認できたら、上記 作成した実行ファイルの動作確認 で紹介した方法で動作確認を行ってみてください。

更新履歴

  • 2019/05/23 追記
    Version 0.9.1 以降のバージョンで、cli-kintone 実行ファイルの作成に go-flags が必要となりました。
information

この Tips は、cli-kintone Ver 0.10.2 と 2020 年 3 月版 kintone で動作を確認しています。