asana-cli 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ require 'asana-cli/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "asana-cli"
8
- gem.version = Asana::Cli::VERSION
8
+ gem.version = Asana::CLI::VERSION
9
9
  gem.authors = ["Chris Heald"]
10
10
  gem.email = ["cheald@gmail.com"]
11
11
  gem.description = %q{Command-line client for Asana}
data/bin/asana CHANGED
@@ -3,6 +3,10 @@ require 'trollop'
3
3
  require 'asana'
4
4
  require 'colorize'
5
5
 
6
+ lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
7
+ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
8
+ require 'asana-cli'
9
+
6
10
  parser = Trollop::Parser.new do
7
11
  opt :list, "List all workspaces"
8
12
  opt :workspace, "Workspace", :type => :string
@@ -11,7 +15,7 @@ parser = Trollop::Parser.new do
11
15
  opt :close, "Complete a task", :type => :integer
12
16
  opt :create, "Create new task", :type => :string
13
17
  opt :message, "Comment on a task", :type => :string
14
- opt :task, "Task to operate on", :type => :integer
18
+ opt :task, "Task to operate on", :type => :string
15
19
  opt :key, "API key to use", :type => :string
16
20
  opt :keyfile, "API keyfile to use", :default => "~/.asana_key"
17
21
  end
@@ -35,44 +39,23 @@ Asana.configure do |client|
35
39
  end
36
40
  end
37
41
 
38
- if opts[:complete] or opts[:close]
39
- task_id = opts[:complete] || opts[:close] || opts[:task]
40
- Trollop::die "Task ID required to complete" unless task_id
41
- task = Asana::Task.find(task_id)
42
- task.create_story :text => opts[:message] if opts[:message]
43
- task.modify(:completed => true)
44
- puts "Task %s closed!".light_green % task_id
45
- elsif opts[:message]
46
- Trollop::die "Task ID required to complete" unless opts[:task]
47
- task = Asana::Task.find(opts[:task])
48
- task.create_story :text => opts[:message] if opts[:message]
49
- elsif opts[:project]
50
- project = Asana::Project.all.detect {|p| p.name.downcase == opts[:project].downcase }
51
- Trollop::die "Unable to find a project named #{opts[:project]}" unless project
52
- puts "\n"
53
- puts "%s\t Tasks" % project.name.light_green
54
- puts ("-" * 60).light_white
55
- project.tasks.each do |task|
56
- puts "%s\t%s" % [task.id.to_s.light_yellow, task.name.light_white]
57
- end
58
- puts "\n"
59
- elsif opts[:workspace]
60
- workspace = Asana::Workspace.all.detect {|p| p.name.downcase == opts[:workspace].downcase }
61
- Trollop::die "Unable to find a workspace named #{opts[:workspace]}" unless workspace
62
- puts "\n"
63
- puts "%s\tProjects" % workspace.name.light_green
64
- puts ("-" * 60).light_white
65
- workspace.projects.each do |project|
66
- puts "%s\t%s" % [project.id.to_s.light_yellow, project.name.light_white]
42
+ begin
43
+ if opts[:complete] or opts[:close]
44
+ task_id = opts[:complete] || opts[:close] || opts[:task]
45
+ raise Asana::CLIError.new "Task ID required to complete" unless task_id
46
+ Asana::CLI.close task_id
47
+ elsif opts[:message]
48
+ raise Asana::CLIError.new "Task ID required to complete" unless opts[:task]
49
+ Asana::CLI.comment opts[:task], opts[:message]
50
+ elsif opts[:task]
51
+ Asana::CLI.show_task opts[:task], opts[:project], opts[:workspace]
52
+ elsif opts[:project]
53
+ Asana::CLI.project_tasks opts[:project], opts[:workspace]
54
+ elsif opts[:workspace]
55
+ Asana::CLI.workspace_tasks opts[:workspace]
56
+ elsif opts[:list]
57
+ Asana::CLI.list_workspaces
67
58
  end
68
- puts "\n"
69
- elsif opts[:list]
70
- puts "\n"
71
- puts "Workspaces".light_green
72
- puts ("-" * 60).light_white
73
- Asana::Workspace.all.each do |workspace|
74
- puts "%s\t%s" % [workspace.id.to_s.light_yellow, workspace.name.light_white]
75
- end
76
- else
77
-
59
+ rescue Asana::CLIError => e
60
+ $stderr.puts e.message.light_red
78
61
  end
@@ -1,7 +1,123 @@
1
1
  require "asana-cli/version"
2
+ require 'colorize'
3
+ require 'asana'
2
4
 
3
5
  module Asana
