gistyのセットアップ
gistへの投稿を便利してくれるらしい、とのことなのでセットアップしてみたという話。
セットアップ手順
https://github.com/swdyh/gisty のREADMEにあるとおりにやれば良いみたい。
やってみた
gem インストール
% gem install gisty Fetching: gisty-0.2.3.gem (100%) Successfully installed gisty-0.2.3 1 gem installed Installing ri documentation for gisty-0.2.3... Building YARD (yri) index for gisty-0.2.3... Installing RDoc documentation for gisty-0.2.3…
作業ディレクトリの指定
自分の場合は、以下を追記
% vim ~/.zshenv export GISTY_DIR="$HOME/work/gists"
Github API アクセストークンの取得
gistyのREADMEにあるように https://swdyh-gisty.heroku.com/ を利用して取得できるらしいけれども、 今回はGithub APIを使うためのaccess tokenを取得するで書いたように自分でやってみた。
scopesはgistを指定すれば良いっぽい。
% curl -u 'username' -d '{"scopes":["gist"],"note":"gisty"}' https://api.github.com/authorizations Enter host password for user 'username': # パスワードの入力 { "app": { "name": "gisty (API)", "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api" }, "scopes": [ "gist" ], "created_at": "2012-09-30T13:34:34Z", "note_url": null, "note": "gisty", "token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "updated_at": "2012-09-30T13:34:34Z", "id": xxxxxx, "url": "https://api.github.com/authorizations/xxxxxx" }
※ username, token, id, urlはブログ掲載にあたり書き換えている。
取得後、https://github.com/settings/applications を確認してみると、Authorized applicationsに"gisty (API) create gists"が追加されていた。
Github API アクセストークンの指定
取得したアクセストークンを以下のように追記
% vim ~/.zshenv export GISTY_ACCESS_TOKEN="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
gistyを使ってgistへ投稿
適当なファイルを作って投稿してみた。
% vim helloworld.rb
% cat helloworld.rb
puts "Hello World!!"
% gisty post helloworld.rb https://gist.github.com/3811198 Cloning into '3811198'... remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0) Receiving objects: 100% (3/3), done.
投稿すると、投稿したファイルのURL( https://gist.github.com/3811198 )がブラウザで表示されたのと、 投稿したgistyの作業ディレクトリにgit cloneされた。
% tree -a work/gists work/gists └── 3811198 ├── .git │ ├── HEAD │ ├── config │ ├── description │ ├── hooks │ │ ├── applypatch-msg.sample │ │ ├── commit-msg.sample │ │ ├── post-update.sample │ │ ├── pre-applypatch.sample │ │ ├── pre-commit.sample │ │ ├── pre-rebase.sample │ │ ├── prepare-commit-msg.sample │ │ └── update.sample │ ├── index │ ├── info │ │ └── exclude │ ├── logs │ │ ├── HEAD │ │ └── refs │ │ ├── heads │ │ │ └── master │ │ └── remotes │ │ └── origin │ │ └── HEAD │ ├── objects │ │ ├── info │ │ └── pack │ │ ├── pack-4597eadd62b3b8fa63a93cb59a15b8e669f4fc9d.idx │ │ └── pack-4597eadd62b3b8fa63a93cb59a15b8e669f4fc9d.pack │ ├── packed-refs │ └── refs │ ├── heads │ │ └── master │ ├── remotes │ │ └── origin │ │ └── HEAD │ └── tags └── helloworld.rb 17 directories, 22 files
ついでに
はてなブログのgist記法を試してみた。
[gist:3811198]
と書くと
のように表示される。
Github APIを使うためのaccess tokenを取得する
本来であればOAuth認証でaccess tokenを取得するべきなのだろうけれども、HTTPSのBasic認証でも取得できるらしいのでやってみた。
access tokenを取得する
https://help.github.com/articles/creating-an-oauth-token-for-command-line-use
上記URLの通りに叩くだけ
$ curl -u 'username' -d '{"scopes":["repo"],"note":"Help example"}' https://api.github.com/authorizations
scopesのところは http://developer.github.com/v3/oauth/#scopes を見つつ、やりたいことに応じて指定する。
取得してみた
% curl -u 'username' -d '{"scopes":["repo"],"note":"Help example"}' https://api.github.com/authorizations Enter host password for user 'username': # usernameに対するパスワードを入力 { "app": { "name": "Help example (API)", "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api" }, "scopes": [ "repo" ], "created_at": "2012-09-30T14:02:09Z", "note_url": null, "note": "Help example", "token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "updated_at": "2012-09-30T14:02:09Z", "id": 000000, "url": "https://api.github.com/authorizations/000000" }
※ username、token、id、urlはブログ掲載にあたり書き換えている。
あと、https://github.com/settings/applications にアクセスすると、Authorized applicationsに"Help example (API) access private repos"が追加されている。
access tokenを使ってのAPIを叩いてみる
リクエスト時のheaderのAuthorizationで指定してあげればいいらしい。
% curl -H "Authorization: token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" https://api.github.com/user # 結果は省略
リクエストパラメータで渡すこともできるらしい。
% curl "https://api.github.com/user?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # 結果は省略
はまったこと
% curl -H "Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" https://api.github.com/user { "message": "Bad credentials" }
"Bad credentials"と言われて、??となったのだけれども、Authorization:のあとに"token"が抜けていた。
ssh port forwading memo
ssh port forwadingでゴニョゴニョしようとして、なんだかよくわからなくなったので、まとめてみた。
そもそも許可されていないとできない AllowTcpForwarding yes
sshd_config
AllowTcpForwarding yes
LとRの違い
参考:技術的雑談-sshのportForwadingってなあに? - Tsubasa's HomePage
- -Lでは、sshを使った側のサーバに転送用のポートができる
- -Rでは、sshdのある側のサーバに転送用のポートができる
L
host2# ssh -l hoge -L 8880:host1:80 host1
- host2側でnetstat -aすると、127.0.0.1:8880がlistenになっている
- host1側は特に変化無し
R
host2# ssh -l hoge -R 8880:host1:80 host1
- host2側は特に変化無し。
- host1側でnetstat -aすると、127.0.0.1:8880がlistenになっている。
127.0.0.1でしかlistenしないの? => GatewayPorts yes (ポート中継の許可) で0.0.0.0のlistenになる
Nとかfと一緒に使うと嬉しいかも。
-N
SSH (1)
リモートコマンドを実行しません。これはポート転送のみをおこないたい場合に便利です (プロトコル バージョン 2 のみ)。
-f
SSH (1)
ssh がコマンドを実行する直前に、バックグラウンドに移行するよう指示します。これはssh にパスワードあるいはパスフレーズを入力する必要はあるものの、そのコマンド自体はバックグラウンドで実行させたいときに便利です。これは-n オプションも含んでいます。リモートサイトで X11 プログラムを起動させる場合には、
ssh -fhostxterm
などとやるのがおすすめです。
多段転送
参考:SSH/多段ポートフォワードでSSH接続 - yanor.net/wiki
例)host3:80 -> host2:81 -> host1:82
host1の82番にアクセスすることで、host3の80番にアクセスできる。
host3# ssh -t -R 81:localhost:80 host2 ssh -t -R 82:localhost:81 host1
コメントを入れただけなのにコンパイルエラーとなった
何が起きたの?
とあるコンパイルが通るScalaプログラムに、コメントを入れてコンパイルし直したらコンパイルが通らなくなった。
具体例
コメントなし
これはコンパイルできる。
object HelloWorld { def greeting { println("HelloWorld") } }
scala> :load ./HelloWorld.scala Loading ./HelloWorld.scala... defined module HelloWorld scala> HelloWorld.greeting HelloWorld
コメントあり
コメントを入れただけなのにコンパイルエラーとなった。
object HelloWorld { def greeting { println("HelloWorld") // し } }
scala> :load ./HelloWorld.scala Loading ./HelloWorld.scala... <script>:4: error: Missing closing brace `}' assumed here } ^
調べてみたら
ちなみにmac環境にて日本語を文字化けさせずに実行するには
export JAVA_OPTS="-Dfile.encoding=UTF-8"
が必要っぽい。
Hello! Scala!!|Happy Tuning
エンコーディングの問題みたいですね。
対策と結果
環境変数を設定して、再度試みると問題なくコンパイルできた。
% export JAVA_OPTS="-Dfile.encoding=UTF-8" % scala Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22). Type in expressions to have them evaluated. Type :help for more information. scala> :load ./HelloWorld.scala Loading ./HelloWorld.scala... defined module HelloWorld scala> HelloWorld.greeting HelloWorld
廃止予定警告 warning: there were deprecation warnings; re-run with -deprecation for details
Scalaをいじっていたら、以下のような警告が出た時のお話。
warning: there were deprecation warnings; re-run with -deprecation for details
こんな感じに警告が出ました。
scala> List(1,2,3).sort(_<_).first warning: there were deprecation warnings; re-run with -deprecation for details res0: Int = 3
調べてみると・・・
廃止予定のメソッドを今更使っちゃダメだよ〜と怒られているみたい。
一般的論として、Scala 2.7 コレクションの古い機能はそのまま残っているはずだ。機能の中には廃止予定となったものもあり、それは今後のリリースで撤廃されるということだ。Scala 2.8 でそのような機能を使ったコードをコンパイルすると廃止予定警告 (deprecation warning) が発生する。その意味や性能特性を変えて 2.8 に残った演算もあり、その場合は廃止予定にするのは無理だった。このような場合は 2.8 でコンパイルすると移行警告 (migration warning) が出される。コードをどう変えればいいのかも提案してくれる完全な廃止予定警告と移行警告を得るには、-deprecation と -Xmigration フラグを scalac に渡す (-Xmigration は X で始まるため、拡張オプションであることに注意)。同じオプションを scala REPL に渡すことで対話セッション上で警告を得ることができる。
Scala 2.8 コレクション API -- Scala 2.7 からの移行
とりあえず、オプション付けて起動してみた
$ scala -deprecation -Xmigration Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_21). Type in expressions to have them evaluated. Type :help for more information.
で、先ほど怒られたやつをもう一度実行してみる
scala> List(1,2,3).sort(_>_).first <console>:6: warning: method first in trait IterableLike is deprecated: use `head' instead List(1,2,3).sort(_>_).first ^ <console>:6: warning: method sort in class List is deprecated: use `sortWith' instead List(1,2,3).sort(_>_).first ^ res0: Int = 3
どうやら、firstではなくhead、sortではなくsortWithを使えと。
headを使うと・・・
scala> List(1,2,3).sort(_>_).head <console>:6: warning: method sort in class List is deprecated: use `sortWith' instead List(1,2,3).sort(_>_).head ^ res1: Int = 3
sortWithも使うと・・・
scala> List(1,2,3).sortWith(_>_).head res2: Int = 3
というわけで、無事に警告が消えた。
HelloWorld
さっそく、HelloWorldを表示したいところだけれども、まずは、動かす環境を作らなければ動かない。。。
Scala 実行環境構築
と言っても、Javaは既に入っているし、Macの場合、MacPortsでインストールできるみたい。
参考:MacPorts で Scala 2.8 のインストール - etc9
Java バージョン確認
% java -version java version "1.6.0_22" Java(TM) SE Runtime Environment (build 1.6.0_22-b04-307-10M3261) Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03-307, mixed mode)
Scalaインストール
% sudo port install scala28 省略 % scala-2.8 -version Scala code runner version 2.8.1.final -- Copyright 2002-2010, LAMP/EPF
ScalaでHello World
・シェルで対話的に実行する
・スクリプトを記述してインタプリタで実行する
・プログラムを記述してコンパイルして実行する
ので、3通りの方法でやってみた。
シェルで対話的に実行
% scala-2.8 Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22). Type in expressions to have them evaluated. Type :help for more information. scala> println("Hello World!!") Hello World!!
スクリプトを記述してインタプリタで実行
% cat HelloWorld.scala println("Hello World") % scala-2.8 HelloWorld.scala Hello World
プログラムを記述してコンパイルして実行
% cat HelloWorldMain.scala object Main{ def main(args:Array[String]) { println("Hello World!!") } } % scalac-2.8 HelloWorldMain.scala % ls HelloWorld.scala HelloWorldMain.scala Main$.class Main.class % scala-2.8 Main Hello World!!
感想
まずは、第1歩。
Rubyのirbのように対話的に確認できる方法があるのはかなり嬉しい。
*1:[asin:4774144363:title][asin:4774144363:detail]
railsでhello world
画面にHelloWorldを表示する
コントローラの作成
script generateで作成
$ ./script/generate controller Hello exists app/controllers/ exists app/helpers/ create app/views/hello exists test/functional/ create test/unit/helpers/ create app/controllers/hello_controller.rb create test/functional/hello_controller_test.rb create app/helpers/hello_helper.rb create test/unit/helpers/hello_helper_test.rb
indexメソッドの追加
とりあえず、メッソドの中身は空で。
$ vim app/controllers/hello_controller.rb
class HelloController < ApplicationController def index end end
コントローラのindexメッソドに対応するビューの作成
$ vim app/views/hello/index.html.erb
Hello World
動かして確認
$ ./script/server
下記、URLにアクセスしたら、
http://192.168.1.24:3000/Hello
こんな感じのメッセージがでた。
We're sorry, but something went wrong. We've been notified about this issue and we'll take a look at it shortly.
ターミナル上には、以下のエラーメッセージ
$ ./script/server => Booting WEBrick => Rails 2.3.5 application starting on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server [2010-04-07 02:24:37] INFO WEBrick 1.3.1 [2010-04-07 02:24:37] INFO ruby 1.8.7 (2010-01-10) [x86_64-linux] [2010-04-07 02:24:37] INFO WEBrick::HTTPServer#start: pid=3023 port=3000 /!\ FAILSAFE /!\ Wed Apr 07 02:24:40 +0900 2010 Status: 500 Internal Server Error no such file to load -- sqlite3 /usr/local/ruby/1.8.7/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' 省略 /usr/local/ruby/1.8.7/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' ./script/server:3 [2010-04-07 02:25:20] INFO going to shutdown ... [2010-04-07 02:25:20] INFO WEBrick::HTTPServer#start done. Exiting
どうやら、sqlite3がないらしい
sqlite3-rubyのインストール
$ sudo gem install sqlite3-ruby Building native extensions. This could take a while... ERROR: Error installing sqlite3-ruby: ERROR: Failed to build gem native extension. /usr/local/ruby/1.8.7/bin/ruby extconf.rb checking for fdatasync() in -lrt... yes checking for sqlite3.h... no *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. 省略
sqlite-develがなく、sqlite3-rubyが入らないみたいなので、sqlite-develをyumでいれる。
$ sudo yum install sqlite-devel $ sudo gem install sqlite3-ruby
改めて確認
$ ./script/server
HelloWorld
表示された
railsのscript consoleでno such file to load -- readlineと怒られた
状況
scrip consoleを使おうとしたら以下の感じで怒られた
$ ./script/console Loading development environment (Rails 2.3.5) /usr/local/ruby/1.8.7/lib/ruby/1.8/irb/completion.rb:10:in `require': no such file to load -- readline (LoadError) from /usr/local/ruby/1.8.7/lib/ruby/1.8/irb/completion.rb:10 from /usr/local/ruby/1.8.7/lib/ruby/1.8/irb/init.rb:254:in `require' from /usr/local/ruby/1.8.7/lib/ruby/1.8/irb/init.rb:254:in `load_modules' from /usr/local/ruby/1.8.7/lib/ruby/1.8/irb/init.rb:252:in `each' from /usr/local/ruby/1.8.7/lib/ruby/1.8/irb/init.rb:252:in `load_modules' from /usr/local/ruby/1.8.7/lib/ruby/1.8/irb/init.rb:21:in `setup' from /usr/local/ruby/1.8.7/lib/ruby/1.8/irb.rb:54:in `start' from /usr/local/ruby/1.8.7/bin/irb:13
対応
rubyをインストールする時にreadline-develが無かったため、readlineが使えない状態みたい。
ので、readline-develを入れて、使えるようにしてあげる。
※ 以下の~/install/ruby-1.8.7-p249はインストールの時に使ったディレクトリ
$ sudo yum install readline-devel $ cd ~/install/ruby-1.8.7-p249/ext/readline/ $ ruby extconf.rb $ make $ sudo make install
確認
無事に動作
$ ./script/console Loading development environment (Rails 2.3.5) >>
rehashについて
とあるサイトを見ていたら、rehashというコマンドが使われていて、何なのか知らなかったので調べてみた。
rehash ハッシュテーブルを再構築する。csh・tcsh の内部コマンド
UNIXの部屋 コマンド検索:rehash (*BSD/Linux)
なんで、今までお世話にならなかったのだろうと考えたら、メインで使っているシェルがbashだからだと納得した。
cshで実際にやってみた
さて、せっかく知ったので、いくつか試してみた。
シェルの変更とPATH確認
まずは、シェルをcshに変更して、PATHの確認
$ csh $ env | grep PATH PATH=/usr/local/ruby/1.8.7/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/ユーザ名/bin
PATHに/home/ユーザ名/binが含まれていたので、そこで試してみる事にする。
~/binの作成
~/bin が無かったので、作成
$ cd ~/ $ mkdir bin
スクリプトの作成の実行権限の付与
実行すると文字列が出力されるスクリプトを作成
$ vim bin/test1
#!/bin/sh echo "hello"
実行権限の付与
$ chmod 755 bin/test1
絶対パスを指定して実行
絶対パスを指定して実行するとハッシュテーブルを使わないらしいので、まずは、スクリプトの動作確認をかねて動かしてみる
$ ~/bin/test1 hello
実行された
rehashする前と後の実行結果
$ test1 test1: コマンドが見つかりません.
$ rehash $ test1 hello
ハッシュテーブルを更新しないと実行できない事が確認できた。
名前を変えてみると
$ mv bin/test1 bin/test2 $ test1 test1: コマンドが見つかりません. $ test2 test2: コマンドが見つかりません. $ ~/bin/test2 hello $ rehash $ test1 test1: コマンドが見つかりません. $ test2 hello
rehashが必要ですね。
bashでやってみた
bashだとハッシュテーブルは使わずに、毎回PATHを見に行くとの事なので、そっちもやってみた。
bashに変更
$ bash
実行してみる
$ test2 hello
名前を変更して実行してみる
$ mv bin/test2 bin/test3 $ test2 -bash: /home/hirai/bin/test2: そのようなファイルやディレクトリはありません $ test3 hello