Skip to content

Commit

Permalink
Add thor and serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
zhulik committed Jan 2, 2023
1 parent 05e2793 commit 1b3b202
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 23 deletions.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
dotcrypt (0.1.0)
dhall (~> 0.5.0)
thor (~> 1.2.0)
zeitwerk (~> 2.6.0)

GEM
Expand Down
13 changes: 0 additions & 13 deletions bin/dhall

This file was deleted.

1 change: 1 addition & 0 deletions dotcrypt.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Gem::Specification.new do |spec|

# Uncomment to register a new dependency of your gem
spec.add_dependency "dhall", "~> 0.5.0"
spec.add_dependency "thor", "~> 1.2.0"
spec.add_dependency "zeitwerk", "~> 2.6.0"

# For more information and examples about making a new gem, check out our
Expand Down
10 changes: 1 addition & 9 deletions example.dhall
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
let some_variable = "some value"
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_records = [
-- {
-- key = "value"
-- },
-- {
-- key = "value"
-- }
-- ],
some_nil = None
}
}
2 changes: 2 additions & 0 deletions exe/dotcrypt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
# frozen_string_literal: true

require "dotcrypt"

Dotcrypt::Cli::App.start(ARGV)
2 changes: 1 addition & 1 deletion lib/dotcrypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require "json"

require "zeitwerk"

require "dhall"
require "thor"

loader = Zeitwerk::Loader.for_gem

Expand Down
5 changes: 5 additions & 0 deletions lib/dotcrypt/CLI/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class Dotcrypt::Cli::App < Thor
include Dotcrypt::Cli::Commands::Load
end
15 changes: 15 additions & 0 deletions lib/dotcrypt/CLI/commands/load.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Dotcrypt::Cli::Commands::Load
def self.included(thor)
thor.class_eval do
desc "load DHALL", "loads dhall file"
# option :name, type: :string, default: ENV.fetch("NAME", nil)
def load(file = "example.dhall")
config = Dotcrypt::Dhall.load(File.read(file))

print(Dotcrypt::Serializers::Env.call(config))
end
end
end
end
15 changes: 15 additions & 0 deletions lib/dotcrypt/serializers/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require "shellwords"

class Dotcrypt::Serializers::Env < Dotcrypt::Serializers::Serializer
def call
Dotcrypt::Flattener.call(@config).reduce("") do |result, (k, v)|
result + (v.nil? ? "" : "export #{k}='#{escape(v)}'\n")
end
end

private

def escape(val) = val.gsub("'", %q('"'"'))
end
11 changes: 11 additions & 0 deletions lib/dotcrypt/serializers/serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class Dotcrypt::Serializers::Serializer
def self.call(...) = new(...).call

def initialize(config)
@config = config
end

def call = raise NotImplementedError
end

0 comments on commit 1b3b202

Please sign in to comment.