Skip to content

Commit

Permalink
Extract strap.sh fetching to a class
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfierke committed Jan 29, 2024
1 parent 7d4cf11 commit 92e09b9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
31 changes: 1 addition & 30 deletions src/mstrap/steps/init_step.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,11 @@ module MStrap
logn "==> Initializing mstrap"
create_rc_dir
CACertInstaller.install!
fetch_strap_sh
StrapShInstaller.new(force: force?).install!
create_brewfile_unless_exists
config.save!
end

private def update_strap_sh?
if File.exists?(Paths::STRAP_SH_PATH)
strap_sh_age.days >= 30 || force?
else
true
end
end

private def strap_sh_age
if file_info = File.info?(Paths::STRAP_SH_PATH)
Time.local - file_info.modification_time
else
Time::Span.zero
end
end

private def force?
!!options.force?
end
Expand All @@ -52,19 +36,6 @@ module MStrap
Dir.mkdir_p(Paths::PROJECT_SOCKETS)
end

private def fetch_strap_sh
if update_strap_sh?
log "--> Fetching latest strap.sh (older than 30 days or missing): "
Dir.mkdir_p(File.join(Paths::RC_DIR, "vendor"))

HTTP::Client.get(Paths::STRAP_SH_URL, tls: MStrap.tls_client) do |response|
File.write(Paths::STRAP_SH_PATH, response.body_io.gets_to_end)
end

success "OK"
end
end

private def create_brewfile_unless_exists
if !File.exists?(Paths::BREWFILE) || force?
logw "No Brewfile found or update requested with --force"
Expand Down
42 changes: 42 additions & 0 deletions src/mstrap/supports/strap_sh_installer.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module MStrap
# Manages the fetching of strap.sh (or strap-linux.sh)
class StrapShInstaller
include DSL

getter? :force

def initialize(@force = false)
end

def install!
fetch_strap_sh if update_strap_sh?
end

private def fetch_strap_sh
log "--> Fetching latest strap.sh (older than 30 days, missing, or requested): "
Dir.mkdir_p(File.join(Paths::RC_DIR, "vendor"))

HTTP::Client.get(Paths::STRAP_SH_URL, tls: MStrap.tls_client) do |response|
File.write(Paths::STRAP_SH_PATH, response.body_io.gets_to_end)
end

success "OK"
end

private def update_strap_sh?
if File.exists?(Paths::STRAP_SH_PATH)
strap_sh_age.days >= 30 || force?
else
true
end
end

private def strap_sh_age
if file_info = File.info?(Paths::STRAP_SH_PATH)
Time.local - file_info.modification_time
else
Time::Span.zero
end
end
end
end

0 comments on commit 92e09b9

Please sign in to comment.