class Staticdctl::CLI

Public Class Methods

new(options={}) click to toggle source
# File lib/staticdctl/cli.rb, line 12
def initialize(options={})
  @gli = GLIObject.new
  @gli.program_desc("Staticd CLI client")
  @gli.version(Staticdctl::VERSION)

  enable_debugging if options[:debugging]
  set_global_options
  build_commands
end

Public Instance Methods

run(*args) click to toggle source
# File lib/staticdctl/cli.rb, line 22
def run(*args)
  @gli.run(*args)
end

Private Instance Methods

build_command_attach_domain() click to toggle source
# File lib/staticdctl/cli.rb, line 223
def build_command_attach_domain
  @gli.desc("Attach a domain name to a site")
  @gli.arg_name("domain_name")
  @gli.command(:"domains:attach") do |c|
    c.action do |global_options,options,args|
      staticd_client(global_options) do |client|
        client.attach_domain(
          global_options[:site],
          name: args.first
        ) do |domain|
          puts "The #{domain.name} domain has been attached to the " +
            "#{domain.site_name} site."
        end
      end
    end
  end
end
build_command_config() click to toggle source
# File lib/staticdctl/cli.rb, line 104
def build_command_config
  @gli.desc("Display current configuration")
  @gli.command(:config) do |c|
    c.action do |global_options, options, args|
      config = load_config(global_options[:config], global_options[:host])
      puts "Current configuration for #{global_options[:host]}:"
      puts "No config." unless config.any?
      config.each do |key, value|
        puts " * #{key}: #{value}"
      end
    end
  end
end
build_command_create_release() click to toggle source
# File lib/staticdctl/cli.rb, line 275
def build_command_create_release
  @gli.desc("Push a new release for the current app")
  @gli.arg_name("[path]")
  @gli.command(:push) do |c|
    c.action do |global_options,options,args|
      source_path = args.any? ? args.first : "."

      print "Counting resources... "
      sitemap = StaticdUtils::Sitemap.create(source_path)
      if sitemap.routes.size < 1
        puts "stop. No resources."
        raise "No resources to send in '#{source_path}'"
      end
      puts "done (#{sitemap.routes.size} resources)."

      print "Asking host to identify new resources... "
      diff_sitemap = staticd_client(global_options) do |client|
        client.cached_resources(sitemap.to_h) do |new_map|
          StaticdUtils::Sitemap.new(new_map.to_h)
        end
      end
      puts "done (#{diff_sitemap.routes.size} new resources to upload)."

      print "Building the archive... "
      archive = StaticdUtils::Archive.create(
        source_path,
        diff_sitemap.routes
      )
      file_size = StaticdUtils::FileSize.new(archive.size)
      puts "done (#{file_size})."

      staticd_client(global_options) do |client|

        print "Uploading the archive... "
        timer_start = Time.now
        client.create_release(
          global_options[:site],
          archive.to_memory_file,
          sitemap.to_memory_file
        ) do |release|
          time_spent = Time.now - timer_start
          speed = archive.size / time_spent / 1000
          puts "done (#{'%.2f' % time_spent}s / #{'%.2f' % speed}kbps)."
          puts ""
          puts "The #{release.site_name} release (#{release.tag}) has " +
              "been created."
          if release.site.url
            puts release.site.url
          end
        end
      end
    end
  end
end
build_command_create_site() click to toggle source
# File lib/staticdctl/cli.rb, line 179
def build_command_create_site
  @gli.desc("Create a new site")
  @gli.command(:"sites:create") do |c|
    c.action do |global_options,options,args|
      staticd_client global_options do |client|
        client.create_site(name: global_options[:site]) do |site|
          puts "The #{site.name} site has been created."
          puts site.url if site.url
        end
      end
    end
  end
end
build_command_destroy_site() click to toggle source
# File lib/staticdctl/cli.rb, line 193
def build_command_destroy_site
  @gli.desc("Destroy a site")
  @gli.command(:"sites:destroy") do |c|
    c.action do |global_options,options,args|
      staticd_client(global_options) do |client|
        client.destroy_site(global_options[:site]) do
          puts "The #{global_options[:site]} site has been destroyed."
        end
      end
    end
  end
end
build_command_detach_domain() click to toggle source
# File lib/staticdctl/cli.rb, line 241
def build_command_detach_domain
  @gli.desc("Detach a domain name from a site")
  @gli.arg_name("domain_name")
  @gli.command(:"domains:detach") do |c|
    c.action do |global_options,options,args|
      staticd_client(global_options) do |client|
        client.detach_domain(global_options[:site], args.first) do |domain|
          puts "The #{args.first} domain has been detached from the " +
            "#{global_options[:site]} site."
        end
      end
    end
  end
end
build_command_domains() click to toggle source
# File lib/staticdctl/cli.rb, line 206
def build_command_domains
  @gli.desc("List all domain attached to the current site")
  @gli.command :domains do |c|
    c.action do |global_options,options,args|
      staticd_client(global_options) do |client|
        client.domains(global_options[:site]) do |domains|
          puts "Domain names attached to #{global_options[:site]}:"
          puts "No domain names attached." unless domains.any?
          domains.each do |domain|
            puts " * #{domain.name}"
          end
        end
      end
    end
  end
