Skip to content

Commit

Permalink
Add exec command, minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zhulik committed Jan 2, 2023
1 parent 7bef776 commit 8d5c821
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 2 deletions.
11 changes: 11 additions & 0 deletions .env.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let some_variable = "some value'"
in {
some_string = some_variable,
some_record = {
some_string = some_variable,
some_naturals = [1,2,3],
some_doubles = [1.2, 2.3],
some_strings = ["value1", "value2"],
some_nil = None
}
}
1 change: 1 addition & 0 deletions lib/dotcrypt/CLI/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
class Dotcrypt::Cli::App < Thor
include Dotcrypt::Cli::Commands::Show
include Dotcrypt::Cli::Commands::Exec
include Dotcrypt::Cli::Commands::ENV
end
13 changes: 13 additions & 0 deletions lib/dotcrypt/CLI/commands/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Dotcrypt::Cli::Commands::ENV
def self.included(thor)
thor.class_eval do
include Dotcrypt::Cli::Commands::Globals

desc "env", "prints exports for ennv"

def env = Dotcrypt::Cli::Commands::Show::Handler.new(**options.merge(output: :env)).call
end
end
end
33 changes: 33 additions & 0 deletions lib/dotcrypt/CLI/commands/exec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module Dotcrypt::Cli::Commands::Exec
def self.included(thor)
thor.class_eval do
include Dotcrypt::Cli::Commands::Globals

desc "exec COMMAND", "exec command a command with dhall env"

def exec(*command) = Handler.new(command, **options).call
end
end

class Handler
def initialize(command, **options)
@command = command
@options = options
end

def call
exec(env, *@command)
end

private

def env
@env ||= Dotcrypt::Dhall.load_from(@options[:file]).then do |c|
# TODO: validate variable names
Dotcrypt::Flattener.call(c, separator: @options[:separator])
end
end
end
end
17 changes: 17 additions & 0 deletions lib/dotcrypt/CLI/commands/globals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Dotcrypt::Cli::Commands::Globals
def self.included(thor)
thor.class_eval do
option :separator, aliases: :s,
type: :string,
default: "_",
desc: "Separator for flattening"

option :file, aliases: :f,
type: :string,
default: ".env.dhall",
desc: "Dhall file to read."
end
end
end
4 changes: 2 additions & 2 deletions lib/dotcrypt/CLI/commands/show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ def self.included(thor) # rubocop:disable Metrics/MethodLength
default: false,
desc: "Whether to flatten the output. Always enabled for `env` format"

def show = Handler.new(options).call
def show = Handler.new(**options).call
end
end

class Handler
def initialize(options)
def initialize(**options)
@options = options
end

Expand Down

0 comments on commit 8d5c821

Please sign in to comment.