Search code examples
rubychocolatey

How to list installed Chocolatey packages using Ruby?


Aim

Compare installed chocolatey package with latest available version on Chocolatey Gallery, install (cinst packageName) latest if latest version > installed version.

Problem

Code from this post which works for java -version has been used to read the output from clist -lo.

command = "clist -lo"

require 'open3'
Open3.popen3(command) do | stdrin, stdout, stderr|
  p stderr.read
  p stdout.read
end

Nor stderr.read neither stdout.read returns the output while the command prompt does. The path variables are correct. Executing clist -lo using cmd on a default path returns the list of installed Chocolatey Packages.

Question

How to get the output from clist -lo using Ruby?


Solution

  • Not sure if this is what you are looking for, but you can use capture3 instead of popen3 to capture the output.

    command = "clist -lo"
    
    require 'open3'
    stdout, stderr, status = Open3.capture3(command)
    
    puts "stdout: #{stdout}"
    puts "stderr: #{stderr}"
    puts "status: #{status}"
    

    results in

    stdout: 7zip.install 9.22.01.20130618
    autohotkey_l 1.1.13.01
    autohotkey_l.install 1.1.13.01
    chocolatey 0.9.8.23
    ChocolateyGUI 0.11.1
    ConsoleZ 1.9.1.13351
    cyg-get 1.1.0
    cygwin 1.7.23.20130814
    expresso 3.0.4334.20120225
    git.install 1.8.3
    Listary 4.02.1360
    PowerShell 3.0.20121027
    putty 0.63.0
    ruby 2.0.0.24700
    TeraCopy 2.27
    Reading environment variables from registry. Please wait... Done.
    stderr:
    status: pid 47528 exit 0
    

    for me