lib/ayadn/set.rb in ayadn-2.1 vs lib/ayadn/set.rb in ayadn-3.0

- old
+ new

@@ -130,10 +130,27 @@ exit end counts_config.save end + desc "edit", "Edit settings in $EDITOR" + def edit + Settings.load_config() + sets = Settings.config[:paths][:config] + "/config.yml" + if sets.blank? + Status.new.not_authorized + exit + end + ed = `echo $EDITOR` + if ed.blank? + Status.new.info "No predefined EDITOR, opening in VIM" + `vim #{sets}` + exit + end + `$EDITOR #{sets}` + end + desc "color ITEM COLOR", "Set ITEM to COLOR" long_desc Descriptions.set_color map "colors" => :color map "colour" => :color map "colours" => :color @@ -200,14 +217,48 @@ exit end formats_config.save end + desc "api URL", "Set an alternative base URL for the API calls." + def api(*args) + if args[0] + begin + SetAPI.new.setURL(args[0]) + rescue NoMethodError, ArgumentError => e + Status.new.error_missing_parameters + exit + rescue => e + raise e + end + else + Status.new.error_missing_parameters + exit + end + end + end class Validators + def self.URL(str) + require 'net/http' + if(str.to_s.empty?) + Status.new.error_missing_parameters + exit + end + begin + URI.parse(str) + # if url.host.nil? || (url.scheme != 'http' && url.scheme != 'https') + # ask: are you sure about this url? + # end + # url + rescue URI::Error => e + return nil + end + end + def self.boolean(value) case value.downcase when "true", "1", "yes" true when "false", "0", "no" @@ -269,24 +320,48 @@ def initialize Settings.load_config() Settings.get_token() Settings.init_config() Logs.create_logger() - @thor = Thor::Shell::Color.new @status = Status.new end def save Settings.save_config() log() end def log @status.say do - @thor.say_status(:updated, "'#{@input}' in '#{@category}'", :cyan) - @thor.say_status(:content, "'#{@output}'", :green) + @status.say_cyan :updated, "'#{@input}' in '#{@category}'" + @status.say_green :content, "'#{@output}'" end Logs.rec.info "new value for '#{@input}' in '#{@category}' => '#{@output}'" + end + + end + + class SetAPI < SetBase + + def initialize + super + @category = 'API' + @status = Status.new + end + + def setURL(url) + @input = url + # @status.say_header "checking URL validity" + url = Validators.URL(url) + if url != nil + @output = url.to_s + @status.say_info "setting up configuration" + File.write(Dir.home + "/ayadn/.api.yml", {root: @output}.to_yaml) + log() + else + @status.say_red :canceled, "URL is invalid" + exit + end end end class SetFormats < SetBase