Signed-off-by: Brice Figureau <brice-pup...@daysofwonder.com>
---
 bin/filebucket                       |  122 +------------------
 lib/puppet/application/filebucket.rb |   95 +++++++++++++++
 spec/unit/application/filebucket.rb  |  219 ++++++++++++++++++++++++++++++++++
 3 files changed, 318 insertions(+), 118 deletions(-)
 create mode 100644 lib/puppet/application/filebucket.rb
 create mode 100644 spec/unit/application/filebucket.rb

diff --git a/bin/filebucket b/bin/filebucket
index ea75cb6..911bd2e 100755
--- a/bin/filebucket
+++ b/bin/filebucket
@@ -94,122 +94,8 @@
 # Copyright (c) 2005 Reductive Labs, LLC
 # Licensed under the GNU Public License
 
-require 'puppet'
-require 'puppet/network/client'
-require 'getoptlong'
-
-options = [
-    [ "--bucket",   "-b",                      GetoptLong::REQUIRED_ARGUMENT ],
-    [ "--debug",       "-d",                   GetoptLong::NO_ARGUMENT ],
-    [ "--help",                "-h",                   GetoptLong::NO_ARGUMENT 
],
-    [ "--local",       "-l",                   GetoptLong::NO_ARGUMENT ],
-    [ "--remote",      "-r",                   GetoptLong::NO_ARGUMENT ],
-    [ "--verbose",  "-v",                      GetoptLong::NO_ARGUMENT ],
-    [ "--version",  "-V",           GetoptLong::NO_ARGUMENT ]
-]
-
-# Add all of the config parameters as valid options.
-Puppet.settings.addargs(options)
-
-result = GetoptLong.new(*options)
-
-options = {}
-
-begin
-    result.each { |opt,arg|
-        case opt
-            when "--version"
-                puts "%s" % Puppet.version
-                exit
-            when "--help"
-                if Puppet.features.usage?
-                    RDoc::usage && exit
-                else
-                    puts "No help available unless you have RDoc::usage 
installed"
-                    exit
-                end
-            when "--bucket"
-                options[:bucket] = arg
-            when "--verbose"
-                options[:verbose] = true
-            when "--debug"
-                options[:debug] = true
-            when "--local"
-                options[:local] = true
-            when "--remote"
-                options[:remote] = true
-            else
-                Puppet.settings.handlearg(opt, arg)
-        end
-    }
-rescue GetoptLong::InvalidOption => detail
-    $stderr.puts "Try '#{$0} --help'"
-    exit(1)
-end
-
-Puppet::Log.newdestination(:console)
-
-client = nil
-server = nil
-
-trap(:INT) do
-    $stderr.puts "Cancelling"
-    exit(1)
-end
-
-if options[:debug]
-    Puppet::Log.level = :debug
-elsif options[:verbose]
-    Puppet::Log.level = :info
-end
-
-# Now parse the config
-Puppet.parse_config
-
-if Puppet.settings.print_configs?
-        exit(Puppet.settings.print_configs ? 0 : 1)
-end
-
-begin
-    if options[:local] or options[:bucket]
-        path = options[:bucket] || Puppet[:bucketdir]
-        client = Puppet::Network::Client.dipper.new(:Path => path)
-    else
-        require 'puppet/network/handler'
-        client = Puppet::Network::Client.dipper.new(:Server => Puppet[:server])
-    end
-rescue => detail
-    $stderr.puts detail
-    if Puppet[:trace]
-        puts detail.backtrace
-    end
-    exit(1)
-end
-
-mode = ARGV.shift
-case mode
-when "get":
-    md5 = ARGV.shift
-    out = client.getfile(md5)
-    print out
-when "backup":
-    ARGV.each do |file|
-        unless FileTest.exists?(file)
-            $stderr.puts "%s: no such file" % file
-            next
-        end
-        unless FileTest.readable?(file)
-            $stderr.puts "%s: cannot read file" % file
-            next
-        end
-        md5 = client.backup(file)
-        puts "%s: %s" % [file, md5]
-    end
-when "restore":
-    file = ARGV.shift
-    md5 = ARGV.shift
-    client.restore(file, md5)
-else
-    raise "Invalid mode %s" % mode
-end
+require 'puppet/application'
+require 'puppet/application/filebucket'
 
