LoginSignup
0
0

More than 1 year has passed since last update.

ruby yes no memo

Last updated at Posted at 2021-12-11

yes no 対話処理

puts <<-"EOS"
----------------------------
ChefCluster: XXXX
AddUserName: AAA
             BBB
             CCC
----------------------------

Do you want to run with the above settings? [y|n]
EOS

case gets.chomp
   when "yes", "YES", "y"
     puts "exec scipt"
   when "no", "NO", "n"
     puts "no exec script"
     exit 1
   else
     puts "abort"
     exit 1
end
puts <<-EOS
----------------------------
ChefCluster: XXXX
AddUserName: AAA
             BBB
             CCC
----------------------------

Do you want to run with the above settings?
EOS

---------  ChefCluster  ----------
XXXX
----------------------------------

---------  AddUserList  ----------
AAA
BBB
CCC 
----------------------------------

Do you want to run with the above settings?
*********  ChefCluster  **********
XXXX
**********************************

*********  AddUserList  **********
AAA
BBB
CCC 
**********************************

Do you want to run with the above settings?
*********  ChefCluster  **********
XXXX
**********************************

*********  AddUserList  **********
AAA
BBB
CCC 
**********************************

Do you want to run with the above settings?
---------------  User Add Setting --------------- 
[ChefCluster]
 XXXX

[Organization]
chef-server

[UserName]
AAA
BBB
CCC
------------------------------------------------

Do you want to run with the above settings?

ファイルを読み込む

user_list = IO.readlines("adduserlist", chomp: true)

user_list.each do |user|
  puts user
end

外部コマンドの実行

shellout

chefがないと使えない。cookbookのレシピでつかうときはこっちがいいらしい
https://qiita.com/sawanoboly/items/43bc9e15739355f9c533
https://github.com/chef/mixlib-shellout/tree/master

  require 'mixlib/shellout'
  find = Mixlib::ShellOut.new("find . -name '*.rb'")
  find.run_command

  puts find.stdout
  puts "error messages" + find.stderr

  # Raise an exception if it didn't exit with 0
  find.error!

Open3.capture3を使ってみよう。遠い昔使った気もする

https://docs.ruby-lang.org/ja/latest/method/Open3/m/capture3.html
https://www.orzs.tech/open3-ruby/
https://thr3a.hatenablog.com/entry/20190525/1558725876
```
require "open3"

result, err, status = Open3.capture3("date")
p result # -> "2021年 1月 14日 木曜日 10:49:33 JST\n"
p err # -> ""
p status # -> #
```

qiita api

token取得

jqがほしい for mac

brew install jq

apiをテストする

token=トークンをセット
userid=ユーザIDをセット
curl -H "Authorization: Bearer $token" "https://qiita.com/api/v2/users/$userid" | jq

httpリクエストfaraday

https://nekorails.hatenablog.com/entry/2018/09/28/152745
https://gist.github.com/mitukiii/2775321
https://straitwalk.hatenablog.com/entry/2014/05/11/014150
https://lostisland.github.io/faraday/usage/
https://qiita.com/kazuyat/items/e8a24c6d76e5c922f630
https://blog.apitore.com/2017/02/27/ruby-api-call-faraday/
```

[12:10:53 ~ ]$ vim Gemfile
source 'https://rubygems.org'
gem 'faraday'
gem 'faraday_middleware'

[12:11:38 ~ ]$ bundle install
Resolving dependencies...
Using bundler 1.17.2
Using multipart-post 2.1.1
Following files may not be writable, so sudo is needed:
/Library/Ruby/Gems/2.6.0
/Library/Ruby/Gems/2.6.0/build_info
/Library/Ruby/Gems/2.6.0/cache
/Library/Ruby/Gems/2.6.0/doc
/Library/Ruby/Gems/2.6.0/extensions
/Library/Ruby/Gems/2.6.0/gems
/Library/Ruby/Gems/2.6.0/specifications
Using ruby2_keywords 0.0.2
Using faraday 1.1.0
Bundle complete! 1 Gemfile dependency, 4 gems now installed.
Use bundle info [gemname] to see where a bundled gem is installed.
```

テスト

require 'faraday
がいる
- ヘッダーの指定
- tokenの指定ぐらいをしたい

例外処理

option設定

https://maku77.github.io/ruby/io/optparse.html
https://qiita.com/sonots/items/1b44ed3a770ef790a63d
https://docs.ruby-lang.org/ja/latest/class/OptionParser.html
https://portaltan.hatenablog.com/entry/2015/07/15/172325

.vimrc

" setting
"文字コードをUFT-8に設定
set fenc=utf-8
" バックアップファイルを作らない
set nobackup
" スワップファイルを作らない
set noswapfile
" 編集中のファイルが変更されたら自動で読み直す
set autoread
" バッファが編集中でもその他のファイルを開けるように
set hidden
" 入力中のコマンドをステータスに表示する
set showcmd


" 見た目系
" 行番号を表示
set number
" 現在の行を強調表示
set cursorline
" 現在の行を強調表示(縦)
set cursorcolumn
" 行末の1文字先までカーソルを移動できるように
set virtualedit=onemore
" インデントはスマートインデント
set smartindent
" ビープ音を可視化
set visualbell
" 括弧入力時の対応する括弧を表示
set showmatch
" ステータスラインを常に表示
set laststatus=2
" コマンドラインの補完
set wildmode=list:longest
" 折り返し時に表示行単位での移動できるようにする
nnoremap j gj
nnoremap k gk
" シンタックスハイライトの有効化
syntax enable


" Tab系
" 不可視文字を可視化(タブが「▸-」と表示される)
" Tab文字を半角スペースにする
set expandtab
" 行頭以外のTab文字の表示幅(スペースいくつ分)
set tabstop=2
" 行頭でのTab文字の表示幅
set shiftwidth=2


" 検索系
" 検索文字列が小文字の場合は大文字小文字を区別なく検索する
set ignorecase
" 検索文字列に大文字が含まれている場合は区別して検索する
set smartcase
" 検索文字列入力時に順次対象文字列にヒットさせる
set incsearch
" 検索時に最後まで行ったら最初に戻る
set wrapscan
" 検索語をハイライト表示
set hlsearch
" ESC連打でハイライト解除
nmap <Esc><Esc> :nohlsearch<CR><Esc>

pryをやってみる

https://qiita.com/kotazi_/items/2c32731ad14bfec331b0

sudo gem install pry pry-do

文字化けが改善できず諦める。faradayは動いていることはわかった
https://qiita.com/kidach1/items/d0137d96bed9ac381fd5
https://qiita.com/sy250f/items/0d14d4411725ffb90b6d

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