#!/usr/bin/env ruby require 'auto_ali_cdn' def help puts <<-EOB 2cdn v#{AutoAliCDN::VERSION} Options: -h, --help Show help -V, --version Show program version Basic usage: 2cdn help # Show help 2cdn setup # Init oss file paths 2cdn upload # Upload resources to Aliyun Oss `2cdn upload $local_file $remote_file $is_delete` 2cdn download # Download resources path to local path EOB end def app_path @app_path ||= `pwd`.gsub("\n", '') end def config_file app_path+'/config/2cdn.yml' end if ARGV.empty? || ARGV[0] =~ /^(-h)|(--help)|(help)$/ help exit end if ARGV[0] =~ /^(-v)|(--version)$/ puts AutoAliCDN::VERSION exit end if ARGV[0] =~ /^setup$/ (system "mkdir #{app_path}/config") unless File.directory?("#{app_path}/config") system "touch #{config_file}" if File.read(config_file).length<=0 File.open(config_file, 'w') do |f| f.puts <<-EOB access_key_id: YOUR_ACCESS_ID access_key_secret: YOUR_ACCESS_KEY endpoint: YOUR_OSS_SERVICE_ADDRESS domain_name: http://cdn.your.server.name bucket: YOUR_BUCKET_NAME site_resource_path: /sites/your.server.name EOB end puts "config/2cdn.yml init ok.\nPlease fillup the keys.\n#{`cat #{config_file}`}" end AutoAliCDN::Oss.oss_setup(config_file) end if ARGV[0] =~ /^upload$/ unless File.exist?(config_file) puts "#{config_file} does not exist\nPlease run `2cdn init` first." exit end AutoAliCDN::Oss.oss_upload(config_file, app_path, ARGV[1], ARGV[2], ARGV[3]) end if ARGV[0] =~ /^download$/ AutoAliCDN::Oss.oss_download(config_file, app_path, ARGV[1] , ARGV[2]) end