Class: CslCli::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/csl_cli/cli.rb

Instance Method Summary collapse

Instance Method Details

#listObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/csl_cli/cli.rb', line 73

def list
  token = nil
  decoded_token = {}
  query = ""

  app_url = ENV['CSL_CLI_APP_URL']

  abort 'app_url missing, please check environment variables' if app_url.nil?

  begin
    tokenstore = CslCli::Tokenstore.new(ENV['HOME'])
    token = tokenstore.load
  rescue
    abort 'Failed loading token from disk. Are you logged in?'
  end

  decoded_token['header'] = JSON.parse(Base64.decode64(token.split('.')[0]))
  decoded_token['payload'] = JSON.parse(Base64.decode64(token.split('.')[1]))

  query = "?limited=true" unless options[:show_all]

  headers = {"Content-Type" => "application/json", "Authorization" => "Bearer #{token}", "Accept" => "application/json"}
  request = CslCli::Request::Get.new(app_url + "/switches/" + query, headers)
  if request.code < 400
    response = request.response

    case options[:format]
    when "csv"
      tp.set :separator, ","
      tp.set :max_width, 100000
    when "json"
      puts response.to_json
      exit
    end

    if options[:show_all]
      tp response, :except => [:updated_at, :url, :id]
    else
      tp response, :except => [:updated_at, :url, :id, :user_id]
    end
  else
    abort "Failed to list notes: #{request.response}"
  end
end

#loginObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/csl_cli/cli.rb', line 11

def 
  email = ENV['CSL_CLI_EMAIL']
  password = ENV['CSL_CLI_PASSWORD']
  app_url = ENV['CSL_CLI_APP_URL']

  abort 'email missing, please check environment variables' if email.nil?
  abort 'password missing, please check environment variables' if password.nil?
  abort 'app_url missing, please check environment variables' if app_url.nil?

  auth = CslCli::Auth.new(app_url)
  logged_in = auth.(email, password)
  if logged_in
    tokenstore = CslCli::Tokenstore.new(ENV['HOME'])
    tokenstore.store(auth.token)
    puts "Successfully logged in (token saved to disk)"
  else
    abort 'wrong credentials'
  end


end

#logoutObject



34
35
36
37
38
# File 'lib/csl_cli/cli.rb', line 34

def logout
  tokenstore = CslCli::Tokenstore.new(ENV['HOME'])
  tokenstore.clear
  puts "Successfully logged out (removed token from disk)"
end

#switch(note) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/csl_cli/cli.rb', line 41

def switch(note)
  token = nil
  decoded_token = {}

  app_url = ENV['CSL_CLI_APP_URL']

  abort 'app_url missing, please check environment variables' if app_url.nil?

  begin
    tokenstore = CslCli::Tokenstore.new(ENV['HOME'])
    token = tokenstore.load
  rescue
    abort 'Failed loading token from disk. Are you logged in?'
  end

  decoded_token['header'] = JSON.parse(Base64.decode64(token.split('.')[0]))
  decoded_token['payload'] = JSON.parse(Base64.decode64(token.split('.')[1]))

  headers = {"Content-Type" => "application/json", "Authorization" => "Bearer #{token}", "Accept" => "application/json"}
  body = {note: note, user_id: decoded_token['payload']['sub']['$oid']}.to_json
  request = CslCli::Request::Post.new(app_url + "/switches/", body, headers)
  if request.code < 300
    response = request.response
    puts "Successfully created new context switch: #{response['created_at']}: #{response['id']}: #{response['note']}"
  else
    abort "Failed to create new note: #{request.response}"
  end
end