class Choreograph::CLI

Public Instance Methods

cache() click to toggle source
# File lib/choreograph.rb, line 44
def cache

   if options[:enable] && options[:disable]
      puts "Use only one, --enable(-e) or --disable(-d)"
      exit(0)
   end

   config = YAML.load_file( '/etc/com.forsloff.choreograph.conf' )

   #TODO: Validate the format of the caching server url
   if options[:enable]
      if config[:cache] && !options[:force]
         puts "Caching is currently configured. Using #{options[:cache]},"
         puts "to overwrite the current caching sever use --force(-f)"
         exit(0)
      end
      config[:cache] = options[:enable]
   elsif options[:disable]
      config[:cache] = false
   end

   `echo "#{config.to_yaml}" | sudo tee /etc/com.forsloff.choreograph.conf`

end
install(application) click to toggle source
# File lib/choreograph.rb, line 97
def install(application)

   # Check all of the avaible package sources for the requested package.

   #package_sources = ['https://raw.githubusercontent.com/forsloff/conductor-repository/master/packages/{{package}}.yaml']
   config = YAML::load_file('/etc/com.forsloff.choreograph.conf')
   config[:sources].each do |source|
       @manifest = HTTParty.get( source.gsub!('{{package}}', application) )
       next if @manifest.code == 404
   end
   if @manifest.code == 404
       puts 'The requested application could not be found.'
   else
       manifest = RecursiveOpenStruct.new( YAML.load(@manifest.body), :recurse_over_arrays => true )
   end

   # If there is a request for a spacific version return the corasponding url, else return the current version url
   resource = options[:version] == 'current' ? manifest.current : manifest.versions.send(options[:version])

   # Create a temp download directory in /tmp/
   destination = `mktemp -d /tmp/com.forsloff.choreograph.XXXXXXXXXXXX`.gsub("\n", "")

   # Try to download using aira2c, failing that use curl. If the download fails delete our temp directory and exit
   # TODO: add support for using muliplue download url in aira2c
   status = syscall('aria2c', resource.url, '-d', destination, '--file-allocation', 'none')
   if status.nil?
       syscall "cd #{destination} && { curl -O #{resource.url} -silent ; cd -; }"
   elsif status == false
       puts 'An error occured while downloading... Exiting'
       syscall 'rm', '-r', destination
       exit
   end

   if resource.dmg
       source      = File.join(destination, resource.dmg)
       mountpoint  = Digest::MD5.hexdigest(source).prepend('/Volumes/')
       syscall 'hdiutil', 'attach', source, '-mountpoint', mountpoint, '-nobrowse', '-quiet'
   elsif resource.zip
       source      = File.join(destination, resource.zip)
       mountpoint  = destination
       syscall 'ditto', '-xk', source, mountpoint
   end

   if resource.pkg
       package = File.join(mountpoint, resource.pkg)
       syscall 'sudo', 'installer', '-pkg', package, '-target', '/'
   end

   if resource.app
       application = File.join(mountpoint, resource.app)
       syscall 'cp', '-a', application, '/Applications/'
   end

   syscall('hdiutil', 'detach', mountpoint, '-quiet') if resource.dmg

   syscall 'rm', '-r', destination

end
setup() click to toggle source
# File lib/choreograph.rb, line 30
def setup
   if File.exists?('/etc/com.forsloff.choreograph.conf') && !options[:force]
      puts "Choreograph has already been setup. please run `choreograph setup -f`"
      exit
   end
   settings = { :cache => false, :sources => ['https://raw.githubusercontent.com/forsloff/conductor-repository/master/packages/{{package}}.yaml'] }
   `echo "#{settings.to_yaml}" | sudo tee /etc/com.forsloff.choreograph.conf`
end
source(url) click to toggle source
# File lib/choreograph.rb, line 72
def source(url)

   if options[:add] && options[:remove]
      puts "Use only one, --add(-a) or --remove(-r)"
      exit(0)
   end

   config = YAML.load_file( '/etc/com.forsloff.choreograph.conf' )

   if options[:add]
      if config[:sources].include?(url)
         puts "This source has already been added"
         exit(0)
      end
      config[:sources] << url
   elsif options[:remove]
      config[:sources].delete(url)
   end

   `echo "#{config.to_yaml}" | sudo tee /etc/com.forsloff.Choreograph.conf`

end

Private Instance Methods

syscall(*cmd) click to toggle source
# File lib/choreograph.rb, line 158
def syscall(*cmd)
   begin
       stdout, stderr, status = Open3.capture3(*cmd)
       status.success? && stdout.slice!(0..-(1 + $/.size)) # strip trailing eol
   rescue
   end
end