+# launch the filebucket
+Puppet::Application[:filebucket].run
\ No newline at end of file
diff --git a/lib/puppet/application/filebucket.rb 
b/lib/puppet/application/filebucket.rb
new file mode 100644
index 0000000..0ba7c41
--- /dev/null
+++ b/lib/puppet/application/filebucket.rb
@@ -0,0 +1,95 @@
+require 'puppet'
+require 'puppet/application'
+require 'puppet/network/client'
+
+filebucket_options = [
+    [ "--bucket",   "-b",                      GetoptLong::REQUIRED_ARGUMENT ],
+    [ "--debug",       "-d",                   GetoptLong::NO_ARGUMENT ],
+    [ "--help",                "-h",                   GetoptLong::NO_ARGUMENT 
],
+    [ "--local",       "-l",                   GetoptLong::NO_ARGUMENT ],
+    [ "--remote",      "-r",                   GetoptLong::NO_ARGUMENT ],
+    [ "--verbose",  "-v",                      GetoptLong::NO_ARGUMENT ],
+    [ "--version",  "-V",           GetoptLong::NO_ARGUMENT ]
+]
+
+Puppet::Application.new(:filebucket, filebucket_options) do
+
+    should_not_parse_config
+
+    dispatch do
+        ARGV.shift
+    end
+
+    command(:get) do
+        md5 = ARGV.shift
+        out = @client.getfile(md5)
+        print out
+    end
+
+    command(:backup) do
+        ARGV.each do |file|
+            unless FileTest.exists?(file)
+                $stderr.puts "%s: no such file" % file
+                next
+            end
+            unless FileTest.readable?(file)
+                $stderr.puts "%s: cannot read file" % file
+                next
+            end
+            md5 = @client.backup(file)
+            puts "%s: %s" % [file, md5]
+        end
+    end
+
+    command(:restore) do
+        file = ARGV.shift
+        md5 = ARGV.shift
+        @client.restore(file, md5)
+    end
+
+    setup do
+        Puppet::Log.newdestination(:console)
+
+        @client = nil
+        @server = nil
+
+        trap(:INT) do
+            $stderr.puts "Cancelling"
+            exit(1)
+        end
+
+        if options[:debug]
+            Puppet::Log.level = :debug
+        elsif options[:verbose]
+            Puppet::Log.level = :info
+        end
+
+        # Now parse the config
+        Puppet.parse_config
+
+        if Puppet.settings.print_configs?
+                exit(Puppet.settings.print_configs ? 0 : 1)
+        end
+
+        begin
+            if options[:local] or options[:bucket]
+                path = options[:bucket] || Puppet[:bucketdir]
+                @client = Puppet::Network::Client.dipper.new(:Path => path)
+            else
+                require 'puppet/network/handler'
+                @client = Puppet::Network::Client.dipper.new(:Server => 
Puppet[:server])
+            end
+        rescue => detail
+            $stderr.puts detail
+            if Puppet[:trace]
+                puts detail.backtrace
+            end
+            exit(1)
+        end
+    end
+
+    option(:version) do |arg|
+        puts "%s" % Puppet.version
+        exit
+    end
+end
\ No newline at end of file
diff --git a/spec/unit/application/filebucket.rb 
b/spec/unit/application/filebucket.rb
new file mode 100644
index 0000000..d1ac82f
--- /dev/null
+++ b/spec/unit/application/filebucket.rb
@@ -0,0 +1,219 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'puppet/application/filebucket'
+
+describe "Filebucket" do
+    before :each do
+        @filebucket = Puppet::Application[:filebucket]
+    end
+
+    it "should ask Puppet::Application to not parse Puppet configuration file" 
do
+        @filebucket.should_parse_config?.should be_false
+    end
+
+    it "should declare a get command" do
+        @filebucket.should respond_to(:get)
+    end
+
+    it "should declare a backup command" do
+        @filebucket.should respond_to(:backup)
+    end
+
+    it "should declare a restore command" do
+        @filebucket.should respond_to(:restore)
+    end
+
+    it "should declare a version option" do
+        @filebucket.should respond_to(:handle_version)
+    end
+
+    it "should exit after printing the version" do
+        @filebucket.stubs(:puts)
+
+        lambda { @filebucket.handle_version(nil) }.should 
raise_error(SystemExit)
+    end
+
+    describe "during setup" do
+
+        before :each do
+            Puppet::Log.stubs(:newdestination)
+            Puppet.stubs(:settraps)
+            Puppet::Log.stubs(:level=)
+            Puppet.stubs(:parse_config)
+            Puppet::Network::Client.dipper.stubs(:new)
+            @filebucket.options.stubs(:[]).with(any_parameters)
+        end
+
+
+        it "should set console as the log destination" do
+            Puppet::Log.expects(:newdestination).with(:console)
+
+            @filebucket.run_setup
+        end
+
+        it "should trap INT" do
+            @filebucket.expects(:trap).with(:INT)
+
+            @filebucket.run_setup
+        end
+
+        it "should set log level to debug if --debug was passed" do
+            @filebucket.options.stubs(:[]).with(:debug).returns(true)
+
+            Puppet::Log.expects(:level=).with(:debug)
+
+            @filebucket.run_setup
+        end
+
+        it "should set log level to info if --verbose was passed" do
+            @filebucket.options.stubs(:[]).with(:verbose).returns(true)
+
+            Puppet::Log.expects(:level=).with(:info)
+
+            @filebucket.run_setup
+        end
+
+        it "should Parse puppet config" do
+            Puppet.expects(:parse_config)
+
+            @filebucket.run_setup
+        end
+
+        it "should print puppet config if asked to in Puppet config" do
+            @filebucket.stubs(:exit)
+            Puppet.settings.stubs(:print_configs?).returns(true)
+
+            Puppet.settings.expects(:print_configs)
+
+            @filebucket.run_setup
+        end
+
+        it "should exit after printing puppet config if asked to in Puppet 
config" do
+            Puppet.settings.stubs(:print_configs?).returns(true)
+
+            lambda { @filebucket.run_setup }.should raise_error(SystemExit)
+        end
+
+        describe "with local bucket" do
+
+            before :each do
+                @filebucket.options.stubs(:[]).with(:local).returns(true)
+            end
+
+            it "should create a client with the default bucket if none passed" 
do
+                Puppet.stubs(:[]).with(:bucketdir).returns("path")
+
+                Puppet::Network::Client::Dipper.expects(:new).with { |h| 
h[:Path] == "path" }
+
+                @filebucket.run_setup
+            end
+
+            it "should create a local Client dipper with the given bucket" do
+                @filebucket.options.stubs(:[]).with(:bucket).returns("path")
+
+                Puppet::Network::Client::Dipper.expects(:new).with { |h| 
h[:Path] == "path" }
+
+                @filebucket.run_setup
+            end
+
+        end
+
+        describe "with remote bucket" do
+
+            it "should create a remote Client to the configured server" do
+                
Puppet.stubs(:[]).with(:server).returns("puppet.reductivelabs.com")
+
+                Puppet::Network::Client::Dipper.expects(:new).with { |h| 
h[:Server] == "puppet.reductivelabs.com" }
+
+                @filebucket.run_setup
+            end
+
+        end
+
+    end
+
+    describe "when running" do
+
+        before :each do
+            Puppet::Log.stubs(:newdestination)
+            Puppet.stubs(:settraps)
+            Puppet::Log.stubs(:level=)
+            Puppet.stubs(:parse_config)
+            Puppet::Network::Client.dipper.stubs(:new)
+            @filebucket.options.stubs(:[]).with(any_parameters)
+
+            @client = stub 'client'
+            Puppet::Network::Client::Dipper.stubs(:new).returns(@client)
+
+            @filebucket.run_setup
+        end
+
+        it "should use the first non-option parameter as the dispatch" do
+            ARGV.stubs(:shift).returns(:get)
+
+            @filebucket.get_command.should == :get
+        end
+
+        describe "the command get" do
+
+            before :each do
+                @filebucket.stubs(:print)
+            end
+
+            it "should call the client getfile method" do
+                @client.expects(:getfile)
+
+                @filebucket.get
+            end
+
+            it "should call the client getfile method with the given md5" do
+                md5="DEADBEEF"
+                ARGV.stubs(:shift).returns(md5)
+
+                @client.expects(:getfile).with(md5)
+
+                @filebucket.get
+            end
+
+            it "should print the file content" do
+                @client.stubs(:getfile).returns("content")
+
+                @filebucket.expects(:print).returns("content")
+
+                @filebucket.get
+            end
+
+        end
+
+        describe "the command backup" do
+            it "should call the client backup method for each given parameter" 
do
+                @filebucket.stubs(:puts)
+                FileTest.stubs(:exists?).returns(true)
+                FileTest.stubs(:readable?).returns(true)
+                ARGV.stubs(:each).multiple_yields("file1","file2")
+
+                @client.expects(:backup).with("file1")
+                @client.expects(:backup).with("file2")
+
+                @filebucket.backup
+            end
+        end
+
+        describe "the command restore" do
+            it "should call the client getfile method with the given md5" do
+                md5="DEADBEEF"
+                file="testfile"
+                ARGV.stubs(:shift).returns(file,md5)
+
+                @client.expects(:restore).with(file,md5)
+
+                @filebucket.restore
+            end
+        end
+
+    end
+
+
+end
\ No newline at end of file
-- 
1.6.0.2


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to puppet-dev@googlegroups.com
To unsubscribe from this group, send email to 
puppet-dev+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to