lib/bone/cli.rb in bone-0.2.6 vs lib/bone/cli.rb in bone-0.3.0

- old
+ new

@@ -1,41 +1,51 @@ require 'bone' -require 'net/http' +# TODO: finish this + class Bone::CLI < Drydock::Command def check! - @token = @global.t || ENV['BONE_TOKEN'] - raise Bone::BadBone, @token unless Bone.valid_token?(@token) + @token = @global.token || ENV['BONE_TOKEN'] + raise Bone::NoToken, @token unless Bone.token?(@token) + Bone.token = @token end def get check! @argv.unshift @alias unless @alias == 'get' - raise "No key specified" unless @argv.first - puts Bone.get(@argv.first) + ## TODO: handle bone name=value + ##if @alias.index('=') > 0 + ## a = @alias.gsub(/\s+=\s+/, '=') + ## name, value = *( ? @argv.first.split('=') : @argv) + ##end + raise Bone::Problem, "No key specified" unless @argv.first + ret = Bone.get(@argv.first) + puts ret unless ret.nil? end - def del - check! - raise "No key specified" unless @argv.first - puts Bone.del(@argv.first) - end - def set + # TODO: use STDIN instead of @option.string check! - opts = {:token => @token } - keyname, value = *(@argv.size == 1 ? @argv.first.split('=') : @argv) - raise "No key specified" unless keyname - raise "No value specified" unless value - if File.exists?(value) && !@option.string - value = File.readlines(value).join - opts[:file] = true + name, value = *(@argv.size == 1 ? @argv.first.split('=') : @argv) + raise Bone::Problem, "No key specified" unless name + from_stdin = false + if value.nil? && !stdin.tty? && !stdin.eof? + from_stdin = true + value = stdin.read end - puts Bone.set(keyname, value, opts) + raise Bone::Problem, "Cannot set null value" unless value + Bone[name] = value + puts from_stdin ? '<STDIN>' : value end + #def del + # check! + # raise Bone::Problem, "No key specified" unless @argv.first + # puts Bone.delete(@argv.first) + #end + def keys check! list = Bone.keys(@argv[0]) if list.empty? return if @global.quiet @@ -45,25 +55,29 @@ puts list end end def token - check! - if @option.force - generate_token_dialog - else - puts @token + check! + puts Bone.token + end + + def secret + check! + puts Bone.secret + end + + def generate + t, s = *Bone.generate + unless t.nil? + puts "# Your token for #{Bone.source}" + puts "BONE_TOKEN=#{t}" + puts "BONE_SECRET=#{s}" end - rescue Bone::BadBone => ex - generate_token_dialog - exit 1 + #rescue Bone::NoToken => ex + # update_token_dialog + # exit 1 end private - def generate_token_dialog - newtoken = Bone.generate_token - puts newtoken and return if @global.quiet - puts "Set the BONE_TOKEN environment variable with the following token" - puts newtoken - end end