LoginSignup
0
0

More than 5 years have passed since last update.

spitsh(Spook in the Shell)で遊ぶ

Last updated at Posted at 2017-12-25

こんにちは、25日目の投稿になります。

今日はPerl 6(nqp)で記述されたOSSであるspitshで遊びたいと思います。
spitshは"Spook"という言語をコンパイルして、シェルスクリプト互換の命令文を出力することができます。このコンパイラはnqpで書かれているようです。

Spitshのインストール

zefを使います:

$ zef install Spit
$ spit eval 'say "hello world"'

dockerのインストール

Synopsis(の一部)の実行

次のコマンドでは、centosの環境において、ncコマンドがインストールされてなかった場合にそれを行うためのSpookプログラムを生成します:

$ spit eval --os=centos  '.install unless Pkg<nc>; ok Cmd<nc>,"nc command exists now"'

実行結果:

BEGIN(){
  e(){ printf %s "$1"; }
  exec 4>/dev/null
  installed(){ yum list installed "$1" >&4 2>&4; }
  install(){ yum install -y $1 >&4 2>&4; }
  exists(){ command -v "$1" >&4; }
  exec 3>&1
  say(){ printf '%s\n' "$1" >&3; }
  note(){ printf '%s\n' "$1" >&2; }
  die(){ note "$1" && kill "-TERM" $$ >&4; }
  ok(){ test "$1" && say "✔ - $2" || die "✘ - $2"; }
}
MAIN(){
  if ! installed nc; then
    install nc
  fi
  ok "$(exists nc && e 1)" 'nc command exists now'
}
BEGIN && MAIN

次は、dockerで実行してみましょう:

$ spit eval --in-docker=centos '.install unless Pkg<nc>; ok Cmd<nc>,"nc command exists now"'

実行結果:

Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
85432449fd0f: Pull complete 
Digest: sha256:3b1a65e9a05f0a77b5e8a698d3359459904c2a354dc3b25ae2e2f5c95f0b3667
Status: Downloaded newer image for centos:latest
Installing pacakges: nc
warning: /var/cache/yum/x86_64/7/base/packages/libpcap-1.5.3-9.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-4.1708.el7.centos.x86_64 (@CentOS)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
✔ - nc command exists now

Synopsis以外のコマンドの実行

興味のある方は/docディレクトリを見てみましょう: https://github.com/spitsh/spitsh/tree/master/doc

まだ未完成な部分が多そうですが今後に期待です。

さいごに

これで、2017年のPerl 6アドベントカレンダーは終わりになります。
ここまで読んでくださった読者の皆様ありがとうございます。
そして、お忙しい中寄稿してくださった @AnaTofuZ さん、@debility さんありがとうございました。

0
0
0

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