Reoganize code + implement basic report

This commit is contained in:
Glenn Y. Rolland 2021-04-04 23:37:13 +02:00
parent bab60ec856
commit 15837d36b2
27 changed files with 163 additions and 118 deletions

View file

@ -1,27 +1,30 @@
require "./models"
require "./actions"
require "./models"
require "option_parser"
require "yaml"
require "colorize"
require "xdg_basedir"
require "completion"
require "../lib/version"
require "../lib/types"
require "../lib/models"
require "../lib/actions"
require "../lib/models"
module Hodler
class Cli
alias Options = {
action: Action::Type,
verbose_enable: Bool,
config_file: String
}
class CtlCli
property config : ConfigModel?
property options : Options?
property options : GlobalOptions?
def initialize
@config = nil
@options = nil
end
def self.parse_options(args) : Options
def self.parse_options(args) : GlobalOptions
# default values
action = Action::Type::Report
action = ReportAction
config_file = XDGBasedir.full_path("hodler/wallet.yml", :config, :read).as(String)
verbose_enable = true
@ -58,20 +61,32 @@ module Hodler
parser.separator
parser.separator "Commands"
parser.on("report", "Compute report") do
parser.banner = "Usage: #{Version::PROGRAM} list [arguments]"
action = Action::Type::Report
parser.on("get", "Get given object") do
parser.banner = "Usage: #{Version::PROGRAM} get [arguments]"
parser.separator
parser.separator "Commands"
parser.on("portfolio", "Show current portfolio") do
parser.banner = "Usage: #{Version::PROGRAM} portfolio [arguments]"
action = GetPortfolioAction
end
parser.on("tui", "Run ncurses interactive UI") do
parser.banner = "Usage: #{Version::PROGRAM} tui [arguments]"
action = Action::Type::TextUi
parser.on("wallet", "Show given wallet") do
parser.banner = "Usage: #{Version::PROGRAM} portfolio [arguments]"
action = GetWalletAction
end
end
parser.on("web", "Run web interactive UI") do
parser.banner = "Usage: #{Version::PROGRAM} web [arguments]"
action = Action::Type::WebUi
end
# parser.on("tui", "Run ncurses interactive UI") do
# parser.banner = "Usage: #{Version::PROGRAM} tui [arguments]"
# action = TextUiAction
# end
# parser.on("web", "Run web interactive UI") do
# parser.banner = "Usage: #{Version::PROGRAM} web [arguments]"
# action = WebUiAction
# end
parser.separator
@ -102,12 +117,12 @@ module Hodler
verbose_enable: verbose_enable,
config_file: config_file,
action: action
}.as(Options)
}.as(GlobalOptions)
end
def self.parse_config(options)
puts "Loading configuration...".colorize(:yellow) if options[:verbose_enable]
config_file = options[:config_file]
puts "Loading configuration... #{config_file}".colorize(:yellow) if options[:verbose_enable]
if ! File.exists? config_file
STDERR.puts "ERROR: Unable to read configuration file '#{config_file}'".colorize(:red)
@ -126,14 +141,12 @@ module Hodler
end
def self.run(args)
app = Cli.new
options = Cli.parse_options(args)
config = Cli.parse_config(options)
app = CtlCli.new
options = CtlCli.parse_options(args)
config = CtlCli.parse_config(options)
app.options = options
app.config = config
portfolio = PortfolioFactory.build(options, config)
action = ActionFactory.build(options, config)
action.perform
@ -142,3 +155,6 @@ module Hodler
end
end
end
Hodler::CtlCli.run(ARGV)

0
src/hodlertui/main.cr Normal file
View file

0
src/hodlerweb/main.cr Normal file
View file

View file

@ -1,4 +1,6 @@
require "tablo"
module Hodler
class Action
enum Type
@ -11,7 +13,8 @@ module Hodler
return false
end
def initialize(config : ConfigModel)
def initialize(global_options : GlobalOptions, config : ConfigModel)
@global_options = global_options
@config = config
end
@ -29,26 +32,16 @@ module Hodler
wallets[from_id] -= tr.from.amount
wallets[fee_id] -= tr.fee.amount
wallets[to_id] += tr.to.amount
pp tr
pp wallets
end
end
end
class ActionFactory
def self.build(options, config)
action_class = Action
[ReportAction, WebUiAction, TextUiAction].each do |cls|
action_class = cls if cls.match(options[:action])
end
action = action_class.new(config)
action = options[:action].new(options, config)
end
end
end
require "./actions/report"
require "./actions/tui"
require "./actions/web"
require "./actions/*"

