{"action":"created","comment":{"url":"https://api.github.com/repos/wurde/script/pulls/comments/47258042","id":47258042,"diff_hunk":"@@ -0,0 +1,178 @@\n+require 'fileutils'\n+require 'open3'\n+\n+\n+module Script\n+  class Handler #:nodoc:\n+    # TODO: fix search matching against subdir files (ie templates)\n+    def search(regexp: nil, quiet: false)\n+      scripts = list_all\n+      scripts = filter(scripts, regexp) if regexp\n+      scripts = descriptors(scripts) if scripts\n+      pretty_print(scripts) unless quiet\n+      scripts\n+    end\n+\n+    def create!(script)\n+      script = set_default_ext(script, DEFAULT_EXT).first\n+      fail \"#{script} already exists.\" if script_exist?(script)\n+      header = build_header(script)\n+      category = ask_category\n+      write_header(script, category, header)\n+      fetch(script)\n+      commit_changes(\"Create script #{script}\")\n+    end\n+\n+    def fetch(*scripts)\n+      scripts = set_default_ext(scripts, DEFAULT_EXT)\n+      scripts.collect! do |script|\n+        search(regexp: Regexp.new(/^#{script}/i), quiet: true).first\n+      end\n+      copy_to_dir(scripts, Dir.pwd)\n+    end\n+\n+    def clean\n+      scripts = open_scripts\n+      scripts.each { |s| touch(s[:path]) }\n+      scripts.each do |s|\n+        move_to_dir(File.join(DESKTOP, s[:filename]), s[:path])\n+      end\n+      commit_changes\n+    end\n+\n+    def sync\n+      commit_changes\n+      pull_upstream\n+      sync_origin\n+    end\n+\n+    def history\n+      puts \"History of recent script changes...\"\n+      out, error = Open3.capture3(%(git diff --name-status \"@{7 days ago}\" \"@{0 days ago}\"))\n+      puts out\n+    end\n+\n+    private\n+\n+    def list_all\n+      Dir[\"#{File.join(SCRIPT_LIB, 'script')}/**/*\"].select { |f| File.file?(f) }\n+    end\n+\n+    def filter(scripts, regexp)\n+      regexp = Regexp.new(regexp) unless regexp.is_a? Regexp\n+      scripts.select! { |c| regexp.match(File.basename(c)) }\n+    end\n+\n+    def descriptors(scripts)\n+      scripts.collect! do |s|\n+        file = File.open(s)\n+        head = file.readlines[0..11].join('')\n+        file.close\n+\n+        script = {}\n+        script[:path] = s\n+        script[:filename] = File.basename(s)\n+        description = /description:(?<desc>.*)/i.match(head)\n+        script[:description] = description[:desc] if description\n+        script\n+      end\n+    end\n+\n+    def pretty_print(scripts)\n+      return if scripts.empty? || scripts.nil?\n+      longest_filename = scripts.max_by { |x| x[:filename].length }\n+      spacing = longest_filename[:filename].length\n+      scripts.each do |script|\n+        printf \"%-#{spacing}s %s\\n\", script[:filename], script[:description]\n+      end\n+    end\n+\n+    def set_default_ext(*files, extension)\n+      files.flatten.collect! do |file|\n+        file += extension if File.extname(file).empty?\n+        file\n+      end\n+    end\n+\n+    def copy_to_dir(*scripts, dir)\n+      scripts.flatten.each do |script|\n+        FileUtils.cp script[:path], dir\n+      end\n+    end\n+\n+    def script_exist?(script)\n+      script += '.rb' unless File.extname(script) == '.rb'\n+      pattern = Regexp.new(/^#{script}/i)\n+      results = search(regexp: pattern, quiet: true)\n+      results.size > 0\n+    end\n+\n+    def build_header(script)\n+      description = ask 'Describe this script: '\n+      description ||= '...'\n+      header = BOILERPLATE.gsub('$1', script)\n+      header = header.gsub('$2', Time.now.strftime('%Y %m%d %H%M%S'))\n+      header = header.gsub('$3', Time.now.strftime('%Y %m%d %H%M%S'))\n+      header = header.gsub('$4', description)\n+      header\n+    end\n+\n+    def write_header(script, category, header)\n+      dir = File.join(SCRIPT_LIB, 'script', category)\n+      f = File.open(File.join(dir, script), 'w+')\n+      f.write header\n+      f.close\n+    end\n+\n+    def commit_changes(message = nil)\n+      message = ask 'Enter a commit message:' if message.nil?\n+      Dir.chdir SCRIPT_ROOT\n+      `git add -A`\n+      Open3.capture3(%(git commit -m \"#{message}\"))\n+    end\n+\n+    def ask_category\n+      puts CATEGORIES.join(', ')\n+      category = ask 'Which application category does this script belong to?'\n+      category ||= ''\n+      until CATEGORIES.include?(category)\n+        category = gets.strip\n+        break if CATEGORIES.include?(category)\n+        puts \"Error: '#{category}' is not one of the available categories.\"","path":"lib/script/handler.rb","position":141,"original_position":141,"commit_id":"dad09b30a1aa6ce93434fbe76236a561e14cd61c","original_commit_id":"dad09b30a1aa6ce93434fbe76236a561e14cd61c","user":{"login":"houndci","id":6697940,"avatar_url":"https://avatars.githubusercontent.com/u/6697940?v=3","gravatar_id":"","url":"https://api.github.com/users/houndci","html_url":"https://github.com/houndci","followers_url":"https://api.github.com/users/houndci/followers","following_url":"https://api.github.com/users/houndci/following{/other_user}","gists_url":"https://api.github.com/users/houndci/gists{/gist_id}","starred_url":"https://api.github.com/users/houndci/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/houndci/subscriptions","organizations_url":"https://api.github.com/users/houndci/orgs","repos_url":"https://api.github.com/users/houndci/repos","events_url":"https://api.github.com/users/houndci/events{/privacy}","received_events_url":"https://api.github.com/users/houndci/received_events","type":"User","site_admin":false},"body":"Do not write to stdout. Use Rails' logger if you want to log.","created_at":"2015-12-10T17:41:34Z","updated_at":"2015-12-10T17:41:34Z","html_url":"https://github.com/wurde/script/pull/46#discussion_r47258042","pull_request_url":"https://api.github.com/repos/wurde/script/pulls/46","_links":{"self":{"href":"https://api.github.com/repos/wurde/script/pulls/comments/47258042"},"html":{"href":"https://github.com/wurde/script/pull/46#discussion_r47258042"},"pull_request":{"href":"https://api.github.com/repos/wurde/script/pulls/46"}}},"pull_request":{"url":"https://api.github.com/repos/wurde/script/pulls/46","id":53214282,"html_url":"https://github.com/wurde/script/pull/46","diff_url":"https://github.com/wurde/script/pull/46.diff","patch_url":"https://github.com/wurde/script/pull/46.patch","issue_url":"https://api.github.com/repos/wurde/script/issues/46","number":46,"state":"open","locked":false,"title":"Fix#45 Bin script","user":{"login":"wurde","id":5455357,"avatar_url":"https://avatars.githubusercontent.com/u/5455357?v=3","gravatar_id":"","url":"https://api.github.com/users/wurde","html_url":"https://github.com/wurde","followers_url":"https://api.github.com/users/wurde/followers","following_url":"https://api.github.com/users/wurde/following{/other_user}","gists_url":"https://api.github.com/users/wurde/gists{/gist_id}","starred_url":"https://api.github.com/users/wurde/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wurde/subscriptions","organizations_url":"https://api.github.com/users/wurde/orgs","repos_url":"https://api.github.com/users/wurde/repos","events_url":"https://api.github.com/users/wurde/events{/privacy}","received_events_url":"https://api.github.com/users/wurde/received_events","type":"User","site_admin":false},"body":"fixes #45 ","created_at":"2015-12-10T04:26:26Z","updated_at":"2015-12-10T17:41:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"60df4bfe2dc4b92ae276d099d637c559ab989f2a","assignee":{"login":"wurde","id":5455357,"avatar_url":"https://avatars.githubusercontent.com/u/5455357?v=3","gravatar_id":"","url":"https://api.github.com/users/wurde","html_url":"https://github.com/wurde","followers_url":"https://api.github.com/users/wurde/followers","following_url":"https://api.github.com/users/wurde/following{/other_user}","gists_url":"https://api.github.com/users/wurde/gists{/gist_id}","starred_url":"https://api.github.com/users/wurde/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wurde/subscriptions","organizations_url":"https://api.github.com/users/wurde/orgs","repos_url":"https://api.github.com/users/wurde/repos","events_url":"https://api.github.com/users/wurde/events{/privacy}","received_events_url":"https://api.github.com/users/wurde/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/wurde/script/pulls/46/commits","review_comments_url":"https://api.github.com/repos/wurde/script/pulls/46/comments","review_comment_url":"https://api.github.com/repos/wurde/script/pulls/comments{/number}","comments_url":"https://api.github.com/repos/wurde/script/issues/46/comments","statuses_url":"https://api.github.com/repos/wurde/script/statuses/dad09b30a1aa6ce93434fbe76236a561e14cd61c","head":{"label":"wurde:fix#45_bin_script","ref":"fix#45_bin_script","sha":"dad09b30a1aa6ce93434fbe76236a561e14cd61c","user":{"login":"wurde","id":5455357,"avatar_url":"https://avatars.githubusercontent.com/u/5455357?v=3","gravatar_id":"","url":"https://api.github.com/users/wurde","html_url":"https://github.com/wurde","followers_url":"https://api.github.com/users/wurde/followers","following_url":"https://api.github.com/users/wurde/following{/other_user}","gists_url":"https://api.github.com/users/wurde/gists{/gist_id}","starred_url":"https://api.github.com/users/wurde/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wurde/subscriptions","organizations_url":"https://api.github.com/users/wurde/orgs","repos_url":"https://api.github.com/users/wurde/repos","events_url":"https://api.github.com/users/wurde/events{/privacy}","received_events_url":"https://api.github.com/users/wurde/received_events","type":"User","site_admin":false},"repo":{"id":16531196,"name":"script","full_name":"wurde/script","owner":{"login":"wurde","id":5455357,"avatar_url":"https://avatars.githubusercontent.com/u/5455357?v=3","gravatar_id":"","url":"https://api.github.com/users/wurde","html_url":"https://github.com/wurde","followers_url":"https://api.github.com/users/wurde/followers","following_url":"https://api.github.com/users/wurde/following{/other_user}","gists_url":"https://api.github.com/users/wurde/gists{/gist_id}","starred_url":"https://api.github.com/users/wurde/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wurde/subscriptions","organizations_url":"https://api.github.com/users/wurde/orgs","repos_url":"https://api.github.com/users/wurde/repos","events_url":"https://api.github.com/users/wurde/events{/privacy}","received_events_url":"https://api.github.com/users/wurde/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/wurde/script","description":"Collection of command-line applications.","fork":false,"url":"https://api.github.com/repos/wurde/script","forks_url":"https://api.github.com/repos/wurde/script/forks","keys_url":"https://api.github.com/repos/wurde/script/keys{/key_id}","collaborators_url":"https://api.github.com/repos/wurde/script/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/wurde/script/teams","hooks_url":"https://api.github.com/repos/wurde/script/hooks","issue_events_url":"https://api.github.com/repos/wurde/script/issues/events{/number}","events_url":"https://api.github.com/repos/wurde/script/events","assignees_url":"https://api.github.com/repos/wurde/script/assignees{/user}","branches_url":"https://api.github.com/repos/wurde/script/branches{/branch}","tags_url":"https://api.github.com/repos/wurde/script/tags","blobs_url":"https://api.github.com/repos/wurde/script/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/wurde/script/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/wurde/script/git/refs{/sha}","trees_url":"https://api.github.com/repos/wurde/script/git/trees{/sha}","statuses_url":"https://api.github.com/repos/wurde/script/statuses/{sha}","languages_url":"https://api.github.com/repos/wurde/script/languages","stargazers_url":"https://api.github.com/repos/wurde/script/stargazers","contributors_url":"https://api.github.com/repos/wurde/script/contributors","subscribers_url":"https://api.github.com/repos/wurde/script/subscribers","subscription_url":"https://api.github.com/repos/wurde/script/subscription","commits_url":"https://api.github.com/repos/wurde/script/commits{/sha}","git_commits_url":"https://api.github.com/repos/wurde/script/git/commits{/sha}","comments_url":"https://api.github.com/repos/wurde/script/comments{/number}","issue_comment_url":"https://api.github.com/repos/wurde/script/issues/comments{/number}","contents_url":"https://api.github.com/repos/wurde/script/contents/{+path}","compare_url":"https://api.github.com/repos/wurde/script/compare/{base}...{head}","merges_url":"https://api.github.com/repos/wurde/script/merges","archive_url":"https://api.github.com/repos/wurde/script/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/wurde/script/downloads","issues_url":"https://api.github.com/repos/wurde/script/issues{/number}","pulls_url":"https://api.github.com/repos/wurde/script/pulls{/number}","milestones_url":"https://api.github.com/repos/wurde/script/milestones{/number}","notifications_url":"https://api.github.com/repos/wurde/script/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/wurde/script/labels{/name}","releases_url":"https://api.github.com/repos/wurde/script/releases{/id}","created_at":"2014-02-05T00:57:16Z","updated_at":"2015-12-03T23:52:55Z","pushed_at":"2015-12-10T17:41:28Z","git_url":"git://github.com/wurde/script.git","ssh_url":"git@github.com:wurde/script.git","clone_url":"https://github.com/wurde/script.git","svn_url":"https://github.com/wurde/script","homepage":"","size":2241,"stargazers_count":2,"watchers_count":2,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":18,"forks":2,"open_issues":18,"watchers":2,"default_branch":"master"}},"base":{"label":"wurde:master","ref":"master","sha":"c7ecf0616a8b696affff9aeb0f1b820729ceb422","user":{"login":"wurde","id":5455357,"avatar_url":"https://avatars.githubusercontent.com/u/5455357?v=3","gravatar_id":"","url":"https://api.github.com/users/wurde","html_url":"https://github.com/wurde","followers_url":"https://api.github.com/users/wurde/followers","following_url":"https://api.github.com/users/wurde/following{/other_user}","gists_url":"https://api.github.com/users/wurde/gists{/gist_id}","starred_url":"https://api.github.com/users/wurde/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wurde/subscriptions","organizations_url":"https://api.github.com/users/wurde/orgs","repos_url":"https://api.github.com/users/wurde/repos","events_url":"https://api.github.com/users/wurde/events{/privacy}","received_events_url":"https://api.github.com/users/wurde/received_events","type":"User","site_admin":false},"repo":{"id":16531196,"name":"script","full_name":"wurde/script","owner":{"login":"wurde","id":5455357,"avatar_url":"https://avatars.githubusercontent.com/u/5455357?v=3","gravatar_id":"","url":"https://api.github.com/users/wurde","html_url":"https://github.com/wurde","followers_url":"https://api.github.com/users/wurde/followers","following_url":"https://api.github.com/users/wurde/following{/other_user}","gists_url":"https://api.github.com/users/wurde/gists{/gist_id}","starred_url":"https://api.github.com/users/wurde/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wurde/subscriptions","organizations_url":"https://api.github.com/users/wurde/orgs","repos_url":"https://api.github.com/users/wurde/repos","events_url":"https://api.github.com/users/wurde/events{/privacy}","received_events_url":"https://api.github.com/users/wurde/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/wurde/script","description":"Collection of command-line applications.","fork":false,"url":"https://api.github.com/repos/wurde/script","forks_url":"https://api.github.com/repos/wurde/script/forks","keys_url":"https://api.github.com/repos/wurde/script/keys{/key_id}","collaborators_url":"https://api.github.com/repos/wurde/script/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/wurde/script/teams","hooks_url":"https://api.github.com/repos/wurde/script/hooks","issue_events_url":"https://api.github.com/repos/wurde/script/issues/events{/number}","events_url":"https://api.github.com/repos/wurde/script/events","assignees_url":"https://api.github.com/repos/wurde/script/assignees{/user}","branches_url":"https://api.github.com/repos/wurde/script/branches{/branch}","tags_url":"https://api.github.com/repos/wurde/script/tags","blobs_url":"https://api.github.com/repos/wurde/script/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/wurde/script/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/wurde/script/git/refs{/sha}","trees_url":"https://api.github.com/repos/wurde/script/git/trees{/sha}","statuses_url":"https://api.github.com/repos/wurde/script/statuses/{sha}","languages_url":"https://api.github.com/repos/wurde/script/languages","stargazers_url":"https://api.github.com/repos/wurde/script/stargazers","contributors_url":"https://api.github.com/repos/wurde/script/contributors","subscribers_url":"https://api.github.com/repos/wurde/script/subscribers","subscription_url":"https://api.github.com/repos/wurde/script/subscription","commits_url":"https://api.github.com/repos/wurde/script/commits{/sha}","git_commits_url":"https://api.github.com/repos/wurde/script/git/commits{/sha}","comments_url":"https://api.github.com/repos/wurde/script/comments{/number}","issue_comment_url":"https://api.github.com/repos/wurde/script/issues/comments{/number}","contents_url":"https://api.github.com/repos/wurde/script/contents/{+path}","compare_url":"https://api.github.com/repos/wurde/script/compare/{base}...{head}","merges_url":"https://api.github.com/repos/wurde/script/merges","archive_url":"https://api.github.com/repos/wurde/script/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/wurde/script/downloads","issues_url":"https://api.github.com/repos/wurde/script/issues{/number}","pulls_url":"https://api.github.com/repos/wurde/script/pulls{/number}","milestones_url":"https://api.github.com/repos/wurde/script/milestones{/number}","notifications_url":"https://api.github.com/repos/wurde/script/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/wurde/script/labels{/name}","releases_url":"https://api.github.com/repos/wurde/script/releases{/id}","created_at":"2014-02-05T00:57:16Z","updated_at":"2015-12-03T23:52:55Z","pushed_at":"2015-12-10T17:41:28Z","git_url":"git://github.com/wurde/script.git","ssh_url":"git@github.com:wurde/script.git","clone_url":"https://github.com/wurde/script.git","svn_url":"https://github.com/wurde/script","homepage":"","size":2241,"stargazers_count":2,"watchers_count":2,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":18,"forks":2,"open_issues":18,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/wurde/script/pulls/46"},"html":{"href":"https://github.com/wurde/script/pull/46"},"issue":{"href":"https://api.github.com/repos/wurde/script/issues/46"},"comments":{"href":"https://api.github.com/repos/wurde/script/issues/46/comments"},"review_comments":{"href":"https://api.github.com/repos/wurde/script/pulls/46/comments"},"review_comment":{"href":"https://api.github.com/repos/wurde/script/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/wurde/script/pulls/46/commits"},"statuses":{"href":"https://api.github.com/repos/wurde/script/statuses/dad09b30a1aa6ce93434fbe76236a561e14cd61c"}}}}