aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-07-15 22:42:12 -0400
committerPaul Duncan <pabs@pablotron.org>2016-07-15 22:42:12 -0400
commit8d3d8938be62852e479091b41920091e9df2ee72 (patch)
treed5285a89cde36cb571e00054b93a7db7aae09381 /src
parenta0095ac9a6b3f92b7ab49384683fb5cef53dae67 (diff)
downloadguff-8d3d8938be62852e479091b41920091e9df2ee72.tar.bz2
guff-8d3d8938be62852e479091b41920091e9df2ee72.zip
refactor cli
Diffstat (limited to 'src')
-rw-r--r--src/guff.cr137
-rw-r--r--src/guff/cli.cr131
2 files changed, 132 insertions, 136 deletions
diff --git a/src/guff.cr b/src/guff.cr
index 67258ec..cfd020c 100644
--- a/src/guff.cr
+++ b/src/guff.cr
@@ -12,140 +12,5 @@ end
require "./guff/**"
-module Guff
- module CLI
- module Actions
- abstract class Action
- def self.run(config : Config)
- new(config).run
- end
-
- def initialize(@config : Config)
- end
-
- abstract def run
- end
-
- class InitAction < Action
- class Data
- YAML.mapping({
- init_sql: Array(String),
- add_user: String,
- test_posts: Array(String),
- })
-
- def self.load(system_dir : String) : Data
- self.from_yaml(File.read(File.join(system_dir, "init.yaml")))
- end
- end
-
- def initialize(config : Config)
- super(config)
-
- # read init data
- @data = Data.load(@config.system_dir)
- end
-
- def run
- STDERR.puts "Initializing data directory"
- Dir.mkdir(@config.data_dir) unless Dir.exists?(@config.data_dir)
-
- Guff::Database.new(@config.db_path) do |db|
- @data.init_sql.each do |sql|
- db.query(sql)
- end
-
- # gen random password and add admin user
- # TODO: move these to init.yaml
- password = Password.random_password
- add_user(db, "Admin", "admin@admin", password)
- add_user(db, "Test", "test@test", "test")
- add_test_posts(db)
-
- STDERR.puts "admin user: admin@admin, password: #{password}"
- end
- end
-
- private def add_user(
- db : Database,
- name : String,
- email : String,
- password : String
- ) : Int64
- db.query(@data.add_user, [
- name,
- email,
- Password.create(password),
- "admin",
- ])
- db.last_insert_row_id.to_i64
- end
-
- private def add_test_posts(db)
- # STDERR.puts "DEBUG: adding test data"
- @data.test_posts.each do |sql|
- db.query(sql)
- end
- end
- end
-
- class RunAction < Action
- def run
- STDERR.puts "Running web server"
- check_dirs
-
- # create context
- context = Context.new(@config)
-
- STDERR.puts "listening on %s:%s" % [@config.host, @config.port]
-
- # run server
- HTTP::Server.new(
- @config.host,
- @config.port.to_i,
- Handlers.get(context)
- ).listen
- end
-
- private def check_dirs
- {
- system: @config.system_dir,
- data: @config.data_dir,
- }.each do |name, dir|
- unless Dir.exists?(dir)
- raise "missing #{name} directory: \"#{dir}\""
- end
- end
- end
- end
- end
-
- def self.run(app : String, args : Array(String))
- begin
- begin
- # parse command-line arguments
- config = Config.parse(app, args)
- rescue err
- raise "#{err}. Use --help for usage"
- end
-
- case config.mode
- when "init"
- Actions::InitAction.run(config)
- when "run"
- Actions::RunAction.run(config)
- when "help"
- # do nothing
- else
- # never reached
- raise "unknown mode: #{config.mode}"
- end
- rescue err
- STDERR.puts "ERROR: #{err}."
- exit -1
- end
- end
- end
-end
-
+# allow cli invocation
Guff::CLI.run($0, ARGV)
diff --git a/src/guff/cli.cr b/src/guff/cli.cr
index 0bb3d4b..c3fb4d6 100644
--- a/src/guff/cli.cr
+++ b/src/guff/cli.cr
@@ -1,2 +1,133 @@
module Guff::CLI
+ module Actions
+ abstract class Action
+ def self.run(config : Config)
+ new(config).run
+ end
+
+ def initialize(@config : Config)
+ end
+
+ abstract def run
+ end
+
+ class InitAction < Action
+ class Data
+ YAML.mapping({
+ init_sql: Array(String),
+ add_user: String,
+ test_posts: Array(String),
+ })
+
+ def self.load(system_dir : String) : Data
+ self.from_yaml(File.read(File.join(system_dir, "init.yaml")))
+ end
+ end
+
+ def initialize(config : Config)
+ super(config)
+
+ # read init data
+ @data = Data.load(@config.system_dir)
+ end
+
+ def run
+ STDERR.puts "Initializing data directory"
+ Dir.mkdir(@config.data_dir) unless Dir.exists?(@config.data_dir)
+
+ Guff::Database.new(@config.db_path) do |db|
+ @data.init_sql.each do |sql|
+ db.query(sql)
+ end
+
+ # gen random password and add admin user
+ # TODO: move these to init.yaml
+ password = Password.random_password
+ add_user(db, "Admin", "admin@admin", password)
+ add_user(db, "Test", "test@test", "test")
+ add_test_posts(db)
+
+ STDERR.puts "admin user: admin@admin, password: #{password}"
+ end
+ end
+
+ private def add_user(
+ db : Database,
+ name : String,
+ email : String,
+ password : String
+ ) : Int64
+ db.query(@data.add_user, [
+ name,
+ email,
+ Password.create(password),
+ "admin",
+ ])
+ db.last_insert_row_id.to_i64
+ end
+
+ private def add_test_posts(db)
+ # STDERR.puts "DEBUG: adding test data"
+ @data.test_posts.each do |sql|
+ db.query(sql)
+ end
+ end
+ end
+
+ class RunAction < Action
+ def run
+ STDERR.puts "Running web server"
+ check_dirs
+
+ # create context
+ context = Context.new(@config)
+
+ STDERR.puts "listening on %s:%s" % [@config.host, @config.port]
+
+ # run server
+ HTTP::Server.new(
+ @config.host,
+ @config.port.to_i,
+ Handlers.get(context)
+ ).listen
+ end
+
+ private def check_dirs
+ {
+ system: @config.system_dir,
+ data: @config.data_dir,
+ }.each do |name, dir|
+ unless Dir.exists?(dir)
+ raise "missing #{name} directory: \"#{dir}\""
+ end
+ end
+ end
+ end
+ end
+
+ def self.run(app : String, args : Array(String))
+ begin
+ begin
+ # parse command-line arguments
+ config = Config.parse(app, args)
+ rescue err
+ raise "#{err}. Use --help for usage"
+ end
+
+ case config.mode
+ when "init"
+ Actions::InitAction.run(config)
+ when "run"
+ Actions::RunAction.run(config)
+ when "help"
+ # do nothing
+ else
+ # never reached
+ raise "unknown mode: #{config.mode}"
+ end
+ rescue err
+ STDERR.puts "ERROR: #{err}."
+ exit -1
+ end
+ end
end