end
build_command_releases() click to toggle source
# File lib/staticdctl/cli.rb, line 256
def build_command_releases
  @gli.desc("List all releases of the current site")
  @gli.command(:releases) do |c|
    c.action do |global_options,options,args|
      staticd_client(global_options) do |client|
        client.releases(global_options[:site]) do |releases|
          releases_string =
            if releases.any?
              releases.map { |release| release.tag }.join(", ")
            else
              "no release deployed yet"
            end
          puts "Releases of #{global_options[:site]}: #{releases_string}."
        end
      end
    end
  end
end
build_command_rm_config() click to toggle source
# File lib/staticdctl/cli.rb, line 134
def build_command_rm_config
  @gli.desc("Remove a configuration option")
  @gli.arg_name("config_key")
  @gli.command(:"config:rm") do |c|
    c.action do |global_options, options, args|
      global_config = load_global_config(global_options[:config])

      unless (
        global_config.has_key?(global_options[:host]) &&
        global_config[global_options[:host]].has_key?(args.first)
      ) then
        puts "The #{args.first} config key cannot be found."
      end

      global_config[global_options[:host]].delete(args.first)
      File.open(global_options[:config], 'w+') do |file|
        file.write(global_config.to_yaml)
        puts "The #{args.first} config key has been removed."
      end
    end
  end
end
build_command_set_config() click to toggle source
# File lib/staticdctl/cli.rb, line 118
def build_command_set_config
  @gli.desc("Set a configuration option")
  @gli.arg_name("config_key")
  @gli.command(:"config:set") do |c|
    c.action do |global_options, options, args|
      global_config = load_global_config(global_options[:config])
      global_config[global_options[:host]] ||= {}
      global_config[global_options[:host]][args[0]] = args[1]
      File.open(global_options[:config], 'w+') do |file|
        file.write(global_config.to_yaml)
        puts "The #{args[0]} config key has been set to #{args[1]}."
      end
    end
  end
end
build_command_sites() click to toggle source
# File lib/staticdctl/cli.rb, line 157
def build_command_sites
  @gli.desc("List all sites")
  @gli.command(:sites) do |c|
    c.action do |global_options,options,args|
      staticd_client(global_options) do |client|
        client.sites do |sites|
          puts "Sites hosted on #{global_options[:host]}:"
          puts "No sites yet." unless sites.any?
          sites.each do |site|
            last_release = site.releases.last
            last_release_string = last_release ? last_release.tag : "-"
            domains =
              site.domain_names.map{ |domain| domain.name }.join(", ")
            domains_string = domains.empty? ? "no domains" : domains
            puts " * #{site.name} (#{last_release_string}): #{site.url}"
          end
        end
      end
    end
  end
end
build_commands() click to toggle source
# File lib/staticdctl/cli.rb, line 90
def build_commands
  build_command_config
  build_command_set_config
  build_command_rm_config
  build_command_sites
  build_command_create_site
  build_command_destroy_site
  build_command_domains
  build_command_attach_domain
  build_command_detach_domain
  build_command_releases
  build_command_create_release
end
enable_debugging() click to toggle source
# File lib/staticdctl/cli.rb, line 28
def enable_debugging
  @gli.on_error { |exception| raise exception }
end
load_config(config_file, host) click to toggle source
# File lib/staticdctl/cli.rb, line 38
def load_config(config_file, host)
  config = load_global_config(config_file)
  config && config.has_key?(host) ? config[host] : {}
end
load_global_config(config_file) click to toggle source
# File lib/staticdctl/cli.rb, line 32
def load_global_config(config_file)
  YAML.load_file(config_file)
rescue
  {}
end
set_global_option_config() click to toggle source
# File lib/staticdctl/cli.rb, line 60
def set_global_option_config
  @gli.desc("Staticd configuration file")
  @gli.default_value("#{ENV['HOME']}/.staticdctl.yml")
  @gli.arg_name("<Staticd configuration file>")
  @gli.flag([:c, :config])
end
set_global_option_debug() click to toggle source
# File lib/staticdctl/cli.rb, line 83
def set_global_option_debug
  @gli.desc("Enable debugging (raise exception on error)")
  @gli.default_value(false)
  @gli.arg_name("debug")
  @gli.switch([:d, :debug])
end
set_global_option_host() click to toggle source
# File lib/staticdctl/cli.rb, line 67
def set_global_option_host
  @gli.desc("Staticd API endpoint")
  @gli.default_value(
    ENV["STATICDCTL_ENDPOINT"] || "http://localhost/api/v1"
  )
  @gli.arg_name("<Staticd API endpoint>")
  @gli.flag([:h, :host])
end
set_global_option_site() click to toggle source
# File lib/staticdctl/cli.rb, line 76
def set_global_option_site
  @gli.desc("Site name")
  @gli.default_value(File.basename(Dir.pwd))
  @gli.arg_name("<Site name>")
  @gli.flag([:s, :site])
end
set_global_options() click to toggle source
# File lib/staticdctl/cli.rb, line 53
def set_global_options
  set_global_option_config
  set_global_option_host
  set_global_option_site
  set_global_option_debug
end
staticd_client(options) { |client| ... } click to toggle source
# File lib/staticdctl/cli.rb, line 43
def staticd_client(options)
  config = load_config(options[:config], options[:host])
  client = Staticdctl::StaticdClient.new(
    options[:host],
    access_id: config["access_id"],
    secret_key: config["secret_key"]
  )
  yield client
end