第 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で動作を確認しています。