Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
Utils -> Config::Utils | Setup extend self -> self.
Browse files Browse the repository at this point in the history
  • Loading branch information
636f7374 committed Sep 23, 2019
1 parent 2d03e9a commit 2151aa5
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 187 deletions.
1 change: 1 addition & 0 deletions src/shadow.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require "sqlite3"
require "file_utils"
require "openssl/hmac"
require "../lib/shield/src/shield/*"
require "./shadow/config/*"
require "./shadow/*"

module Shadow::CommandParser
Expand Down
51 changes: 28 additions & 23 deletions src/shadow/config.cr → src/shadow/config/config.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Shadow::Config
extend self

def default_path
ENV["HOME"] + "/.shadow.yml"
def self.default_path
String.build do |io|
io << ENV["HOME"] << "/.shadow.yml"
end
end

def init_config
def self.init_config
Utils.conflict? default_path, :conflict do |conflict?|
Render.delete default_path if conflict?
config = Parser::Config.new
Expand All @@ -17,7 +17,7 @@ module Shadow::Config
end
end

def parse_config(path = default_path)
def self.parse_config(path = default_path)
begin
Utils.read path do |text|
yield Parser::Config.from_yaml text
Expand All @@ -27,36 +27,41 @@ module Shadow::Config
end
end

def path_exist?
def self.default_path_exist?
Utils.exist? default_path, :config do |exist?|
yield exist?
end
end

def load_config
path_exist? do |exist?|
parse_config do |config|
return yield config
end if exist?
def self.load_config
default_path_exist? do |exist?|
if exist?
parse_config do |config|
return yield config
end
end

init_config do
path_exist? do |exist?|
parse_config do |config|
yield config
end if exist?
default_path_exist? do |exist?|
if exist?
parse_config do |config|
yield config
end
end
end
end
end
end

def destroy
def self.destroy
destroy_database do
Utils.delete default_path do
Render.destroy default_path
end
end
end

def move_database
def self.move_database
load_config do |config|
Utils.ask_move config.database, :database do |after|
Utils.move config.database, after do
Expand All @@ -70,7 +75,7 @@ module Shadow::Config
end
end

def rename_database
def self.rename_database
load_config do |config|
Utils.ask_rename config.database, :database do |after|
Utils.rename config.database, after do
Expand All @@ -84,7 +89,7 @@ module Shadow::Config
end
end

def bind_database
def self.bind_database
load_config do |config|
Utils.ask_bind :database do |after|
before = config.database
Expand All @@ -96,7 +101,7 @@ module Shadow::Config
end
end

def reset_database
def self.reset_database
load_config do |config|
Utils.conflict? config.database, :database do |conflict?|
Render.delete config.database if conflict?
Expand All @@ -109,7 +114,7 @@ module Shadow::Config
end
end

def destroy_database
def self.destroy_database(&block)
load_config do |config|
Utils.delete config.database do
Render.delete config.database
Expand All @@ -118,7 +123,7 @@ module Shadow::Config
end
end

def init_database
def self.init_database
load_config do |config|
Utils.conflict? config.database, :database do |conflict?|
Render.delete config.database if conflict?
Expand Down
145 changes: 145 additions & 0 deletions src/shadow/config/utils.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
module Shadow::Config
module Utils
def self.input : String
STDIN.gets.to_s.chomp.rstrip
end

def self.ask_rename(path, ask : Symbol, &block)
loop do
print Render.reply[:rename][ask]
input = self.input

break yield String.build do |io|
io << File.dirname path
io << "/"
io << File.basename input
end
end
end

def self.ask_move(path, ask : Symbol, &block)
loop do
print Render.reply[:move][ask]
input = self.input
input = input.gsub /\\ /, " "
input += "/" unless '/' == input[-1]

if File.directory? input
break yield String.build do |io|
io << input << File.basename path
end
end

puts Render.reply[:invalid][:path]
end
end

def self.ask_bind(ask : Symbol, &block)
loop do
print Render.reply[:bind][ask]
input = self.input

if File.file? input
break yield input
end

puts Render.reply[:invalid][:file]
end
end

def self.exist?(path, ask : Symbol, &block)
loop do
break yield true if File.file? path
print Render.reply[:exist][ask]
case self.input
when "Y", "y" then break yield false
when "N", "n" then abort nil
end
end
end

def self.conflict?(path : String, ask : Symbol, &block)
loop do
unless File.file? path
break yield false
end
print Render.reply[:conflict][ask]
case self.input
when "Y", "y"
_break = false
self.delete path do
_break = true
end

break yield true if _break
when "N", "n" then break
else
puts Render.reply[:invalid][:input]
end
end
end

def self.rename(before, after : String, &block)
begin
File.rename before, after
yield
rescue ex : Errno
raise RenameFailed.new ex.message
end
end

def self.move(before, after : String, &block)
begin
FileUtils.mv before, after
yield
rescue ex : Errno
raise MoveFailed.new ex.message
end
end

def self.create(path : String, &block)
begin
FileUtils.touch path
yield
rescue ex : Exception
raise CreateFailed.new ex.message
end
end

def self.read(path : String, &block)
begin
yield File.read path
rescue ex : Errno
raise ReadFailed.new ex.message
end
end

def self.delete(path : String, &block)
begin
File.delete path
yield
rescue ex : Errno
raise DeleteFailed.new ex.message
end
end

def self.mkdir_p(path : String, full_path? : Bool = true, &block)
begin
path = File.dirname path if full_path?
Dir.mkdir_p path
yield
rescue ex : Errno
raise CreateFailed.new ex.message
end
end

def self.write(path, text : String, &block)
begin
File.write path, text, mode: "wb"
yield
rescue ex : Errno
raise WriteFailed.new ex.message
end
end
end
end
10 changes: 5 additions & 5 deletions src/shadow/record.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,24 @@ class Shadow::Record

def set_column_with_value(record : Parser::Record)
Render.enter "Column"
record.set_column Utils.input
record.set_column Config::Utils.input
Render.enter "_Value"
record.set_value Utils.input
record.set_value Config::Utils.input
yield
end

def set_column_with_rowid(record : Parser::Record)
set_rowid record do
Render.enter "Column"
record.set_column Utils.input
record.set_column Config::Utils.input
yield
end
end

def set_rowid(record : Parser::Record)
loop do
Render.enter "_RowId"
i = Utils.input
i = Config::Utils.input
if i.to_i?
record.set_rowid i.to_i
break yield
Expand Down Expand Up @@ -173,7 +173,7 @@ class Shadow::Record
return Render.error message unless success?
data = "Nil" if data.empty?
print String.build { |io| io << option.record.column << ":[" << data << "]: " }
option.record.set_value Utils.input
option.record.set_value Config::Utils.input
database.update_column_by_rowid(option.table.name, option.record) do |success?, message|
return Render.error message unless success?
Render.update data, option.record.value
Expand Down
Loading

0 comments on commit 2151aa5

Please sign in to comment.