csl_cli 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e2b401c7ad704b3ce2b9b10ebb78178246718f28
4
+ data.tar.gz: 8af2e93c4b863f2e7983e707a452e2d20ab72d4f
5
+ SHA512:
6
+ metadata.gz: cd5ffb43aacc1a3ee315b2e63d18d9166374d9a7f58c7a6be30e72631ec22452250f9404dbf06046cd62125142e63b80bbbc8151f2dff87e5f9fb17d2dff55f1
7
+ data.tar.gz: b2dc582f6d06b9d863fe579640576480f51cadc650ca62481f6eabf280f6a77425e373f6fdd176c68fb30a4552b66c7a9afeeaebc1384bcded840ba05dd80638
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in csl_cli.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # CslCli
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/csl_cli`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'csl_cli'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install csl_cli
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/csl_cli.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "csl_cli"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/csl_cli.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'csl_cli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "csl_cli"
8
+ spec.version = CslCli::VERSION
9
+ spec.authors = ["Nils Bartels"]
10
+ spec.email = ["gemcslcli@schrohm.de"]
11
+
12
+ spec.summary = %q{Context switch logger cli.}
13
+ spec.homepage = "https://github.com/brtz/csl-cli"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "httparty", "~> 0.14.0"
24
+ spec.add_dependency "thor"
25
+ spec.add_dependency "table_print"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.13"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "rubocop"
31
+
32
+ end
data/exe/csl ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'csl_cli/cli'
4
+ CslCli::CLI.start
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ require 'json'
3
+
4
+ module CslCli
5
+ class Auth
6
+ @token = nil
7
+ @decoded_token = nil
8
+ @csl_app_url = nil
9
+
10
+ attr_reader :token
11
+ attr_reader :decoded_token
12
+ attr_reader :csl_app_url
13
+
14
+ def initialize(api_url)
15
+ @csl_app_url = api_url
16
+ end
17
+
18
+ def login(email, password)
19
+ credentials = {auth: {email: email, password: password}}.to_json
20
+ request_auth = CslCli::Request::Post.new(@csl_app_url + '/user_token', credentials, {"Content-Type" => "application/json"})
21
+ if request_auth.code < 300
22
+ @token = JSON.parse(request_auth.response.body)["jwt"]
23
+ @decoded_token = {}
24
+ @decoded_token['header'] = JSON.parse(Base64.decode64(@token.split('.')[0]))
25
+ @decoded_token['payload'] = JSON.parse(Base64.decode64(@token.split('.')[1]))
26
+
27
+ true
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,114 @@
1
+ require 'csl_cli'
2
+ require 'thor'
3
+ require 'table_print'
4
+ require 'json'
5
+ require 'base64'
6
+
7
+ module CslCli
8
+ class CLI < Thor
9
+
10
+ desc 'login', 'Log in at csl app using environmet variables (csl_cli_email, csl_cli_password, csl_cli_app_url)'
11
+ def login
12
+ email = ENV['CSL_CLI_EMAIL']
13
+ password = ENV['CSL_CLI_PASSWORD']
14
+ app_url = ENV['CSL_CLI_APP_URL']
15
+
16
+ abort 'email missing, please check environment variables' if email.nil?
17
+ abort 'password missing, please check environment variables' if password.nil?
18
+ abort 'app_url missing, please check environment variables' if app_url.nil?
19
+
20
+ auth = CslCli::Auth.new(app_url)
21
+ logged_in = auth.login(email, password)
22
+ if logged_in
23
+ tokenstore = CslCli::Tokenstore.new(ENV['HOME'])
24
+ tokenstore.store(auth.token)
25
+ puts "Successfully logged in (token saved to disk)"
26
+ else
27
+ abort 'wrong credentials'
28
+ end
29
+
30
+
31
+ end
32
+
33
+ desc 'logout', 'removes the token from disk'
34
+ def logout
35
+ tokenstore = CslCli::Tokenstore.new(ENV['HOME'])
36
+ tokenstore.clear
37
+ puts "Successfully logged out (removed token from disk)"
38
+ end
39
+
40
+ desc 'switch NOTE', 'creates a context switch with NOTE'
41
+ def switch(note)
42
+ token = nil
43
+ decoded_token = {}
44
+
45
+ app_url = ENV['CSL_CLI_APP_URL']
46
+
47
+ abort 'app_url missing, please check environment variables' if app_url.nil?
48
+
49
+ begin
50
+ tokenstore = CslCli::Tokenstore.new(ENV['HOME'])
51
+ token = tokenstore.load
52
+ rescue
53
+ abort 'Failed loading token from disk. Are you logged in?'
54
+ end
55
+
56
+ decoded_token['header'] = JSON.parse(Base64.decode64(token.split('.')[0]))
57
+ decoded_token['payload'] = JSON.parse(Base64.decode64(token.split('.')[1]))
58
+
59
+ headers = {"Content-Type" => "application/json", "Authorization" => "Bearer #{token}", "Accept" => "application/json"}
60
+ body = {note: note, user_id: decoded_token['payload']['sub']['$oid']}.to_json
61
+ request = CslCli::Request::Post.new(app_url + "/switches/", body, headers)
62
+ if request.code < 300
63
+ response = request.response
64
+ puts "Successfully created new context switch: #{response['created_at']}: #{response['id']}: #{response['note']}"
65
+ else
66
+ abort "Failed to create new note: #{request.response}"
67
+ end
68
+ end
69
+
70
+ desc 'list', 'lists your context switches'
71
+ option :show_all, type: :boolean, aliases: '-s'
72
+ option :format, type: :string, aliases: '-f'
73
+ def list
74
+ token = nil
75
+ decoded_token = {}
76
+ query = ""
77
+
78
+ app_url = ENV['CSL_CLI_APP_URL']
79
+
80
+ abort 'app_url missing, please check environment variables' if app_url.nil?
81
+
82
+ begin
83
+ tokenstore = CslCli::Tokenstore.new(ENV['HOME'])
84
+ token = tokenstore.load
85
+ rescue
86
+ abort 'Failed loading token from disk. Are you logged in?'
87
+ end
88
+
89
+ decoded_token['header'] = JSON.parse(Base64.decode64(token.split('.')[0]))
90
+ decoded_token['payload'] = JSON.parse(Base64.decode64(token.split('.')[1]))
91
+
92
+ query = "?limited=true" unless options[:show_all]
93
+
94
+ headers = {"Content-Type" => "application/json", "Authorization" => "Bearer #{token}", "Accept" => "application/json"}
95
+ request = CslCli::Request::Get.new(app_url + "/switches/" + query, headers)
96
+ if request.code < 400
97
+ response = request.response
98
+ if options[:format] == 'csv'
99
+ tp.set :separator, ","
100
+ tp.set :max_width, 100000
101
+ end
102
+
103
+ if options[:show_all]
104
+ tp response, :except => [:updated_at, :url, :id]
105
+ else
106
+ tp response, :except => [:updated_at, :url, :id, :user_id]
107
+ end
108
+ else
109
+ abort "Failed to list notes: #{request.response}"
110
+ end
111
+ end
112
+
113
+ end
114
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+ require 'httparty'
3
+
4
+ module CslCli
5
+ module Request
6
+ @response = nil
7
+ @code = nil
8
+
9
+ class Post
10
+ attr_reader :response
11
+ attr_reader :code
12
+
13
+ def initialize(url, body, headers)
14
+ @response = HTTParty.post(url, body: body, headers: headers)
15
+ @code = @response.code
16
+ end
17
+ end
18
+
19
+ class Get
20
+ attr_reader :response
21
+ attr_reader :code
22
+
23
+ def initialize(url, headers)
24
+ @response = HTTParty.get(url, headers: headers)
25
+ @code = @response.code
26
+ end
27
+ end
28
+
29
+ class Put
30
+ attr_reader :response
31
+ attr_reader :code
32
+
33
+ def initialize(url, body, headers)
34
+ @response = HTTParty.put(url, body: body, headers: headers)
35
+ @code = @response.code
36
+ end
37
+ end
38
+
39
+ class Delete
40
+ attr_reader :response
41
+ attr_reader :code
42
+
43
+ def initialize(url, body, headers)
44
+ @response = HTTParty.delete(url, body: body, headers: headers)
45
+ @code = @response.code
46
+ end
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ module CslCli
4
+ class Tokenstore
5
+ @path = ""
6
+ @saved_token = nil
7
+ @tokenfile = nil
8
+
9
+ attr_reader :path
10
+
11
+ def initialize(path)
12
+ @path = path
13
+ @tokenfile = File.join(@path, '.csl_token')
14
+ end
15
+
16
+ def load
17
+ file = File.open(@tokenfile, 'rb')
18
+ @saved_token = file.read()
19
+ file.close
20
+ return @saved_token
21
+ end
22
+
23
+ def store(token)
24
+ file = File.open(@tokenfile, 'w')
25
+ file.write(token)
26
+ file.close
27
+ File.chmod(0600, @tokenfile)
28
+ return true
29
+ end
30
+
31
+ def clear
32
+ File.delete(@tokenfile) if File.exist?(@tokenfile)
33
+ return true
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module CslCli
2
+ VERSION = "1.0.0"
3
+ end
data/lib/csl_cli.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'csl_cli/version'
2
+
3
+ module CslCli
4
+ autoload 'Request', 'csl_cli/request'
5
+ autoload 'Auth', 'csl_cli/auth'
6
+ autoload 'Tokenstore', 'csl_cli/tokenstore'
7
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csl_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nils Bartels
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.14.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.14.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: table_print
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - gemcslcli@schrohm.de
114
+ executables:
115
+ - csl
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - csl_cli.gemspec
129
+ - exe/csl
130
+ - lib/csl_cli.rb
131
+ - lib/csl_cli/auth.rb
132
+ - lib/csl_cli/cli.rb
133
+ - lib/csl_cli/request.rb
134
+ - lib/csl_cli/tokenstore.rb
135
+ - lib/csl_cli/version.rb
136
+ homepage: https://github.com/brtz/csl-cli
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.5.2
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Context switch logger cli.
160
+ test_files: []