lib/tsks/cli.rb in tsks-0.0.1 vs lib/tsks/cli.rb in tsks-0.0.2

- old
+ new

@@ -1,9 +1,10 @@ require "thor" require "time" require "tsks/storage" require "tsks/request" +require "tsks/actions" module Tsks class CLI < Thor @setup_folder = File.expand_path ENV["SETUP_FOLDER"] || "~/.tsks" @@ -19,11 +20,11 @@ Dir.mkdir CLI.setup_folder Tsks::Storage.init end - desc "add TSK", "Add a new tsk (Use --context to specify one i.g. Work)" + desc "add TSK", "Add a new tsk (Use --context to specify one e.g. Work)" option :context def add tsk if !File.directory? CLI.setup_folder return puts "tsks was not initialized yet." end @@ -64,11 +65,11 @@ tsks = Tsks::Storage.select_by({done: 0}) end if tsks.count > 0 for tsk in tsks - puts "#{tsk[:id]} @#{tsk[:context]} #{tsk[:tsk]}" + puts "#{tsk[:local_id]} @#{tsk[:context]} #{tsk[:tsk]}" end else puts "No tsks found." end end @@ -84,10 +85,12 @@ res = Tsks::Request.post "/register", {email: options[:email], password: options[:password]} if res && res[:status_code] == 201 File.write File.join(CLI.setup_folder, "token"), res[:token] + File.write File.join(CLI.setup_folder, "user_id"), res[:user_id] + Tsks::Actions.update_tsks_with_uuid res[:user_id] puts "Succesfully registered." elsif res && res[:status_code] == 409 puts "This e-mail is already registered." end end @@ -103,10 +106,12 @@ res = Tsks::Request.post "/login", {email: options[:email], password: options[:password]} if res && res[:status_code] == 200 File.write File.join(CLI.setup_folder, "token"), res[:token] + File.write File.join(CLI.setup_folder, "user_id"), res[:user_id] + Tsks::Actions.update_tsks_with_uuid res[:user_id] puts "Succesfully logged in." elsif res && res[:status_code] == 403 puts "Invalid e-mail or password." end end @@ -119,14 +124,16 @@ if !File.exist? File.join CLI.setup_folder, "token" return puts "Please, login before try to sync." end + user_id = File.read File.join CLI.setup_folder, "user_id" + Tsks::Actions.update_tsks_with_uuid user_id + local_tsks = Tsks::Storage.select_all local_id=false + token = File.read File.join CLI.setup_folder, "token" get_res = Tsks::Request.get "/tsks", token - local_tsks = Tsks::Storage.select_all remote_tsks = [] - for tsk in get_res[:tsks] tsk[:created_at] = Time.parse(tsk[:created_at]).strftime "%F %T" tsk[:updated_at] = Time.parse(tsk[:updated_at]).strftime "%F %T" remote_tsks.append tsk end