Class: HledgerForecast::Cli

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

Overview

The Command Line Interface for the application Takes user arguments and translates them into actions

Class Method Summary collapse

Class Method Details

.compare(options) ⇒ Object



296
297
298
299
300
301
302
# File 'lib/hledger_forecast/cli.rb', line 296

def self.compare(options)
  if !File.exist?(options[:file1]) || !File.exist?(options[:file2])
    return puts("\nError: ".bold.red + "One or more of the files could not be found to compare")
  end

  puts(Comparator.compare(options[:file1], options[:file2]))
end

.generate(options) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/hledger_forecast/cli.rb', line 254

def self.generate(options)
  forecast = File.read(options[:forecast_file])

  begin
    transactions = Generator.generate(forecast, options)
  rescue StandardError => e
    puts("An error occurred while generating transactions: #{e.message}")
    exit(1)
  end

  output_file = options[:output_file]

  if File.exist?(output_file) && !options[:force]
    print("\nFile '#{output_file}' already exists. Overwrite? (y/n): ")
    overwrite = gets.chomp.downcase

    if overwrite == "y"
      File.write(output_file, transactions)
      puts("\nSuccess: ".bold.green + "File '#{output_file}' has been overwritten.")
    else
      puts("\nInfo: ".bold.blue + "Operation aborted. File '#{output_file}' was not overwritten.")
    end
  else
    File.write(output_file, transactions)
    puts("\nSuccess: ".bold.green + "File '#{output_file}' has been created")
  end
end

.parse_commands(args = ARGV, _stdin = $stdin) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
69
70
71
72
# File 'lib/hledger_forecast/cli.rb', line 19

def self.parse_commands(args = ARGV, _stdin = $stdin)
  command = nil
  options = {}

  global = OptionParser.new do |opts|
    opts.banner = "Usage: hledger-forecast [command] [options]"
    opts.separator("")
    opts.separator("Commands:")
    opts.separator("  generate    Generate a forecast from a file")
    opts.separator("  summarize   Summarize the forecast file and output to the terminal")
    opts.separator("  compare     Compare and highlight the differences between two CSV files")
    opts.separator("")
    opts.separator("Options:")

    opts.on_tail("-h", "--help", "Show this help message") do
      puts(opts)
      exit
    end

    opts.on_tail("-v", "--version", "Show the installed version") do
      puts(VERSION)
      exit
    end
  end

  if args.empty?
    puts(global)
    exit(1)
  end

  begin
    global.order!(args)
    command = args.shift || "generate"
  rescue OptionParser::InvalidOption => e
    puts(e)
    puts(global)
    exit(1)
  end

  case command
  when "generate"
    options = parse_generate_options(args)
  when "summarize"
    options = parse_summarize_options(args)
  when "compare"
    options = parse_compare_options(args)
  else
    puts("Unknown command: #{command}")
    puts(global)
    exit(1)
  end

  return command, options
end

.parse_compare_options(args) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/hledger_forecast/cli.rb', line 227

def self.parse_compare_options(args)
  options = {}

  global = OptionParser.new do |opts|
    opts.banner = "Usage: hledger-forecast compare [path/to/file1.csv] [path/to/file2.csv]"
    opts.separator("")
  end

  begin
    global.parse!(args)
  rescue OptionParser::InvalidOption => e
    puts(e)
    puts(global)
    exit(1)
  end

  if args[0].nil? || args[1].nil?
    puts(global)
    exit(1)
  end

  options[:file1] = args[0]
  options[:file2] = args[1]

  options
end

.parse_generate_options(args) ⇒ Object



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/hledger_forecast/cli.rb', line 74

