Class: OsCtl::Image::Operations::Nix::RunInShell

Inherits:
Base
  • Object
show all
Includes:
Lib::Utils::Log, Lib::Utils::System
Defined in:
lib/osctl/image/operations/nix/run_in_shell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

run

Constructor Details

#initialize(expression, command, opts = {}) ⇒ RunInShell

Returns a new instance of RunInShell.

Parameters:

  • expression (String)

    nix file

  • command (Array<String>)
  • opts (Hash) (defaults to: {})

    see Lib::Utils::System#syscmd

Options Hash (opts):

  • :name (String)

    temporary executable name

  • :expression (String)

    nix file



20
21
22
23
24
25
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 20

def initialize(expression, command, opts = {})
  @expression = expression
  @command = command
  @name = opts.delete(:name) || 'nix-shell-run'
  @opts = opts
end

Instance Attribute Details

#commandArray<String> (readonly)

Returns:

  • (Array<String>)


13
14
15
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 13

def command
  @command
end

#expressionString (readonly, protected)

Returns:

  • (String)


10
11
12
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 10

def expression
  @expression
end

#nameObject (readonly, protected)

Returns the value of attribute name.



37
38
39
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 37

def name
  @name
end

#optsObject (readonly, protected)

Returns the value of attribute opts.



37
38
39
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 37

def opts
  @opts
end

Instance Method Details

#create_executableObject (protected)



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 39

def create_executable
  tmp = Tempfile.new(name, '/tmp')
  tmp.write(<<EOF
#!/bin/sh
exec #{command.join(' ')}
EOF
)
  tmp.close
  File.chmod(0700, tmp.path)
  tmp
end

#executeOsCtl::Lib::SystemCommandResult

Returns:

  • (OsCtl::Lib::SystemCommandResult)

Raises:

  • (OsCtl::Lib::SystemCommandError)


29
30
31
32
33
34
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 29

def execute
  exe = create_executable
  syscmd("nix-shell --run #{exe.path} #{expression}", opts)
ensure
  exe.unlink
end