From 07634453ed364ddb9a5f6c404daf509f031684a1 Mon Sep 17 00:00:00 2001 From: sf Date: Sat, 15 Oct 2011 14:52:37 -0400 Subject: [PATCH 1/3] updated the template script --- doc/tracks_template_cli.rb | 64 +++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/doc/tracks_template_cli.rb b/doc/tracks_template_cli.rb index 3212a8b1..29893e55 100644 --- a/doc/tracks_template_cli.rb +++ b/doc/tracks_template_cli.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -# Version 0.1 (Sept 4, 2011) +# Version 0.2 (Sept 30, 2011) # # Based on the tracks_cli by Vitalie Lazu (https://gist.github.com/45537) @@ -21,11 +21,15 @@ # ENV['GTD_CONTEXT_URL'] --> 'http://localhost:3000/contexts.xml' # Format of input file: +# - A token to be replaced in the subsequent lines starts with the string token # - New Projects start at the beginning of the line # - New actions start with a '.' at the beginning of the line. To add a note to an action, separate the action from its note by using '|'. You have to stay in the same line. # - Comments start with '#' # Example of an input file. Remove the '# ' string at the beginning and save it in a file. +# token [location] +# token [start] +# token [end] # Book trip to [location] # .Check visa requirements for [location]|instantiate template_visa, if visa required # .Book flight to [location]|starting trip around [start], returning around [end] @@ -45,12 +49,13 @@ # .Set a reminder to check for reimbursement for [location] # .Mail folder to secretary -# Instantiate this template: cat template | ./tracks_template_cli -c 1 -k '[location]=Dublin' -k '[start]=Oct 10' -k '[end]=Oct 16' +# Instantiate this template: ./tracks_template_cli -c 1 -f template_file.txt require 'net/https' require 'optparse' require 'cgi' require 'time' +require 'readline' class Hash def to_query_string @@ -104,6 +109,8 @@ module Gtd req.basic_auth ENV['GTD_LOGIN'], ENV['GTD_PASSWORD'] req.body = "#{props}" + puts req.body + resp = http.request(req) if resp.code == '302' || resp.code == '201' @@ -203,6 +210,10 @@ module Gtd @keywords[v.split("=")[0]] = v.split("=")[1] end + cmd.on('-f [S]', "filename of the template") do |v| + @filename = v + end + cmd.on('-c [N]', Integer, 'default context id to set for new projects') do |v| @options[:context_id] = v end @@ -219,17 +230,34 @@ module Gtd def run(args) @parser.parse!(args) - lines = STDIN.read + # lines = STDIN.read gtd = API.new - if lines.strip.empty? - puts "Please pipe in some content to tracks on STDIN." + if not File.exist?(@filename) + puts "ERROR: file #{@filename} doesn't exist" exit 1 end + if ENV['GTD_LOGIN'] == nil + puts "ERROR: no GTD_LOGIN environment variable set" + exit 1 + end + + if ENV['GTD_PASSWORD'] == nil + puts "ERROR: no GTD_PASSWORD environment variable set" + exit 1 + end + + file = File.open(@filename) + + # if lines.strip.empty? + # puts "Please pipe in some content to tracks on STDIN." + # exit 1 + # end + ## check for existence of the context if !@options[:context_id] - puts "Error: need to specify a context_id with -c option. Go here to find one: #{API::GTD_URI_CONTEXTS}" + puts "ERROR: need to specify a context_id with -c option. Go here to find one: #{API::GTD_URI_CONTEXTS}" exit 1 end @@ -238,8 +266,20 @@ module Gtd exit 1 end - lines.each_line do |line| - next if (line.strip.empty? || line[0].chr == "#") + #lines.each_line do |line| + while line = file.gets + line = line.strip + next if (line.empty? || line[0].chr == "#") + + if (line.split(' ')[0] == "token") + ## defining a new token; ask for input + + newtok=line.split(' ')[1] + + print "Input required for "+newtok+": " + @keywords[newtok]=gets.chomp + next + end # replace tokens @keywords.each do |key,val| @@ -254,13 +294,13 @@ module Gtd # find notes tmp= line.split("|") - if tmp.length > 2 - puts "Formatting error: found multiple |" + if tmp.length > 3 + puts "Formatting error: found too many |" exit 1 end - line=tmp[0] - @options[:note]=tmp[1] + line=tmp[0] + @options[:note]=tmp[1] if !@options[:project_id] puts "Warning: no project specified for task \"#{line}\". Using default project." From a8a699da1f09e323ffe82428d3a60a8ee062ab2b Mon Sep 17 00:00:00 2001 From: sf Date: Sun, 16 Oct 2011 01:53:29 -0400 Subject: [PATCH 2/3] permit using stdin --- doc/tracks_template_cli.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) mode change 100644 => 100755 doc/tracks_template_cli.rb diff --git a/doc/tracks_template_cli.rb b/doc/tracks_template_cli.rb old mode 100644 new mode 100755 index 29893e55..fc466c67 --- a/doc/tracks_template_cli.rb +++ b/doc/tracks_template_cli.rb @@ -109,8 +109,6 @@ module Gtd req.basic_auth ENV['GTD_LOGIN'], ENV['GTD_PASSWORD'] req.body = "#{props}" - puts req.body - resp = http.request(req) if resp.code == '302' || resp.code == '201' @@ -212,6 +210,11 @@ module Gtd cmd.on('-f [S]', "filename of the template") do |v| @filename = v + + if not File.exist?(@filename) + puts "ERROR: file #{@filename} doesn't exist" + exit 1 + end end cmd.on('-c [N]', Integer, 'default context id to set for new projects') do |v| @@ -233,11 +236,6 @@ module Gtd # lines = STDIN.read gtd = API.new - if not File.exist?(@filename) - puts "ERROR: file #{@filename} doesn't exist" - exit 1 - end - if ENV['GTD_LOGIN'] == nil puts "ERROR: no GTD_LOGIN environment variable set" exit 1 @@ -248,7 +246,11 @@ module Gtd exit 1 end - file = File.open(@filename) + if @filename.nil? + file = STDIN + else + file = File.open(@filename) + end # if lines.strip.empty? # puts "Please pipe in some content to tracks on STDIN." From 3fa40279f4b884ec54a9e56deb8784d345fc8a2e Mon Sep 17 00:00:00 2001 From: sf Date: Sun, 16 Oct 2011 01:59:45 -0400 Subject: [PATCH 3/3] check for use of token with the command line --- doc/tracks_template_cli.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/doc/tracks_template_cli.rb b/doc/tracks_template_cli.rb index fc466c67..fcf2a728 100755 --- a/doc/tracks_template_cli.rb +++ b/doc/tracks_template_cli.rb @@ -51,6 +51,8 @@ # Instantiate this template: ./tracks_template_cli -c 1 -f template_file.txt +# Template can also be read from STDIN, however, then you have specify all tokens with -k + require 'net/https' require 'optparse' require 'cgi' @@ -278,8 +280,11 @@ module Gtd newtok=line.split(' ')[1] - print "Input required for "+newtok+": " - @keywords[newtok]=gets.chomp + if @keywords[newtok].nil? + print "Input required for "+newtok+": " + @keywords[newtok]=gets.chomp + end + next end