Skip to content

Commit

Permalink
new Consistfile init
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdfm committed Dec 3, 2023
1 parent 0c5a2be commit e8b7932
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/spec/reports/
/tmp/
/Gemfile.lock
.DS_Store
21 changes: 21 additions & 0 deletions lib/consist/cli.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require "fileutils"

require "thor"
require "sshkit"
require "sshkit/dsl"

require "consist/utils"
require "consist/resolver"
require "consist/recipe"
require "consist/recipes"
Expand All @@ -17,10 +20,15 @@
module Consist
class CLI < Thor
extend ThorExt::Start
include Thor::Actions
include SSHKit::DSL

map %w[-v --version] => "version"

def self.source_root
File.dirname(__FILE__)
end

desc "version", "Display consist version"
def version
say "consist/#{VERSION} #{RUBY_DESCRIPTION}"
Expand Down Expand Up @@ -51,5 +59,18 @@ def up(server_ip)
consist_dir = options[:consistdir]
Consist::Consistfile.new(server_ip, consist_dir:, consistfile:, specified_step:)
end

desc "init", "Initialize a project with Consist, optionally specifying a GH path to a Consistfile"
def init(gh_path = nil)
if gh_path
full_url = "https://github.com/#{gh_path}"
Consist::Utils.clone_repo_contents(full_url, Dir.pwd)
else
puts "Creating new Consistfile..."
directory "templates/.consist", File.join(Dir.pwd, ".consist")
template "templates/Consistfile.tt", File.join(Dir.pwd, "Consistfile")
puts "...done"
end
end
end
end
19 changes: 19 additions & 0 deletions lib/consist/templates/Consistfile.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
consist do
config :example, "example"

# Define a recipe block:
#
# recipe :my_recipe do
# name "My recipe"
#
# steps do
# step do
# shell do
# <<~EOS
# echo 'hello'
# EOS
# end
# end
# end
# end
end
27 changes: 27 additions & 0 deletions lib/consist/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "fileutils"
require "tmpdir"

module Consist
module Utils
extend self

def clone_repo_contents(source_repo, target_dir)
temp_dir = Dir.mktmpdir

puts "Using #{source_repo} as template to initialize Consistfile..."
system("git clone --depth=1 #{source_repo} #{temp_dir} >/dev/null 2>&1")

Dir.foreach(temp_dir) do |item|
next if [".", "..", ".git"].include?(item)

source_path = File.join(temp_dir, item)
target_path = File.join(target_dir, item)
FileUtils.cp_r(source_path, target_path)
end

FileUtils.remove_entry_secure(temp_dir)

puts "...done"
end
end
end

0 comments on commit e8b7932

Please # to comment.