til-rb 0.0.3 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 36329998af769fde62b800136de45304100925992f85d14463a127070da4b66d
4
- data.tar.gz: e0283b08a1145176a3e6b7ea6e77dc65f4030f86571d7db3f6b475a3085213fa
3
+ metadata.gz: 59a21e1f498c47b6ee14766f23b04d3a75297abf4cf4871ee35ae22307455b54
4
+ data.tar.gz: 501c6ad1040355ce2e96f9209d65eb8489e52ffbe21d4e8a43b5294dd3adf0b0
5
5
  SHA512:
6
- metadata.gz: '092def75bd54793b9318dc91a0b58bf3a6b09a488100daa0320bf97882f6fa94c6b84686aaf54c12e5bf51b662cc322b41de9a0a62c7e2fd2bd66732d3e80780'
7
- data.tar.gz: 3d241da839314e320055bf5c2e717017f57deafdf2a9f3463a8174ccd4a846df3afa68c9f2601669ab5b54cd8aa034c0d864a68dff6d0a7e0136c0d48d199953
6
+ metadata.gz: eeee1045a7652efbf9de6793e5f03895a790f22e9ebd8fd634d59fa71a3d42ae050c3aa1b6a6d4b83e8a9f299ee33a52527f06ce8aadf9bce9d556f78c4fa044
7
+ data.tar.gz: ce2e02df0edf57fc1c9cd9d1b6ae73c2d32ec1f6ba8a00b7fa3cd46d2913823a18eeadecd7178e26b158618de13b967f725965a365db94c1bb319ac9319cb95f
data/README.md CHANGED
@@ -5,3 +5,69 @@
5
5
  A work-in-progress tool to maintain a TIL repo.
6
6
 
7
7
  Inspiration: https://github.com/jbranchaud/til
