Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 77 lines (63 sloc) 2.049 kb
#!/usr/bin/env ruby
# coding: utf-8
require 'chronic'
require 'highline/import'
require 'trollop'
require 'yaml'
# load library
file = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
lib = File.join File.dirname(file), "/../lib/beeminder"
if File.exists? lib
# using local version
require lib
else
require 'beeminder'
end
usage = "usage: beemind goal value [comment]"
opts = Trollop::options do
banner usage
opt :config, "Path to config.", :type => :string, :default => "~/.beeminderrc"
opt :list, "List all available goals."
opt :token, "Use this auth token instead of config. (Optional. Will ask for it if none given.)", :type => :string
opt :date, "Set a manual date. Uses Chronic syntax.", :type => :string, :default => "now"
end
Trollop::die usage if not (2..3).include?(ARGV.size) and not opts[:list]
goal, value, comment = ARGV unless opts[:list]
opts[:config] = File.expand_path opts[:config]
if not opts[:token]
# read config
if File.exists? opts[:config]
# read config to fill in the gaps
auth = YAML.load File.open(opts[:config]) || {}
opts[:token] ||= auth["token"]
end
# still empty? ask!
if opts[:token].nil?
# explicitly convert to String to work around HighLine bug
opts[:token] = ask("Auth token:").to_s
# save config for later
auth = {
"token" => opts[:token]
}
File.open(opts[:config], "w+") {|f| YAML.dump auth, f}
File.chmod 0600, opts[:config]
puts "Written config to '#{opts[:config]}.'"
end
end
# login
bee = Beeminder::User.new opts[:token]
if opts[:list]
# list all available goals
puts "available goals:"
goals = bee.goals.sort_by{|g| g.slug}.each do |goal|
puts " #{goal.slug} (#{goal.title})"
end
else
date = Chronic.parse(opts[:date], :context => :past)
Trollop::die "invalid date" if date.nil?
g = bee.goal goal
dp = Beeminder::Datapoint.new :timestamp => date,
:value => value,
:comment => (comment || "")
g.add dp
end
Jump to Line
Something went wrong with that request. Please try again.