lib/drive_env/cli/spreadsheet.rb in drive_env-0.4.0 vs lib/drive_env/cli/spreadsheet.rb in drive_env-0.4.1

- old
+ new

@@ -1,9 +1,10 @@ require 'thor' require 'text-table' require 'google_drive' require 'drive_env' +require 'json' module DriveEnv module Cli class Spreadsheet < Thor desc 'show SPREADSHEET_URL_OR_ALIAS', '' @@ -20,22 +21,37 @@ puts table.to_s end desc 'to_env SPREADSHEET_URL_OR_ALIAS', '' + option :format, type: 'string', default: 'dotenv', enum: %w[dotenv json], + desc: 'output format' def to_env(url_or_alias) ws = worksheet(url_or_alias) + + envs = [] ws.rows.each.with_index do |row, idx| row_to_env(row) do |key, value, comment| + envs << { key: key, value: value, comment: comment } + end + end + + case options[:format] + when 'dotenv' + envs.each do |env| case - when key && value && comment - puts "#{key}=#{value} #{comment}" - when key && value && !comment - puts "#{key}=#{value}" - when !key && !value && comment - puts comment + when env[:key] && env[:value] && env[:comment] + puts "#{env[:key]}=#{env[:value]} #{env[:comment]}" + when env[:key] && env[:value] && !env[:comment] + puts "#{env[:key]}=#{env[:value]}" + when !env[:key] && !env[:value] && env[:comment] + puts "#{env[:comment]}" end end + when 'json' + puts JSON.pretty_generate( + envs.select{|env| env[:key] }.map{|env| { key: env[:key], value: env[:value] } } + ) end end desc 'runner SPREADSHEET_URL_OR_ALIAS COMMANDS', '' def runner(url_or_alias, *commands)