8
+
9
+ See it in action below:
10
+
11
+ ![til in action](til.gif)
12
+
13
+ ## Installation
14
+
15
+ ### Step 1: Install the gem
16
+
17
+ ```
18
+ gem install til-rb
19
+ ```
20
+
21
+ You will also need `fzf` to run `til`, it's available on homebrew, so unless you already installed it, you'll have to run
22
+
23
+ ```
24
+ brew install fzf
25
+ ```
26
+
27
+ _fzf is technically not a hard requirement, we really could have a slightly different workflow if it's not available.
28
+ Given that I currently am the only user, there's no need to change this at the moment, but if you'd like to use this
29
+ gem without `fzf`, let me know and I'll happily work on it!_
30
+
31
+ ### Step 2: Create a GitHub repo
32
+
33
+ You need a GitHub repo to store your TILs. The gem has pretty strict expectations about the format of the README.md
34
+ file, so I recommend forking [this repo](https://github.com/pjambet/til-template), or just copying the content to a
35
+ fresh new repo.
36
+
37
+ Note: The format expectations mentioned above are the following:
38
+
39
+ - A "categories" section as defined by a leading markdown separator `---`, followed by a blank line, the `###
40
+ Categories` title, and a list of all the categories.
41
+ - A links section as defined by a leading markdown separator `---`, followed by a blank line and a series of markdown
42
+ titles for each categories present in the previous section.
43
+
44
+ ### Step 3: Add the environment variables
45
+
46
+ Add the following variables to your environment:
47
+
48
+ - `TIL_RB_GITHUB_TOKEN`: The only required scope is `public_repo` if your TIL repo is public and `repo` if it is private. You can
49
+ create a token [in the GitHub
50
+ settings](https://github.com/settings/tokens/new?scopes=public_repo&description=Token%20for%20til-rb)
51
+ - `TIL_RB_GITHUB_REPO`: The repo name, e.g. `pjambet/til`
52
+
53
+ You might want to add those to either your `.bashrc` or `.zshrc` but please be careful in case you share those publicly
54
+ as the token is private and *must not* be shared publicly.
55
+
56
+ Note: An earlier version of this gem used different names, `GH_TOKEN` & `GH_REPO`, it still works, but is not the
57
+ recommended approach anymore, see #2.
58
+
59
+ ### Step 4:
60
+
61
+ Run `til` from the command line
62
+
63
+ ## Future improvements
64
+
65
+ - An `init` command that will create a repo for you
66
+ - A `configure` command that will store the token and the repo name in a file in `~/.config/til/config` or `~/.til`
67
+
68
+ ## Known issues
69
+
70
+ The current version (0.0.5) deletes the temporary file before attempting to create the new commit, so if anything goes
71
+ wrong there, the content of the file will be lost.
72
+ This will be fixed soon, by only deleting the temporary file after the commit was created, but please keep that in mind
73
+ if you typed a long TIL and definitely don't want to lose its content.
data/bin/til CHANGED
@@ -1,5 +1,20 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'til-rb'
4
+ require 'optparse'
4
5
 
5
- Til::Core.run
6
+ options = {}
7
+ OptionParser.new do |opts|
8
+ opts.banner = 'Usage: til [options]'
9
+
10
+ opts.on('-t', '--title=TITLE', 'Set the title from the command line') do |title|
11
+ options[:title] = title
12
+ end
13
+
14
+ opts.on('-h', '--help', 'Prints this help') do
15
+ puts opts
16
+ exit
17
+ end
18
+ end.parse!
19
+
20
+ Til::Core.run options: options
@@ -5,7 +5,10 @@ require 'readline'
5
5
  module Til
6
6
  class Core
7
7
 
8
- def self.run
8
+ GH_TOKEN_ENV_VAR_NAME = 'TIL_RB_GITHUB_TOKEN'
9
+ GH_REPO_ENV_VAR_NAME = 'TIL_RB_GITHUB_REPO'
10
+
11
+ def self.run(options: {})
9
12
  # Exit if `fzf` is not available
10
13
  # Optionally print a spinner
11
14
  # Grab the list of existing categories
@@ -18,17 +21,18 @@ module Til
18
21
  # Create the new file
19
22
  # Create a new commit
20
23
  # Output a link to the file and the link to edit it
21
- til = new
24
+ til = new(options: options)
22
25
  til.run
23
26
  end
24
27
 
25
- def initialize(kernel: Kernel, process: Process, env: ENV, github_client: nil, stderr: $stderr)
28
+ def initialize(options: {}, kernel: Kernel, process: Process, env: ENV, github_client: nil, stderr: $stderr)
29
+ @options = options
26
30
  @kernel = kernel
27
31
  @process = process
28
32
  @env = env
29
33
  @stderr = stderr
30
34
  @github_client = github_client
31
- @repo_name = 'pjambet/til'
35
+ @repo_name = nil
32
36
  @new_category = false
33
37
  end
34
38
 
@@ -41,7 +45,7 @@ module Til
41
45
  if @new_category
42
46
  selected_category = prompt_for_new_category
43
47
  end
44
- prepopulate_tempfile(selected_category)
48
+ prepopulate_tempfile(selected_category, @options[:title])
45
49
  open_editor
46
50
  til_content = read_file
47
51
  commit_new_til(selected_category, til_content)
@@ -58,17 +62,25 @@ module Til
58
62
  end
59
63
 
60
64
  def check_environment_variables
61
- if @env['GH_TOKEN'].nil? || @env['GH_TOKEN'] == ''
62
- raise 'The GH_TOKEN (with the public_repo or repo scope) environment variable is required'
65
+ if @env[GH_TOKEN_ENV_VAR_NAME].nil? || @env[GH_TOKEN_ENV_VAR_NAME] == ''
66
+ if @env['GH_TOKEN'].nil? || @env['GH_TOKEN'] == ''
67
+ raise "The #{GH_TOKEN_ENV_VAR_NAME} (with the public_repo or repo scope) environment variable is required"
68
+ else
69
+ @stderr.puts "Using GH_TOKEN is deprecated, use #{GH_TOKEN_ENV_VAR_NAME} instead"
70
+ end
63
71
  end
64
72
 
65
- if (@env['VISUAL'].nil? || @env['VISUAL'] == '') && (@env['EDITOR'].nil? || @env['EDITOR'] == '')
66
- raise 'The VISUAL or EDITOR environment variables are required'
73
+ if @env[GH_REPO_ENV_VAR_NAME].nil? || @env[GH_REPO_ENV_VAR_NAME] == ''
74
+ if @env['GH_REPO'].nil? || @env['GH_REPO'] == ''
75
+ raise "The #{GH_REPO_ENV_VAR_NAME} environment variable is required"
76
+ else
77
+ @stderr.puts "Using GH_REPO is deprecated, use #{GH_REPO_ENV_VAR_NAME} instead"
78
+ end
67
79
  end
68
80
  end
69
81
 
70
82
  def fetch_existing_categories
71
- existing_categories = github_client.contents(@repo_name, path: '').filter do |c|
83
+ existing_categories = github_client.contents(repo_name, path: '').filter do |c|
72
84
  c['type'] == 'dir'
73
85
  end
74
86
 
@@ -80,7 +92,11 @@ module Til
80
92
  end
81
93
 
82
94
  def github_client
83
- @github_client ||= Octokit::Client.new(access_token: @env['GH_TOKEN'])
95
+ @github_client ||= Octokit::Client.new(access_token: @env[GH_TOKEN_ENV_VAR_NAME] || @env['GH_TOKEN'])
96
+ end
97
+
98
+ def repo_name
99
+ @repo_name ||= (@env[GH_REPO_ENV_VAR_NAME] || @env['GH_REPO'])
84
100
  end
85
101
 
86
102
  def prompt_fzf(categories)
@@ -116,7 +132,7 @@ module Til
116
132
  end
117
133
 
118
134
  def open_editor
119
- editor = ENV['VISUAL'] || ENV['EDITOR']
135
+ editor = ENV['VISUAL'] || ENV['EDITOR'] || 'vi'
120
136
  system(*editor.split, @tempfile.path)
121
137
  end
122
138
 
@@ -126,22 +142,26 @@ module Til
126
142
  content
127
143
  end
128
144
 
145
+ def new_filename(commit_title)
146
+ today = Time.now.strftime '%Y-%m-%d'
147
+ name = commit_title.split.map(&:downcase).join('-')
148
+ "#{today}_#{name}.md"
149
+ end
150
+
129
151
  def commit_new_til(category, content)
130
152
  commit_title = content.lines[0].chomp
131
153
  if commit_title.start_with?('#')
132
154
  commit_title = commit_title[1..].strip
133
155
  end
134
- today = Time.now.strftime '%Y-%m-%d'
135
- name = commit_title.split.map(&:downcase).join('-')
136
- filename = "#{today}_#{name}.md"
156
+ filename = new_filename(commit_title)
137
157
 
138
- ref = github_client.ref @repo_name, 'heads/master'
139
- commit = github_client.commit @repo_name, ref.object.sha
140
- tree = github_client.tree @repo_name, commit.commit.tree.sha, recursive: true
141
- readme = github_client.readme @repo_name
158
+ ref = github_client.ref repo_name, 'heads/master'
159
+ commit = github_client.commit repo_name, ref.object.sha
160
+ tree = github_client.tree repo_name, commit.commit.tree.sha, recursive: true
161
+ readme = github_client.readme repo_name
142
162
  readme_content = Base64.decode64 readme.content
143
163
 
144
- blob = github_client.create_blob @repo_name, content
164
+ blob = github_client.create_blob repo_name, content
145
165
  blobs = tree.tree.filter { |object|
146
166
  object[:type] == 'blob' && object[:path] != 'README.md'
147
167
  }.map { |object|
@@ -149,17 +169,20 @@ module Til
149
169
  }
150
170
 
151
171
  updated_readme_content = update_readme_content(category, commit_title, filename, readme_content)
152
- new_readme_blob = github_client.create_blob @repo_name, updated_readme_content
172
+ new_readme_blob = github_client.create_blob repo_name, updated_readme_content
153
173
  blobs << { path: 'README.md', mode: '100644', type: 'blob', sha: new_readme_blob }
154
174
 
155
175
  blobs << { path: "#{category}/#{filename}", mode: '100644', type: 'blob', sha: blob }
156
176
 
157
- tree = github_client.create_tree @repo_name, blobs
158
- commit = github_client.create_commit @repo_name, commit_title, tree.sha, ref.object.sha
159
- github_client.update_ref @repo_name, 'heads/master', commit.sha
177
+ tree = github_client.create_tree repo_name, blobs
178
+ commit = github_client.create_commit repo_name, commit_title, tree.sha, ref.object.sha
179
+ github_client.update_ref repo_name, 'heads/master', commit.sha
160
180
 
161
- puts "You can see your new TIL at : https://github.com/pjambet/til/blob/master/#{category}/#{filename}"
162
- puts "You can edit your new TIL at : https://github.com/pjambet/til/edit/master/#{category}/#{filename}"
181
+ cgi_escaped_filename = CGI.escape(filename)
182
+ til_url = "https://github.com/#{repo_name}/blob/master/#{category}/#{cgi_escaped_filename}"
183
+ til_edit_url = "https://github.com/#{repo_name}/edit/master/#{category}/#{cgi_escaped_filename}"
184
+ puts "You can see your new TIL at : #{til_url}"
185
+ puts "You can edit your new TIL at : #{til_edit_url}"
163
186
  end
164
187
 
165
188
  def update_readme_content(category, commit_title, filename, readme_content)
@@ -17,7 +17,7 @@ module Til
17
17
  loc_in_page = @initial_content.index("### #{existing_cat[1]}")
18
18
  next_cat_location = @initial_content.index('###', loc_in_page + 1)
19
19
 
20
- new_line = "- [#{item_title}](#{category}/#{filename})"
20
+ new_line = "- [#{item_title}](#{category}/#{CGI.escape(filename)})"
21
21
  new_readme_content = ''
22
22
  if next_cat_location
23
23
  breakpoint = next_cat_location - 2
@@ -64,7 +64,7 @@ module Til
64
64
 
65
65
  next_bound = @initial_content.index('###', current_search_index + 1)
66
66
 
67
- new_line = "- [#{item_title}](#{category}/#{filename})"
67
+ new_line = "- [#{item_title}](#{category}/#{CGI.escape(filename)})"
68
68
 
69
69
  if next_bound
70
70
  new_readme_content = @initial_content[0..(first_dashdashdash + 2)] \
@@ -1,5 +1,5 @@
1
1
  module Til
2
2
 
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.8'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: til-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre Jambet
@@ -66,7 +66,7 @@ files:
66
66
  - lib/til/core.rb
67
67
  - lib/til/readme_updater.rb
68
68
  - lib/til/version.rb
69
- homepage: https://rubygems.org/gems/til-rb
69
+ homepage: https://github.com/pjambet/til-rb/
70
70
  licenses:
71
71
  - MIT
72
72
  metadata: {}