Skip to content

Commit

Permalink
Merge pull request #13 from geolessel/app-setup
Browse files Browse the repository at this point in the history
feature: App setup
  • Loading branch information
geolessel authored Sep 6, 2018
2 parents 51a0a62 + dcfce32 commit 5bdc663
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 37 deletions.
33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,24 @@ and the result is this project.
```sh
brew install --HEAD geolessel/homebrew-repo/trello-cli
```
2. [Get an API token from Trello](https://trello.com/1/authorize?expiration=never&scope=read,write&response_type=token&name=trello-cli&key=3020057fffe933d81fe081eb4f8d126a)
3. Create a `.trello-cli` directory in your `$HOME` directory (like `/Users/geolessel`)
```sh
mkdir ~/.trello-cli
```
4. Create a `~/.trello-cli/secrets.json` file in and add keys
and values for `key` (your Trello API key), `token` (your Trello API token),
and `memberId` (your Trello member id).
```json
{
"token": "YOUR TRELLO TOKEN",
"memberId": "YOUR MEMBER ID"
}
2. Run `trello`

The application should take you through a setup process that will direct
you to Trello's site asking if you'll grant the app access to your Trello
account. Once you accept, Trello will provide you with an API token. Just
paste that into the line asking for your token and it will do the rest.

```
--| This app requires access to your trello account. |--
You can get your member id by visiting this URL:
https://api.trello.com/1/members/me?key=3020057fffe933d81fe081eb4f8d126a&token={YOUR_TOKEN}
I'll open up a web page requesting access. Once you accept, you will
be presented with an API token. Copy that and use it in the next step.
Press ENTER to continue
This will return JSON and inside of that, you can find your `"id"`.
Token: <PASTE YOUR TOKEN HERE>
Completing setup
Done.
```

## Usage

Expand Down Expand Up @@ -71,7 +70,7 @@ For now, you need to build it yourself and you must have the

## Contributing

1. Fork it (<https://github.com/geolessel/trello/fork>)
1. Fork it (<https://github.com/geolessel/trello-cli/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
Expand Down
12 changes: 6 additions & 6 deletions src/api.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ class API
API_ROOT = "https://api.trello.com/1/"

def self.get(path : String, params : String)
url = "#{API_ROOT}/#{path}?#{App::CREDENTIALS}&#{params}"
App::LOG.debug("GETting URL: #{url}")
url = "#{API_ROOT}/#{path}?#{App.credentials}&#{params}"
App.log.debug("GETting URL: #{url}")
response = HTTP::Client.get(url)
json = JSON.parse(response.body)
json
end

def self.post(path : String, params : String)
url = "#{API_ROOT}/#{path}?#{App::CREDENTIALS}&#{params}"
App::LOG.debug("POSTing URL: #{url}")
url = "#{API_ROOT}/#{path}?#{App.credentials}&#{params}"
App.log.debug("POSTing URL: #{url}")
response = HTTP::Client.post(url)
end

def self.put(path : String, params : String)
url = "#{API_ROOT}/#{path}?#{App::CREDENTIALS}&#{params}"
App::LOG.debug("PUTting URL: #{url}")
url = "#{API_ROOT}/#{path}?#{App.credentials}&#{params}"
App.log.debug("PUTting URL: #{url}")
response = HTTP::Client.put(url)
end
end
78 changes: 73 additions & 5 deletions src/app.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ require "ncurses"
require "logger"

class App
SECRETS = JSON.parse(File.read("#{ENV["HOME"]}/.trello-cli/secrets.json"))
CREDENTIALS = "key=3020057fffe933d81fe081eb4f8d126a&token=#{SECRETS["token"]}"
MEMBER_ID = SECRETS["memberId"]

LOG = Logger.new(File.open("#{ENV["HOME"]}/.trello-cli/log.txt", "w"), level: Logger::DEBUG)
CONFIG_DIR = "#{ENV["HOME"]}/.trello-cli"
APP_KEY = "3020057fffe933d81fe081eb4f8d126a"

@@windows : Array(Window) = [] of Window
@@member_id : String = ""
@@secrets : JSON::Any = JSON::Any.new("{}")
@@token : String | Nil = ""
@@log : Logger = Logger.new(nil)

def self.init
@@secrets = JSON.parse(File.read("#{CONFIG_DIR}/secrets.json"))
@@token = App.secrets["token"].to_s
@@member_id = @@secrets["memberId"].to_s
@@log = Logger.new(File.open("#{CONFIG_DIR}/log.txt", "w"), level: Logger::DEBUG)
end

def self.activate_window(window : Window)
@@active_window = window
Expand All @@ -34,6 +42,32 @@ class App
@@windows
end

def self.credentials
"key=#{APP_KEY}&token=#{@@token}"
end

def self.member_id
@@member_id
end

def self.secrets
@@secrets
end

def self.log
@@log
end

def self.run_setup
Setup.make_config_dir
Setup.display_intro_text
@@token = Setup.get_token
@@member_id = Setup.fetch_member_id
Setup.write_config(@@token, @@member_id)
puts "Done."
sleep 1
end

module Colors
extend self

Expand All @@ -53,4 +87,38 @@ class App
NCurses::ColorPair.new(4).init(NCurses::Color::YELLOW, NCurses::Color::BLACK).attr
end
end

module Setup
extend self

def make_config_dir
Dir.mkdir_p(CONFIG_DIR)
end

def display_intro_text
puts
puts "--| This app requires access to your trello account. |--"
puts
puts "I'll open up a web page requesting access. Once you accept, you will"
puts "be presented with an API token. Copy that and use it in the next step."
puts "Press ENTER to continue"
gets
end

def get_token
`open 'https://trello.com/1/authorize?expiration=never&scope=read,write&response_type=token&name=trello-cli&key=#{APP_KEY}'`
print "Token: "
gets
end

def fetch_member_id
puts "Completing setup"
json = API.get("members/me", "")
json["id"].to_s
end

def write_config(token, member_id)
File.write("#{App::CONFIG_DIR}/secrets.json", "{\"token\": \"#{token}\", \"memberId\": \"#{member_id}\"}")
end
end
end
2 changes: 1 addition & 1 deletion src/card_action/card_action.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CardAction
end

def title
App::LOG.debug("unhandled action: #{type}")
App.log.debug("unhandled action: #{type}")
""
end

Expand Down
8 changes: 4 additions & 4 deletions src/card_detail.cr
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ class CardDetail
end

def add_self_as_member
response = API.post("/cards/#{@id}/idMembers", "value=#{App::MEMBER_ID}")
response = API.post("/cards/#{@id}/idMembers", "value=#{App.member_id}")
if response.success?
fetch
else
App::LOG.debug("failed to add self as member: #{response.inspect}")
App.log.debug("failed to add self as member: #{response.inspect}")
end
end

Expand All @@ -66,7 +66,7 @@ class CardDetail
if response.success?
fetch
else
App::LOG.debug("failed to add label to card: #{response.inspect}")
App.log.debug("failed to add label to card: #{response.inspect}")
end
end

Expand All @@ -75,7 +75,7 @@ class CardDetail
if response.success?
fetch
else
App::LOG.debug("failed to move card to list: #{response.inspect}")
App.log.debug("failed to move card to list: #{response.inspect}")
end
end
end
2 changes: 1 addition & 1 deletion src/cards_window.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CardsWindow < OptionSelectWindow

def render_row(option)
member_ids = option.json.as_h["members"].as_a.map { |m| m["id"].to_s }
if member_ids.includes?(App::MEMBER_ID)
if member_ids.includes?(App.member_id)
win.attron(App::Colors.yellow)
super
win.attroff(App::Colors.yellow)
Expand Down
2 changes: 1 addition & 1 deletion src/option_select_window.cr
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class OptionSelectWindow < Window
win.add_help(key: "h", description: "Go back")
end
else
App::LOG.debug("Unhandled key: #{key}")
App.log.debug("Unhandled key: #{key}")
end
end

Expand Down
11 changes: 9 additions & 2 deletions src/trello.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require "ncurses"
require "./*"

module Trello
VERSION = "0.1.0"
VERSION = "0.2.0"

def self.start
LibNCurses.setlocale(0, "") # enable unicode
Expand Down Expand Up @@ -46,4 +46,11 @@ module Trello
end
end

Trello.start unless ENV.fetch("CRYSTAL_ENV", nil) == "test"
unless ENV.fetch("CRYSTAL_ENV", nil) == "test"
if !File.exists?("#{App::CONFIG_DIR}/secrets.json")
App.run_setup
end

App.init
Trello.start
end

0 comments on commit 5bdc663

Please sign in to comment.