Module: Tr3llo::Application

Extended by:
Application
Included in:
Application
Defined in:
lib/3llo/application.rb

Constant Summary collapse

DEFAULT_CONFIG_FILE_PATH =
"~/.3llo.config.json".freeze

Instance Method Summary collapse

Instance Method Details

#build_option_parserObject



71
72
73
74
75
76
77
78
79
# File 'lib/3llo/application.rb', line 71

def build_option_parser()
  OptionParser.new do |parser|
    parser.program_name = "3llo v#{Tr3llo::VERSION}"
    parser.on("-h", "--help", "show this message")
    parser.on("-i", "--init=INIT", String, "the init command to run when the program starts")
    parser.on("-c", "--config=CONFIG", String, "file path to the config file")
    parser.on("--configure", "set up configuration")
  end
end

#execute_configure!(interface) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/3llo/application.rb', line 43

def execute_configure!(interface)
  interface.puts("Please key in the following information:")
  interface.puts("Tips: Visit https://trello.com/app-key to obtain the credentials.\n\n")

  key = interface.input.ask("API key:", required: true)
  token = interface.input.ask("API token:", required: true)

  if API::Token.verify(key, token)
    file_content = JSON.dump({"key" => key, "token" => token})
    File.write(File.expand_path(DEFAULT_CONFIG_FILE_PATH), file_content)

    message = "Configuration has been saved to" + Utils.paint(DEFAULT_CONFIG_FILE_PATH, "green") + "."
    interface.puts(message)
  else
    error_message = "Either key or token is invalid. Please try again."
    interface.print_error(error_message)
  end
end

#execute_help!(parser, interface) ⇒ Object



39
40
41
# File 'lib/3llo/application.rb', line 39

def execute_help!(parser, interface)
  interface.puts(parser.help)
end

#fetch_board!Object



151
152
153
154
155
# File 'lib/3llo/application.rb', line 151

def fetch_board!()
  $application.resolve(:board)
rescue ::Container::KeyNotFoundError
  raise BoardNotSelectedError
end

#fetch_client!Object



169
170
171
# File 'lib/3llo/application.rb', line 169

def fetch_client!()
  $application.resolve(:api_client)
end

#fetch_configuration!Object



161
162
163
# File 'lib/3llo/application.rb', line 161

def fetch_configuration!()
  $application.resolve(:configuration)
end

#fetch_interface!Object



165
166
167
# File 'lib/3llo/application.rb', line 165

def fetch_interface!()
  $application.resolve(:interface)
end

#fetch_registry!Object



173
174
175
# File 'lib/3llo/application.rb', line 173

def fetch_registry!()
  $application.resolve(:registry)
end

#fetch_user!Object



157
158
159
# File 'lib/3llo/application.rb', line 157

def fetch_user!()
  $application.resolve(:user)
end

#get_config_entry(config, key, env_key) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/3llo/application.rb', line 128

def get_config_entry(config, key, env_key)
  config.fetch(key) do
    if ENV.has_key?(env_key)
      Utils.deprecate!(
        "Setting #{env_key.inspect} as an environment variable is deprecated. " \
          "It will be removed in the future versions of 3llo. Please use config file instead."
      )

      ENV.fetch(env_key)
    else
      raise KeyError.new(key: key)
    end
  end
end

#load_configuration!(config_file) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/3llo/application.rb', line 98

def load_configuration!(config_file)
  config_path = File.expand_path(config_file)

  config =
    if File.exist?(config_path)
      JSON.load(File.read(config_path))
    else
      {}
    end

  configuration = Tr3llo::Configuration.new

  configuration.api_key = get_config_entry(config, "key", "TRELLO_KEY")
  configuration.api_token = get_config_entry(config, "token", "TRELLO_TOKEN")

  configuration.finalize!()

  $application.register(:configuration, configuration)
rescue KeyError => exception
  command_string = "3llo --configure"

  abort(
    Utils.paint(
      "#{exception.key.inspect} has not been configured. " \
        "Please run #{command_string.inspect} to set up configuration.",
      "red"
    )
  )
end

#load_user!(interface) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/3llo/application.rb', line 177

def load_user!(interface)
  user = Tr3llo::API::User.find("me")
  decorated_username = Utils.format_highlight("@#{user.username}")

  interface.print_frame do
    interface.puts("You're logged in as #{decorated_username}")
  end

  $application.register(:user, user)
end

#parse_cli_args!(parser, args) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/3llo/application.rb', line 62

def parse_cli_args!(parser, args)
  options = {}
  parser.parse!(args, into: options)

  options
rescue OptionParser::InvalidArgument, OptionParser::InvalidOption
  {}
end


81
82
83
84
85
# File 'lib/3llo/application.rb', line 81

def print_help!(interface)
  interface.print_frame do
    interface.puts(View::Help.render())
  end
end

#register_api_client!Object



87
88
89
90
91
# File 'lib/3llo/application.rb', line 87

def register_api_client!()
  remote_server = Tr3llo::RemoteServer.new("https://api.trello.com/1")

  $application.register(:api_client, remote_server)
end

#register_board!(board) ⇒ Object



147
148
149
# File 'lib/3llo/application.rb', line 147

def register_board!(board)
  $application.register(:board, board)
end

#register_interface!Object



93
94
95
96
# File 'lib/3llo/application.rb', line 93

def register_interface!()
  prompt = TTY::Prompt.new()
  $application.register(:interface, Tr3llo::Interface.new(prompt, $stdout))
end

#register_registry!Object



143
144
145
# File 'lib/3llo/application.rb', line 143

def register_registry!()
  $application.register(:registry, Tr3llo::Registry.new)
end

#start(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/3llo/application.rb', line 7

def start(args)
  $application = Container.new()

  interface = register_interface!()
  option_parser = build_option_parser()
  register_api_client!()

  options = parse_cli_args!(option_parser, args)

  if options.has_key?(:help)
    execute_help!(option_parser, interface)
    exit
  end

  if options.has_key?(:configure)
    execute_configure!(interface)
    exit
  end

  init_command = options.fetch(:init, "")
  config_file = options.fetch(:config, DEFAULT_CONFIG_FILE_PATH)

  print_help!(interface)

  load_configuration!(config_file)
  register_registry!()

  load_user!(interface)

  Controller.start(init_command)
end