Skip to content

Commit

Permalink
- Add require_context directive
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyBen committed Jan 26, 2023
1 parent 16bab22 commit 348a53d
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/require-context/commands.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--help
ping
server --help
server
server deploy
7 changes: 7 additions & 0 deletions examples/require-context/runfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title 'DevOps Toolbox'

import 'server', image: 'my-rails-app', allow_deploy: true

action 'ping' do
say 'PONG'
end
25 changes: 25 additions & 0 deletions examples/require-context/server.runfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
title 'Server commands'
summary 'Build and manage all the servers'

# Declare that we need three variables in the `import` directive
# One mandatory (:image) and two with a default fallback values
require_context :image
require_context :allow_deploy, default: false
require_context :env, default: 'development'

# $ run server : default command
action do |args|
say 'g`Context`:'
p context
say 'g`Args`:'
p args
end

help 'Deploy to production'
action 'deploy' do
if context[:allow_deploy]
say 'DEPLOYING'
else
say! "r`Deployment not allowed`"
end
end
10 changes: 10 additions & 0 deletions lib/runfile/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@ class Action
attr_accessor :block, :help, :host

def run(args = {})
validate_context

instance_eval do
host.helpers.each { |b| b.call args }
block.call args
end
end

def validate_context
host.required_contexts.each do |varname, default|
next if host.context[varname]
raise UserError, "Need #{varname}" if default.nil?
host.context[varname] = default
end
end

def inspectable
{ name: name, prefix: prefix, shortcut: shortcut, host: host }
end
Expand Down
8 changes: 8 additions & 0 deletions lib/runfile/concerns/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ def param(name, help)
params[name] = help
end

def require_context(varname, default: nil)
required_contexts[varname] = default
end

def required_contexts
@required_contexts ||= {}
end

def shortcut(name)
current_action.shortcut = name
end
Expand Down
1 change: 1 addition & 0 deletions spec/approvals/initiator/examples
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
multiple-runfiles
named-only
naval-fate
require-context
shortcut

Run run example EXAMPLE with any of these examples to copy the files
Expand Down
4 changes: 4 additions & 0 deletions spec/approvals/integration/require-context/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Usage:
run ping
run server
run [COMMAND] (--help | -h)
14 changes: 14 additions & 0 deletions spec/approvals/integration/require-context/run --help
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
DevOps Toolbox

Usage:
run ping
run server
run [COMMAND] (--help | -h)

Commands:
nb`server`
Build and manage all the servers

Options:
--help, -h
Show this message
1 change: 1 addition & 0 deletions spec/approvals/integration/require-context/run ping
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PONG
4 changes: 4 additions & 0 deletions spec/approvals/integration/require-context/run server
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Context:
{:image=>"my-rails-app", :allow_deploy=>true, :env=>"development"}
Args:
{"server"=>true, "deploy"=>false, "--help"=>false}
16 changes: 16 additions & 0 deletions spec/approvals/integration/require-context/run server --help
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Server commands

Build and manage all the servers

Usage:
run server
run server deploy
run server (--help | -h)

Commands:
nb`deploy`
Deploy to production

Options:
--help, -h
Show this message
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEPLOYING
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
unless ENV['NOCOV']
SimpleCov.start do
enable_coverage :branch
# primary_coverage :branch
primary_coverage :branch
end
end

Expand Down

0 comments on commit 348a53d

Please sign in to comment.