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

  • :env (Hash)

    environment variables



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

def initialize(expression, command, opts = {})
  super()
  @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)

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.



40
41
42
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 40

def name
  @name
end

#optsObject (readonly, protected)

Returns the value of attribute opts.



40
41
42
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 40

def opts
  @opts
end

Instance Method Details

#create_executableObject (protected)



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 42

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

#executeOsCtl::Lib::SystemCommandResult

Returns:

  • (OsCtl::Lib::SystemCommandResult)

Raises:

  • (OsCtl::Lib::SystemCommandError)


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

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