Skip to content

Commit

Permalink
Merge pull request #6 from jay7x/wip
Browse files Browse the repository at this point in the history
Use YAML to store user-specified config
  • Loading branch information
jay7x authored Feb 16, 2023
2 parents 174fd96 + c4046f0 commit 088527a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
14 changes: 11 additions & 3 deletions spec/tasks/lima_start_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@
end

context 'with config specified' do
config = { 'a': 'b' }
tmpfile = Tempfile.new(["lima_#{vm_name}", '.json'])
let(:opts) { super().merge(config: { 'a': 'b' }) }

tmpfile = Tempfile.new(["lima_#{vm_name}", '.yaml'])
before :each do
allow(Tempfile).to receive(:new).and_return(tmpfile)
end
Expand All @@ -140,7 +141,14 @@
tmpfile.unlink
end

it_behaves_like 'limactl start', { name: vm_name, config: config }, tmpfile.path
it 'invokes `limactl start` with correct parameters' do
allow(Open3).to receive(:capture3).with('limactl', 'validate', tmpfile.path).and_return(['stdout', 'stderr', 0])
allow(Open3).to receive(:capture3).with('limactl', 'start', "--name=#{opts[:name]}", "--timeout=#{opts[:timeout]}", tmpfile.path).and_return(lima_response)

task.set_opts(opts)
result = task.create
expect(result).to eq(success_result)
end
end
end

Expand Down
32 changes: 28 additions & 4 deletions tasks/start.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'json'
require 'yaml'
require 'open3'
require 'tempfile'
require 'shellwords'
Expand Down Expand Up @@ -40,11 +40,18 @@ def create
elsif @url
cfg_url = @url
elsif @config
# Write @config to a temporary JSON file and pass it to limactl later
# Write @config to a temporary YAML file and pass it to limactl later
safe_name = Shellwords.escape(@name)
tmpfile = Tempfile.new(["lima_#{safe_name}", '.json'])
tmpfile.write(JSON.dump(@config))
tmpfile = Tempfile.new(["lima_#{safe_name}", '.yaml'])
# @config has symbolized keys by default. So .to_yaml will write keys as :symbols.
# Keys should be stringified to avoid this so Lima can parse the YAML properly.
tmpfile.write(stringify_keys_recursively(@config).to_yaml)
tmpfile.close

# Validate the config
_, stderr_str, status = Open3.capture3(@limactl, 'validate', tmpfile.path)
raise TaskHelper::Error.new(stderr_str, 'lima/validate-error', @config.to_yaml) unless status == 0

cfg_url = tmpfile.path
else
raise TaskHelper::Error.new('One of url/template/config parameters must be specified', 'lima/create-error')
Expand Down Expand Up @@ -81,6 +88,23 @@ def set_opts(opts = {})
@template = opts[:template]
@config = opts[:config]
end

private

# Stringify Hash keys recursively
def stringify_keys_recursively(hash)
stringified_hash = {}
hash.each do |k, v|
stringified_hash[k.to_s] = if v.is_a?(Hash)
stringify_keys_recursively(v)
elsif v.is_a?(Array)
v.map { |x| x.is_a?(Hash) ? stringify_keys_recursively(x) : x }
else
v
end
end
stringified_hash
end
end

LimaStart.run if $PROGRAM_NAME == __FILE__

0 comments on commit 088527a

Please sign in to comment.