View file

@ -0,0 +1,16 @@
require "../actions"
module Hodler
class GetPortfolioAction < Action
def self.match(action)
# return (action == Action::Type::Report)
end
# Display Wallet, Symbol, Held, Initial Value, Current Value, Evolution (%)
def perform
portfolio = PortfolioFactory.build(@global_options, @config)
puts portfolio.to_report
end
end
end

View file

View file

@ -0,0 +1,15 @@
require "../actions"
module Hodler
class GetPortfolioAction < Action
def self.match(action)
# return (action == Action::Type::Report)
end
def perform
portfolio = PortfolioFactory.build(@global_options, @config)
puts portfolio.to_report
end
end
end

22
src/lib/models.cr Normal file
View file

@ -0,0 +1,22 @@
require "yaml"
module Hodler
class Model
include YAML::Serializable
end
end
require "./types"
require "./models/*"
# require "./models/deposit"
# require "./models/fee_trade"
# require "./models/portfolio"
# require "./models/ref_wallet"
# require "./models/trade"
# require "./models/transaction_trade"
# require "./models/ui_config"
# require "./models/wallet"
# require "./models/wallet_value_trade"
#

View file

@ -0,0 +1,46 @@
require "../models"
module Hodler
class PortfolioModel < Model
property wallets : Array(WalletModel)
def initialize()
@wallets = [] of WalletModel
end
def import_wallets(wallets : Array(WalletModel))
wallets.each do |wallet|
@wallets << wallet
end
end
def add_wallet
end
def remove_wallet
end
def to_report
data = @wallets.map{|row| [row.name, row.type.to_s, row.refs.map{|ref| ref.sym}.join(", ")] }
# pp data
table = Tablo::Table.new(data, connectors: Tablo::CONNECTORS_SINGLE_ROUNDED) do |t|
t.add_column("Name") { |row| row[0] }
t.add_column("Type") { |row| row[1] }
t.add_column("Symbols", width: 40) { |row| row[2] }
end
end
end
class PortfolioFactory
def self.build(options : GlobalOptions, config : ConfigModel) : PortfolioModel
portfolio = PortfolioModel.new
portfolio.import_wallets(config.wallets)
return portfolio
end
end
end

12
src/lib/types.cr Normal file
View file

@ -0,0 +1,12 @@
require "./actions/*"
module Hodler
alias AmountT = Float64 | Int64
alias GlobalOptions = {
action: Action.class,
verbose_enable: Bool,
config_file: String
}
end

View file

@ -1,14 +0,0 @@
require "option_parser"
require "yaml"
require "colorize"
require "xdg_basedir"
require "completion"
require "./version"
require "./types"
require "./models"
require "./cli"
Hodler::Cli.run(ARGV)

View file

@ -1,21 +0,0 @@
require "yaml"
module Hodler
class Model
include YAML::Serializable
end
end
require "./types"
require "./models/config"
require "./models/deposit"
require "./models/fee_trade"
require "./models/portfolio"
require "./models/ref_wallet"
require "./models/trade"
require "./models/transaction_trade"
require "./models/ui_config"
require "./models/wallet"
require "./models/wallet_value_trade"

View file

@ -1,36 +0,0 @@
require "../models"
module Hodler
class PortfolioModel < Model
property wallets : Array(WalletModel)
def initialize
@wallets = [] of WalletModel
end
def import_wallets(wallets : Array(WalletModel))
wallets.each do |wallet_config|
wallet = WalletModel.new(wallet_config.name, wallet_config.type)
# wallet.import(wallet_config)
pp wallet_config
end
end
def add_wallet
end
def remove_wallet
end
end
class PortfolioFactory
def self.build(options : Cli::Options, config : ConfigModel)
portfolio = PortfolioModel.new
portfolio.import_wallets(config.wallets)
end
end
end

View file

@ -1,4 +0,0 @@
module Hodler
alias AmountT = Float32 | Int32
end