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



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

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

Instance Attribute Details

#commandArray<String> (readonly)

Returns:

  • (Array<String>)


15
16
17
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 15

def command
  @command
end

#expressionString (readonly)

Returns:

  • (String)


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

def expression
  @expression
end

#nameObject (readonly, protected)

Returns the value of attribute name.



48
49
50
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 48

def name
  @name
end

#optsObject (readonly, protected)

Returns the value of attribute opts.



48
49
50
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 48

def opts
  @opts
end

Instance Method Details

#create_executableObject (protected)



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 50

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

#executeOsCtl::Lib::SystemCommandResult

Returns:

  • (OsCtl::Lib::SystemCommandResult)

Raises:

  • (OsCtl::Lib::SystemCommandError)


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/osctl/image/operations/nix/run_in_shell.rb', line 33

def execute
  exe = create_executable
  syscmd(
    'nix --extra-experimental-features ' \
    "#{Shellwords.escape('nix-command flakes')} " \
    "develop --file #{Shellwords.escape(expression)} " \
    "--command #{Shellwords.escape(exe.path)}",
    opts
  )
ensure
  exe&.unlink
end