Skip to content
/ run Public

A Crystal library for running commands in reusable contexts.

License

Notifications You must be signed in to change notification settings

mosop/run

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Crystal Run

A Crystal library for running commands in reusable contexts.

Build Status

Installation

Add this to your application's shard.yml:

dependencies:
  run:
    github: mosop/run

Code Samples

Smiley

cmd = Run::Command.new("echo", [":)"])
cmd.run.wait # prints ":)"

Additional Arguments

cmd = Run.command("echo", [":)"])
%w(hello goodbye).each do |i|
  cmd.run(args: [i]).wait
end

This prints:

:) hello
:) goodbye

Over and Over

cmd = Run.command("echo", [":)"])
100.times do
  cmd.run.wait
end

This prints 100 of :).

Multiple at Once

cg = Run.group
100.times do
  cg.command "echo", [":)"]
end
cg.run.wait

This prints 100 of :) too.

Nested Contexts

cg = Run.group(chdir: "path")
cg.command "pwd"
cg.command "pwd", chdir: "to"
cg.command "pwd", chdir: ".."
cg.run.wait

If the current directory is /Users/mosop, this code prints:

/Users/mosop/path
/Users/mosop/path/to
/Users/mosop

Parallel

cg = Run.group
cg.command "wget", %w(http://mosop.rocks)
cg.command "wget", %w(http://mosop.yoga)
cg.command "wget", %w(http://mosop.ninja)
process_group = cg.run(parallel: true)

# do other things

process_group.wait

Running Code Blocks

  • In a fiber
cg = Run.group
100.times do
  cg.future do
    puts ":)"
    0
  end
end
cg.run.wait
  • In a forked process
cg = Run.group
100.times do
  cg.fork do
    puts ":)"
    0
  end
end
cg.run.wait

Returning Data by Forked Processes

cg = Run.group
100.times do
  cg.fork do
    Run::ExitStatus.new(0, ":)").exit!
    0
  end
end

cg.run.wait do |process|
  if process = process.as?(Run::AsProcess)
    puts process.exit_status.data
  end
end

This prints 100 of :).

Usage

require "run"

And see:

Versioning Policy

See Wiki.

Release Notes

See Releases.

About

A Crystal library for running commands in reusable contexts.

Resources

License

Stars

Watchers

Forks

Packages

No packages published