wiki_tips_post 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 52dea333415c40693a32086c38d08b529e973a3a
4
+ data.tar.gz: 9d5fea956cf2f81317c43b7f7e653c96f364f980
5
+ SHA512:
6
+ metadata.gz: 06f0513f61df1c1db092aa6c75a86bfb355e123e4009b95e6a0bb3a236b963c4afcde03e8f51ade9a680b8ac0607a60c233f67d4f65bb71f1e42929e1f3c0c10
7
+ data.tar.gz: 90a50e600216ffd2e9e136c531bdab8b1251485aa0444ecbbab43933792fe708c6d0ef8e5580cb29f59dec33533bdbf1c13f669bc65e89dc6385bc3735d3b1fb
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wiki_tips_post.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Kai Xiang
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # WikiTipsPost
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'wiki_tips_post'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install wiki_tips_post
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/wiki_tips_post/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: [:spec]
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ # vim: ft=ruby
3
+
4
+ libdir = File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
5
+ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
6
+
7
+ require "rubygems"
8
+
9
+ require "wiki_tips_post"
10
+
11
+ $stdout.sync = true
12
+
13
+ if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new("1.9.3")
14
+ warn "ERROR: \033[31mRuby version #{RUBY_VERSION} is not supported.\033[0m
15
+ Please install 1.9.3 or later. (See
16
+ http://docs.cloudfoundry.com/docs/common/install_ruby.html for more
17
+ information)"
18
+ exit 1
19
+ end
20
+
21
+ WikiTipsPost::HelperCommand.start(ARGV)
@@ -0,0 +1,67 @@
1
+ require "mothership"
2
+ require "highline/import"
3
+
4
+ module WikiTipsPost
5
+ class HelperCommand < Mothership
6
+
7
+ desc "Show Help"
8
+ input :command, :argument => :optional
9
+ def help
10
+ if name = input[:command]
11
+ if cmd = @@commands[name.gsub("-", "_").to_sym]
12
+ Mothership::Help.command_help(cmd)
13
+ else
14
+ unknown_command(name)
15
+ end
16
+ else
17
+ Mothership::Help.basic_help(@@commands, @@global)
18
+ end
19
+ end
20
+
21
+ desc "Post tip on cloudops wiki with tags"
22
+ input (:title) { ask ("the title of the wiki:") }
23
+ input (:tip) { ask("the tip:") }
24
+ input (:tags) { ask("the tags for this tip (multiple tags split with ,) :") }
25
+ input (:file)
26
+ def post
27
+ begin
28
+ tag_array = input[:tags].split(',')
29
+ rescue StandardError => e
30
+ puts "Wrong tag format, it should be string split with ,"
31
+ end
32
+
33
+ tip_content = nil
34
+ if input[:file]
35
+ tip_content = File.read File.expand_path(input[:file])
36
+ end
37
+
38
+ @wikiposter = WikiPostHelper.new(
39
+ :title => input[:title],
40
+ :tips => tip_content || input[:tip],
41
+ :tags => tag_array
42
+ )
43
+
44
+ puts "\ncloning repo..."
45
+ @wikiposter.prepare_repo
46
+
47
+ puts "\ncreating post..."
48
+ @wikiposter.create_post
49
+
50
+ puts "\npushing repo..."
51
+ push_repo_success = @wikiposter.push_to_repo
52
+
53
+ if push_repo_success
54
+ puts "\nremoving temporary clone of repo..."
55
+ @wikiposter.delete_local_repo_clone
56
+
57
+ puts "\nsuccessfully added tip!"
58
+ else
59
+ puts "\npush was NOT successful"
60
+
61
+ exit 1
62
+ end
63
+ end
64
+ end
65
+
66
+ end
67
+
@@ -0,0 +1,7 @@
1
+ module WikiTipsPost
2
+ class KernelWrapper
3
+ def system(*args)
4
+ Kernel.system(*args)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module WikiTipsPost
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,80 @@
1
+ require 'tmpdir'
2
+
3
+ module WikiTipsPost
4
+
5
+ WIKI_REPO='git@github.com:pivotal-cf/cloudops-tools.wiki.git'
6
+ INDEX_PAGE='Cloudops Wiki Tips Index.md'
7
+
8
+ class WikiPostHelper
9
+ def initialize(title: title, tips: tips, tags: tags)
10
+ @title = title
11
+ @tips = tips
12
+ @tags = tags
13
+ @tmp_dir = Dir.mktmpdir("wiki")
14
+ @kernel = KernelWrapper.new
15
+ end
16
+
17
+ def prepare_repo
18
+ git_clone="git clone #{WIKI_REPO} #{@tmp_dir}"
19
+ @kernel.system(git_clone)
20
+ end
21
+
22
+ def add_new_page
23
+ File.write(tip_path, @tips)
24
+ end
25
+
26
+ def link_to_tag(tag)
27
+ tag_path = File.join(
28
+ @tmp_dir,
29
+ '#' + tag.chomp(' ').reverse.chomp(' ').reverse.gsub(' ','_') + '.md'
30
+ )
31
+
32
+ file_already_exists = File.exists?(tag_path)
33
+ new_tag_contents = (file_already_exists ? File.read(tag_path).strip : "") + "\n* [[ #{@title} ]]"
34
+ File.open(tag_path, 'w' ) { |f| f.write new_tag_contents }
35
+
36
+ !file_already_exists
37
+ end
38
+
39
+ def link_to_index(tag)
40
+ tag_link = "* [[ ##{ tag.gsub(' ', '_') } ]]"
41
+ index_page_path = File.join(@tmp_dir, INDEX_PAGE)
42
+ index_page_contents = File.read(index_page_path)
43
+
44
+ unless index_page_contents.index(tag_link)
45
+ new_index_page_contents = index_page_contents.strip + "\n" + tag_link
46
+ content = new_index_page_contents.split("\n").sort
47
+ content.delete("Category with Tags:")
48
+ content.insert 0, "Category with Tags:"
49
+ final_content = content.join("\n")
50
+ File.open(index_page_path, 'w') { |f| f.write final_content }
51
+
52
+ end
53
+ end
54
+
55
+ def create_post
56
+ add_new_page
57
+ @tags.each do |tg|
58
+ if link_to_tag(tg)
59
+ link_to_index(tg)
60
+ end
61
+ end
62
+ end
63
+
64
+ def push_to_repo
65
+ git_push="pushd #{@tmp_dir} && git add . && git commit -m'Added a tip' && git push origin head && popd"
66
+ @kernel.system(git_push)
67
+ end
68
+
69
+ def delete_local_repo_clone
70
+ delete_dir="sudo rm -r #{@tmp_dir}"
71
+ @kernel.system(delete_dir)
72
+ end
73
+
74
+ private
75
+
76
+ def tip_path
77
+ File.join(@tmp_dir, @title + '.md')
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,8 @@
1
+ require "wiki_tips_post/version"
2
+ require "wiki_tips_post/helper_command"
3
+ require "wiki_tips_post/wiki_post_helper"
4
+ require "wiki_tips_post/kernel_wrapper"
5
+
6
+ module WikiTipsPost
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1 @@
1
+ * [[ some_tip ]]
@@ -0,0 +1,2 @@
1
+ Category with Tags:
2
+ * [[ #test_tag1 ]]
@@ -0,0 +1 @@
1
+ Category with Tags:
@@ -0,0 +1,25 @@
1
+
2
+ # This file was generated by the `rspec --init` command. Conventionally, all
3
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
5
+ # loaded once.
6
+ #
7
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
8
+
9
+ $:.unshift(File.expand_path("../lib", File.dirname(__FILE__)))
10
+
11
+ require 'wiki_tips_post'
12
+ require 'yaml'
13
+
14
+ RSpec.configure do |config|
15
+ config.failure_color = :magenta
16
+ config.tty = true
17
+ config.color = true
18
+ config.run_all_when_everything_filtered = true
19
+ config.filter_run :focus
20
+ end
21
+
22
+ def path_for_fixture(fixture_name)
23
+ fixtures_path = File.expand_path("./fixtures", File.dirname(__FILE__))
24
+ File.join fixtures_path, fixture_name
25
+ end
@@ -0,0 +1,130 @@
1
+ require "spec_helper"
2
+
3
+ module WikiTipsPost
4
+
5
+ describe WikiPostHelper do
6
+ let!(:tmp_dir) { Dir.mktmpdir }
7
+ let!(:kernel) { double(:kernel_wrapper) }
8
+ let(:subject) do
9
+ described_class.new(
10
+ :title => 'test title',
11
+ :tips => 'test tips text',
12
+ :tags => ['test_tag1', 'test_tag2']
13
+ )
14
+ end
15
+
16
+ before do
17
+ allow(KernelWrapper).to receive(:new).and_return(kernel)
18
+ allow(Dir).to receive(:mktmpdir).and_return(tmp_dir)
19
+ end
20
+
21
+ describe "adding a tip" do
22
+
23
+ before do
24
+ FileUtils.cp_r fixture_path, tmp_dir
25
+ end
26
+
27
+ after do
28
+ FileUtils.rm_r tmp_dir
29
+ end
30
+
31
+ context 'index without tag' do
32
+ let(:fixture_path) { path_for_fixture('wiki/index_without_tag/.') }
33
+
34
+ it 'prepares the git repo in a tmp directory' do
35
+ expect(kernel).to receive(:system).with("git clone git@github.com:pivotal-cf/cloudops-tools.wiki.git #{tmp_dir}")
36
+ subject.prepare_repo
37
+ end
38
+
39
+ it 'adds new pages into the repo with subject and name' do
40
+ subject.add_new_page
41
+
42
+ tip_contents = File.read File.join(tmp_dir, 'test title.md')
43
+ expect(tip_contents).to eq('test tips text')
44
+ end
45
+
46
+ describe 'creates tag pages' do
47
+ it 'when there is a space in between' do
48
+ need_index = subject.link_to_tag('test tag1')
49
+ tag_page_contents = File.read File.join(tmp_dir, '#test_tag1.md')
50
+ expect(tag_page_contents).to eq("\n* [[ test title ]]")
51
+ expect(need_index).to be(true)
52
+ end
53
+
54
+ it 'when there is a space in front' do
55
+ need_index = subject.link_to_tag(' test tag2')
56
+ tag_page_contents = File.read File.join(tmp_dir, '#test_tag2.md')
57
+ expect(tag_page_contents).to eq("\n* [[ test title ]]")
58
+ expect(need_index).to be(true)
59
+ end
60
+
61
+ it 'when there is a space in back' do
62
+ need_index = subject.link_to_tag('test tag3 ')
63
+ tag_page_contents = File.read File.join(tmp_dir, '#test_tag3.md')
64
+ expect(tag_page_contents).to eq("\n* [[ test title ]]")
65
+ expect(need_index).to be(true)
66
+ end
67
+ end
68
+
69
+ it 'adds the created and sorted tag page to the index page' do
70
+ expected_content = (<<EOF).chomp
71
+ Category with Tags:
72
+ * [[ #test_taga ]]
73
+ * [[ #test_tagb ]]
74
+ * [[ #test_tagc ]]
75
+ EOF
76
+
77
+
78
+ subject.link_to_index('test tagc')
79
+ subject.link_to_index('test tagb')
80
+ subject.link_to_index('test taga')
81
+
82
+ index_page_contents = File.read File.join(tmp_dir, INDEX_PAGE)
83
+ expect(index_page_contents).to eq(expected_content)
84
+ end
85
+ end
86
+
87
+ context 'index with tag' do
88
+ let(:fixture_path) { path_for_fixture('wiki/index_with_tag/.') }
89
+
90
+ it 'add itself into a existing tag page' do
91
+ tag_file_path = File.join(tmp_dir, '#test_tag1.md')
92
+ expect(File.exist?(tag_file_path))
93
+
94
+ need_index = subject.link_to_tag('test_tag1')
95
+ tag_page_contents = File.read File.join(tmp_dir, '#test_tag1.md')
96
+ expect(tag_page_contents).to eq("* [[ some_tip ]]\n* [[ test title ]]")
97
+ expect(need_index).to be(false)
98
+ end
99
+
100
+ it 'adds the created tag page to the index page' do
101
+ expected_content = "Category with Tags:\n* [[ #test_tag1 ]]\n"
102
+
103
+ # Nothing changes before and after
104
+ index_page_contents = File.read File.join(tmp_dir, INDEX_PAGE)
105
+ expect(index_page_contents).to eq(expected_content)
106
+
107
+ subject.link_to_index('test_tag1')
108
+
109
+ index_page_contents = File.read File.join(tmp_dir, INDEX_PAGE)
110
+ expect(index_page_contents).to eq(expected_content)
111
+ end
112
+ end
113
+ end
114
+
115
+
116
+ describe "pushing to the wiki repo" do
117
+ it "calls git push from the tmp dir" do
118
+ expect(kernel).to receive(:system).with("pushd #{tmp_dir} && git add . && git commit -m'Added a tip' && git push origin head && popd")
119
+ subject.push_to_repo
120
+ end
121
+ end
122
+
123
+ describe "deleting the temporary repo" do
124
+ it "calls removes the tmp dir" do
125
+ expect(kernel).to receive(:system).with(/^(sudo )?rm -r #{tmp_dir}/)
126
+ subject.delete_local_repo_clone
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wiki_tips_post/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wiki_tips_post"
8
+ spec.version = WikiTipsPost::VERSION
9
+ spec.authors = ["Kai Xiang"]
10
+ spec.email = ["kxiang@pivotal.io"]
11
+ spec.summary = %q{A tool helps to post tips into cloudops-tools wiki.}
12
+ spec.description = %q{And orgnize it with tags.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "mothership"
26
+ spec.add_development_dependency "highline"
27
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wiki_tips_post
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kai Xiang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mothership
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: highline
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: And orgnize it with tags.
84
+ email:
85
+ - kxiang@pivotal.io
86
+ executables:
87
+ - wiki_tips_post
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".ruby-version"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/wiki_tips_post
98
+ - lib/wiki_tips_post.rb
99
+ - lib/wiki_tips_post/helper_command.rb
100
+ - lib/wiki_tips_post/kernel_wrapper.rb
101
+ - lib/wiki_tips_post/version.rb
102
+ - lib/wiki_tips_post/wiki_post_helper.rb
103
+ - spec/fixtures/wiki/index_with_tag/#test_tag1.md
104
+ - spec/fixtures/wiki/index_with_tag/Cloudops Wiki Tips Index.md
105
+ - spec/fixtures/wiki/index_without_tag/Cloudops Wiki Tips Index.md
106
+ - spec/spec_helper.rb
107
+ - spec/wiki_tips_post/wiki_post_helper_spec.rb
108
+ - wiki_tips_post.gemspec
109
+ homepage: ''
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.2.2
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: A tool helps to post tips into cloudops-tools wiki.
133
+ test_files:
134
+ - spec/fixtures/wiki/index_with_tag/#test_tag1.md
135
+ - spec/fixtures/wiki/index_with_tag/Cloudops Wiki Tips Index.md
136
+ - spec/fixtures/wiki/index_without_tag/Cloudops Wiki Tips Index.md
137
+ - spec/spec_helper.rb
138
+ - spec/wiki_tips_post/wiki_post_helper_spec.rb