4
- module Cli
5
- # Your code goes here...
6
+ class CLIError < StandardError; end
7
+ module CLI
8
+ class << self
9
+ def close(task_id)
10
+ task = Asana::Task.find(task_id)
11
+ task.create_story :text => opts[:message] if opts[:message]
12
+ task.modify(:completed => true)
13
+ puts "Task %s closed!".light_green % task_id
14
+ end
15
+
16
+ def comment(task_id, message)
17
+ task = Asana::Task.find(task_id)
18
+ task.create_story :text => message
19
+ end
20
+
21
+ def project_tasks(project_id, workspace_id = nil)
22
+ projects = resolve workspace_id, project_id, nil
23
+ raise CLIError.new "Unable to find a project named #{project_id}" if projects.empty?
24
+
25
+ projects.each do |project|
26
+ puts "\n"
27
+ puts "%s\t Tasks" % project.name.light_green
28
+ puts ("-" * 60).light_white
29
+ project.tasks.each do |task|
30
+ puts "%s\t%s" % [task.id.to_s.light_yellow, task.name.light_white]
31
+ end
32
+ puts "\n"
33
+ end
34
+ end
35
+
36
+ def workspace_tasks(workspace_id)
37
+ workspaces = resolve workspace_id, nil, nil
38
+
39
+ raise CLIError.new "Unable to find a workspace named #{workspace_id}" if workspaces.empty?
40
+ workspaces.each do |workspace|
41
+ puts "\n"
42
+ puts "%s\tProjects" % workspace.name.light_green
43
+ puts ("-" * 60).light_white
44
+ workspace.projects.each do |project|
45
+ puts "%s\t%s" % [project.id.to_s.light_yellow, project.name.light_white]
46
+ end
47
+ puts "\n"
48
+ end
49
+ end
50
+
51
+ def list_workspaces
52
+ puts "\n"
53
+ puts "Workspaces".light_green
54
+ puts ("-" * 60).light_white
55
+ Asana::Workspace.all.each do |workspace|
56
+ puts "%s\t%s" % [workspace.id.to_s.light_yellow, workspace.name.light_white]
57
+ end
58
+ end
59
+
60
+ def show_task(task_id, project_id = nil, workspace_id = nil)
61
+ tasks = resolve workspace_id, project_id, task_id
62
+ tasks.each do |task|
63
+ puts task.name.light_green
64
+ puts "-" * 60
65
+ task.attributes.each do |key, val|
66
+ vals = val
67
+ if val.is_a? Array
68
+ vals = val.map {|v| v.respond_to?(:name) ? v.name : v.inspect }
69
+ elsif val.is_a? Asana::Workspace or val.is_a? Asana::Task::Assignee
70
+ vals = val.name
71
+ end
72
+
73
+ vals = vals.join(", ") if vals.is_a? Array
74
+ puts "%20s\t%s" % [key, vals.to_s.light_white]
75
+ end
76
+ puts "\n"
77
+ end
78
+ end
79
+
80
+ def resolve(w, p, t)
81
+ w_id = w.to_i if w and w.match(/^\d+$/)
82
+ p_id = p.to_i if p and p.match(/^\d+$/)
83
+ t_id = t.to_i if t and t.match(/^\d+$/)
84
+
85
+ if t
86
+ if t_id
87
+ return [Asana::Task.find(t_id)]
88
+ elsif p_id
89
+ Asana::Project.find(p_id).tasks.select {|_t| _t.name.downcase[t.downcase] }
90
+ else
91
+ if w_id
92
+ projects = Asana::Workspace.find(w_id).projects
93
+ else
94
+ projects = Asana::Workspace.all.flat_map &:projects
95
+ end
96
+ projects = projects.select {|_p| _p.name.downcase[p.downcase] } if p
97
+ return projects.flat_map do |project|
98
+ puts project.name
99
+ project.tasks.select {|_t| _t.name.downcase[t.downcase] }
100
+ end
101
+ end
102
+ elsif p
103
+ if p_id
104
+ return [Asana::Project.find(p_id)]
105
+ else
106
+ if w_id
107
+ projects = Asana::Workspace.find(w_id).projects
108
+ else
109
+ projects = Asana::Workspace.all.flat_map &:projects
110
+ end
111
+ return projects.select {|_p| _p.name.downcase[p.downcase] }
112
+ end
113
+ elsif w
114
+ if w_id
115
+ return [Asana::Workspace.find(w_id)]
116
+ else
117
+ Asana::Workspace.all.select {|_w| _w.name.downcase[w.downcase] }
118
+ end
119
+ end
120
+ end
121
+ end
6
122
  end
7
123
  end
@@ -1,5 +1,5 @@
1
1
  module Asana
2
- module Cli
3
- VERSION = "0.0.1"
2
+ module CLI
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asana-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: