Skip to content

Commit

Permalink
Remove unused variables
Browse files Browse the repository at this point in the history
  • Loading branch information
j8r committed Sep 16, 2019
1 parent 79d0125 commit ff5b29c
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 44 deletions.
2 changes: 0 additions & 2 deletions spec/service_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ end
def assert_service(service, file = __FILE__, line = __LINE__)
Service.init = service

user = group = TEST_APP_PACKAGE_NAME
test_prefix = DPPM::Prefix.new File.tempname("_dppm_service_test")
test_prefix.create
test_prefix.ensure_app_dir
test_app = test_prefix.new_app(TEST_APP_PACKAGE_NAME)
FileUtils.cp_r Path[SAMPLES_DIR, TEST_APP_PACKAGE_NAME].to_s, test_app.path.to_s

service_config = test_app.service.config.class.new
test_app.service.file = test_app.service_file

it "creates a service", file, line do
Expand Down
2 changes: 1 addition & 1 deletion src/cli/app.cr
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ module DPPM::CLI::App
end
end
if follow
while true
loop do
yield channel.receive
end
else
Expand Down
4 changes: 2 additions & 2 deletions src/host.cr
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ struct DPPM::Host
end

# Returns an available port
def self.available_port(port : UInt16 = 0_u16) : UInt16
def self.available_port(start_port : UInt16 = 0_u16) : UInt16
ports_used = Set(UInt16).new
(port..UInt16::MAX).each do |port|
(start_port..UInt16::MAX).each do |port|
begin
tcp_port_available port
return port
Expand Down
2 changes: 1 addition & 1 deletion src/prefix.cr
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ struct DPPM::Prefix

# DPPM configuration.
property dppm_config : Config do
if config_file = dppm.config_file
if config_file = dppm.config_file?
Config.new File.read(config_file)
else
@@default_dppm_config
Expand Down
25 changes: 13 additions & 12 deletions src/prefix/app.cr
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct DPPM::Prefix::App

# Returns a database, if any.
getter database : Database::MySQL | Nil do
if config_vars = pkg_file.config_vars
if pkg_file.config_vars
if database_type = get_config?("database_type")
type = database_type.to_s
elsif databases = pkg_file.databases
Expand Down Expand Up @@ -450,6 +450,7 @@ struct DPPM::Prefix::App
self
end

# ameba:disable Metrics/CyclomaticComplexity
def add(
vars : Hash(String, String) = Hash(String, String).new,
shared : Bool = true,
Expand Down Expand Up @@ -480,11 +481,10 @@ struct DPPM::Prefix::App
database_app = @prefix.new_app database
Log.info "initialize database", database

(database_create database_app).tap do |database|
database.clean
database.check_user
vars.merge! database.vars
end
new_database = database_create database_app
new_database.clean
new_database.check_user
vars.merge! new_database.vars
end

# Default variables
Expand Down Expand Up @@ -639,7 +639,7 @@ struct DPPM::Prefix::App

# Build and add missing dependencies and copy library configurations
install_deps deps, shared do |dep_pkg|
if dep_config = dep_pkg.config
if dep_pkg.config?
Log.info "copying library configuration files", dep_pkg.name
dep_conf_path = conf_path / dep_pkg.package
Dir.mkdir_p dep_conf_path.to_s
Expand All @@ -658,10 +658,10 @@ struct DPPM::Prefix::App
write_configs
set_permissions

if (real_database = database?) && database_app && database_password
if (current_database = database?) && database_app && database_password
Log.info "configure database", database_app.name
real_database.ensure_root_password database_app
real_database.create database_password
current_database.ensure_root_password database_app
current_database.create database_password
end

# Running the add task
Expand Down Expand Up @@ -795,6 +795,7 @@ struct DPPM::Prefix::App
end
end

# ameba:disable Metrics/CyclomaticComplexity
def delete(confirmation : Bool = true, preserve_database : Bool = false, keep_user_group : Bool = false, &block) : App?
raise "Application doesn't exist: #{@path}" if !File.exists? @path.to_s

Expand All @@ -805,7 +806,7 @@ struct DPPM::Prefix::App
end

# Checks
if service = service?
if service?
if service.exists?
Log.info "a system service is found", @name
service.check_delete
Expand All @@ -821,7 +822,7 @@ struct DPPM::Prefix::App
Log.output << "\nbasedir: " << @path
Log.output << "\nuser: " << owner.user.name
Log.output << "\ngroup: " << owner.group.name
service?.try do |service|
if service?
Log.output << "\nservice: " << service.file
end
Log.output << '\n'
Expand Down
17 changes: 8 additions & 9 deletions src/prefix/base.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ module DPPM::Prefix::Base
@config_file_initialized = false

# Raises if the configuration file if it exists.
getter config_file : Path? do
getter? config_file : Path? do
if !@config_file_initialized
if Dir.exists? conf_path.to_s
Dir.each_child conf_path.to_s do |file|
file_path = conf_path / file
if file.starts_with? "config."
@config_file = file_path
@config_file = conf_path / file
end
end
end
Expand All @@ -35,23 +34,23 @@ module DPPM::Prefix::Base

# Raises if the configuration file doesn't exist.
def config_file! : Path
config_file || raise "Config file not available"
config_file? || raise "Config file not available"
end

@config_initialized = false

# Returns the main configuration.
getter config : ::Config::Types? do
if !@config_initialized && (config_path = config_file)
@config = ::Config.read? config_path
getter? config : ::Config::Types? do
if !@config_initialized && config_file?
@config = ::Config.read? config_file!
@config_initialized = true
end
@config
end

# Raises if no configuration is available.
def config! : ::Config::Types
config || raise "No valid config file: #{conf_path}config.*"
config? || raise "No valid config file: #{conf_path}config.*"
end

class ConfigKeyError < Exception
Expand All @@ -74,7 +73,7 @@ module DPPM::Prefix::Base
abstract def get_config(key : String, &block)

protected def config_from_pkg_file(key : String, &block)
if app_config = config
if app_config = config?
if config_vars = pkg_file.config_vars
if config_key = config_vars[key]?
yield app_config, config_key
Expand Down
6 changes: 4 additions & 2 deletions src/prefix/pkg.cr
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ class DPPM::Prefix::Pkg
Log.info "calculing package dependencies", @name
src.resolve_deps.each do |dep_name, versions|
dep_src = @prefix.new_src(dep_name)
version = if versions.includes?(latest = dep_src.pkg_file.version_from_tag "latest")
latest = dep_src.pkg_file.version_from_tag "latest"

version = if versions.includes? latest
latest
else
versions.first.to_s
Expand All @@ -137,7 +139,7 @@ class DPPM::Prefix::Pkg
pkg_file.ensure_version @version

vars = Host.vars.dup
arch_alias = if (aliases = pkg_file.aliases) && (version_alias = aliases[Host.arch]?)
arch_alias = if version_alias = pkg_file.aliases.try &.[Host.arch]?
version_alias
else
Host.arch
Expand Down
29 changes: 17 additions & 12 deletions src/prefix/program_data_task.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,7 @@ struct DPPM::Prefix::ProgramData::Task
if line = raw_line.as_s?
@line_number += 1
# # New variable assignation
if line.size > 4 &&
(line_var = line.partition(" = ")) &&
(first_line_var = line_var[0]) &&
Utils.ascii_alphanumeric_underscore? first_line_var
if (output = execute(line_var[2])).is_a? String
@vars[first_line_var] = output
else
raise "Expected String, got #{output}"
end
else
if !assign_variable? line
cmd = var_reader line
Log.info "execute", cmd
case output = execute cmd, last_cond
Expand All @@ -57,6 +48,18 @@ struct DPPM::Prefix::ProgramData::Task
raise Exception.new "Error at line #{@line_number}", ex
end

# Assigns a value to a variable from a line string if it corresponds to an assignment.
private def assign_variable?(line : String) : String?
return if line.size < 5
var, _, value = line.partition(" = ")
return if !Utils.ascii_alphanumeric_underscore? var
if (output = execute value).is_a? String
@vars[var] = output
else
raise "Expected String, got #{output}"
end
end

private def var_reader(cmd : String)
# Check if variables in the command are defined
building_command = false
Expand Down Expand Up @@ -125,8 +128,10 @@ struct DPPM::Prefix::ProgramData::Task
end

# Methods from
# https://crystal-lang.org/api/Dir.html
# https://crystal-lang.org/api/Dir.html and
# https://crystal-lang.org/api/File.html
#
# ameba:disable Metrics/CyclomaticComplexity
def execute(cmdline : String, last_cond : Bool = false) : String | Bool
# Check if it's a variable
if line = cmdline.lchop?('\'').try &.rchop?('\'')
Expand Down Expand Up @@ -234,7 +239,7 @@ struct DPPM::Prefix::ProgramData::Task
if success
output.to_s
else
raise "Execution returned an error: #{command} #{cmd.join ' '}\n#{output}"
raise "Execution returned an error: #{command} #{cmd.join ' '}\n#{output}\n#{error}"
end
else
raise "Unknown command or variable: #{cmd.join ' '}"
Expand Down
2 changes: 1 addition & 1 deletion src/service.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ module Service
Exec.new command, args, output: DPPM::Log.output, error: DPPM::Log.error do |process|
success = process.wait.success?
end
return success
success
end
end
4 changes: 3 additions & 1 deletion src/service/openrc_config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Service::Config
private OPENRC_ENV_VARS_PREFIX = "supervise_daemon_args=\"--env '"
private OPENRC_NETWORK_SERVICE = "net"

# ameba:disable Metrics/CyclomaticComplexity
def self.from_openrc(data : String)
service = new
line_number = 1
Expand Down Expand Up @@ -70,6 +71,7 @@ class Service::Config
service
end

# ameba:disable Metrics/CyclomaticComplexity
def to_openrc : String
String.build do |str|
str << OPENRC_SHEBANG << "\n\n"
Expand Down Expand Up @@ -110,7 +112,7 @@ class Service::Config
\t#{OPENRC_RELOAD_COMMAND}#{@reload_signal}
\teend $? "Failed to reload $RC_SVCNAME"
}
E
end
end
Expand Down
1 change: 1 addition & 0 deletions src/service/systemd_config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Service::Config
service
end

# ameba:disable Metrics/CyclomaticComplexity
def to_systemd : String
# Transform the hash to a systemd service
systemd = {"Unit" => Hash(String, String).new,
Expand Down
3 changes: 2 additions & 1 deletion src/web_site/caddy.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ struct WebSite::Caddy
include Site
@extra : IO::Memory = IO::Memory.new

# ameba:disable Metrics/CyclomaticComplexity
def initialize(@file : Path)
if File.exists? @file.to_s
line_number = 0
Expand Down Expand Up @@ -51,7 +52,7 @@ struct WebSite::Caddy
@hosts.try &.each do |host|
io << host.to_s.lchop("//") << ' '
end
io << "{"
io << '{'
io << "\n root " << @root if @root
if proxy = @proxy
io << "\n proxy / " << proxy.to_s.lchop("//")
Expand Down

0 comments on commit ff5b29c

Please sign in to comment.