JesseHerrick/Bukkit-CLI

View on GitHub
bin/bukkit

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

$: << File.expand_path(File.dirname(__FILE__) + "/../lib")

require 'rubygems'
require 'commander/import'

require 'bukkit/version'
require 'bukkit.rb'

program :version, Bukkit::VERSION
program :description, "A Command Line Interface for CraftBukkit."

command :new do |c|
    c.syntax = "bukkit new my-awesome-server-name [options]"
    c.summary = "Create a new Bukkit server."
    c.description = "Create a new Bukkit server with the given name."
    c.example "Creates a new server with the name 'my-awesome-server-name'.", "bukkit new my-awesome-server-name"
    c.option "--rb", "Create a new server with the recommended build."
    c.option "--beta", "Create a new server with the beta build."
    c.option "--dev", "Create a new server with the dev build."
    c.option "-f", "--force", "Overwrites the chosen directory if it already exists."
    c.option "--nostart", "Just creates a new server, doesn't start it."

    c.action do |args, options|
    puts "I'm sorry, but it looks like the Bukkit project has been discontinued.".red
    puts "Check out this link to see what's going on: ".green + "http://bukkit.org/threads/mojang-and-the-bukkit-project.309715/"
    end
end
alias_command :create, :new

command :update do |c|
    c.syntax = "bukkit update [options]"
    c.summary = "Download the latest CraftBukkit version."
    c.description = "Download the latest CraftBukkit version from dl.bukkit.org."
    c.example c.summary, "bukkit update"
    c.option "--rb", "Update server with the recommended build."
    c.option "--beta", "Update server with the beta build."
    c.option "--dev", "Update server with the dev build."

    c.action do |args, options|
        rb = options.rb
        beta = options.beta
        dev = options.dev
        force = options.force

        update_options = Hash.new

        # Add the build to the new_options hash.
        if rb
            update_options.merge!(:build => :rb)
        elsif beta
            update_options.merge!(:build => :beta)
        elsif dev
            update_options.merge!(:build => :dev)
        else
            # If the user didn't give a build option then ask for it.
            build = ask "Build [rb/beta/dev]: ".downcase!
            case build
            when "rb", "recommended"
                update_options.merge!(:build => :rb)
            when "beta"
                update_options.merge!(:build => :beta)
            when "dev", "development"
                update_options.merge!(:build => :dev)
            else
                puts "Invalid build option."
            end
        end
        # Now finally send the options to the method.
        Bukkit::Server.update(update_options)
    end
end

command :start do |c|
    c.syntax = "bukkit start"
    c.summary = "Open the plugin's start in the default browser."
    c.description = c.summary
    c.option "--url", "Show just the plugin's URL."
    c.option "-r", "--ram MEMORY", Integer, "Specify the amount of RAM."
    c.example "Start a bukkit server.", "bukkit start"
    c.example "Start a bukkit server with a specified amount of RAM (in megabytes.", "bukkit start --ram 1000"

    c.action do |args, options|
        @ram = options.default[:ram]
        start_options = { :ram => @ram.to_i }

        Bukkit::Server.start(start_options)
    end
end

command :website do |c|
    c.syntax = "bukkit website [options]"
    c.summary = "Open the plugin's website in the default browser."
    c.description = c.summary
    c.example c.summary, "bukkit website plugin-name"
    c.example "Show just the plugin's URL.", "bukkit website plugin-name --url"
    c.option "--url", "Show just the plugin's URL."

    c.action do |args, options|
        plugin_name = args.shift
        abort "You didn't enter a plugin name." if plugin_name.empty?

        plugin = Bukkit::Plugin.new(plugin_name)

        if options.url
            puts "    Website: ".yellow + plugin.website
        else
            plugin.view_website
        end
    end
end

command :install do |c|
    c.syntax = "bukkit install my-favorite-plugin"
    c.summary = "Install a plugin to your server."
    c.description = "An easy way to install a plugin to your server. (case insensitive)"
    c.example c.summary, "bukkit install my-favorite-plugin"

    c.action do |args, options|
        name = args.shift

        if name.nil? || name.empty?
            abort "You didn't enter a plugin name."
        else
            plugin = Bukkit::Plugin.new(name)
            plugin.install
        end
    end
end

command :uninstall do |c|
    c.syntax = "bukkit uninstall my-not-so-favorite-plugin"
    c.summary = "Uninstall a plugin from your server."
    c.description = "An easy way to uninstall a plugin from your server. (case insensitive)"
    c.example c.summary, "bukkit uninstall my-not-so-favorite-plugin"

    c.action do |args, options|
        name = args.shift

        if name.nil? || name.empty?
            abort "You didn't enter a plugin name."
        else
            Bukkit::Plugin.uninstall(name)
        end
    end
end