def self.parse_generate_options(args)
  options = {}

  global = OptionParser.new do |opts|
    opts.banner = "Usage: hledger-forecast generate [options]"
    opts.separator("")

    opts.on(
      "-f",
      "--forecast FILE",
      "The path to the FORECAST csv file to generate from"
    ) do |file|
      options[:forecast_file] = file
      options[:output_file] ||= file.sub("csv", "journal")
    end

    opts.on(
      "-o",
      "--output-file FILE",
      "The path to the OUTPUT file to create"
    ) do |file|
      options[:output_file] = file
    end

    opts.on(
      "-v",
      "--verbose",
      "Do not group transactions in the output file"
    ) do
      options[:verbose] = true
    end

    opts.on(
      "-t",
      "--tags TAGS",
      "Only include transactions with given tags (comma-separated)"
    ) do |tags|
      options[:tags] = tags.split(",").map(&:strip)
    end

    opts.on(
      "--force",
      "Force an overwrite of the output file"
    ) do
      options[:force] = true
    end

    opts.on_tail("-h", "--help", "Show this help message") do
      puts(opts)
      exit
    end
  end

  begin
    global.parse!(args)
  rescue OptionParser::InvalidOption => e
    puts(e)
    puts(global)
    exit(1)
  end

  if options.empty?
    puts(global)
    exit(1)
  end

  options[:forecast_file] ||= "forecast.csv"
  options[:file_type] ||= "csv"
  options[:output_file] ||= "forecast.journal"

  options
end

.parse_summarize_options(args) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/hledger_forecast/cli.rb', line 147

def self.parse_summarize_options(args)
  options = {}

  global = OptionParser.new do |opts|
    opts.banner = "Usage: hledger-forecast summarize [options]"
    opts.separator("")

    opts.on(
      "-f",
      "--forecast FILE",
      "The path to the FORECAST csv file to summarize"
    ) do |file|
      options[:forecast_file] = file
    end

    opts
      .on(
        "-r",
        "--roll-up PERIOD",
        "The period to roll-up your forecasts into. One of:",
        "[yearly], [half-yearly], [quarterly], [monthly], [weekly], [daily]"
      ) do |rollup|
        options[:roll_up] = rollup
      end

    opts.on(
      "-t",
      "--tags TAGS",
      "Only include transactions with given tags (comma-separated)"
    ) do |tags|
      options[:tags] = tags.split(",").map(&:strip)
    end

    opts.on(
      "-v",
      "--verbose",
      "Show additional information in the summary"
    ) do |_|
      options[:verbose] = true
    end

    # opts.on("--from DATE",
    #         "Include transactions that start FROM a given DATE [yyyy-mm-dd]") do |from|
    #   options[:from] = from
    # end
    #
    # opts.on("--to DATE",
    #         "Include transactions that run TO a given DATE [yyyy-mm-dd]") do |to|
    #   options[:to] = to
    # end

    # opts.on("-s", "--scenario \"NAMES\"",
    #         "Include transactions from given scenarios, e.g.:",
    #         "\"base, rennovation, car purchase\"") do |_scenario|
    #   # Loop through scenarios, seperated by a comma
    #   options[:scenario] = {}
    # end

    opts.on_tail("-h", "--help", "Show this help message") do
      puts(opts)
      exit
    end
  end

  begin
    global.parse!(args)
  rescue OptionParser::InvalidOption => e
    puts(e)
    puts(global)
    exit(1)
  end

  if options.empty?
    puts(global)
    exit(1)
  end

  options
end

.run(command, options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hledger_forecast/cli.rb', line 5

def self.run(command, options)
  case command
  when "generate"
    generate(options)
  when "summarize"
    summarize(options)
  when "compare"
    compare(options)
  else
    puts("Unknown command: #{command}")
    exit(1)
  end
end

.summarize(options) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/hledger_forecast/cli.rb', line 282

def self.summarize(options)
  config = File.read(options[:forecast_file])
  config = HledgerForecast::CSVParser.parse(config) if options[:file_type] == "csv"

  begin
    summarizer = Summarizer.summarize(config, options)
  rescue StandardError => e
    puts("An error occurred while summarizing transactions: #{e.message}")
    exit(1)
  end

  puts(SummarizerFormatter.format(summarizer[:output], summarizer[:settings]))
end