lib/berkshelf/cli.rb in berkshelf-1.0.4 vs lib/berkshelf/cli.rb in berkshelf-1.1.0.rc1

- old
+ new

@@ -29,10 +29,11 @@ namespace "berkshelf" map 'in' => :install map 'up' => :upload map 'ud' => :update + map 'ls' => :list map 'ver' => :version map 'book' => :cookbook class_option :config, type: :string, @@ -128,14 +129,19 @@ aliases: "-e" method_option :only, type: :array, desc: "Only cookbooks that are in these groups.", aliases: "-o" - desc "update", "Update all Cookbooks and their dependencies specified by a Berksfile to their latest versions" - def update - Lockfile.remove! - invoke :install + desc "update [COOKBOOKS]", "Update all Cookbooks and their dependencies specified by a Berksfile to their latest versions" + def update(*cookbook_names) + berksfile = Berksfile.from_file(options[:berksfile]) + + update_options = { + cookbooks: cookbook_names + }.merge(options).symbolize_keys + + berksfile.update(update_options) end method_option :berksfile, type: :string, default: File.join(Dir.pwd, Berkshelf::DEFAULT_FILENAME), @@ -155,17 +161,17 @@ default: false, desc: "Freeze the uploaded cookbooks so that they cannot be overwritten" option :force, type: :boolean, default: false, - desc: "Upload all cookbooks even if a frozen one exists on the target Chef Server" + desc: "Upload cookbook(s) even if a frozen one exists on the target Chef Server" option :ssl_verify, type: :boolean, default: nil, desc: "Disable/Enable SSL verification when uploading cookbooks" - desc "upload", "Upload the Cookbooks specified by a Berksfile or a Berksfile.lock to a Chef Server" - def upload + desc "upload [COOKBOOKS]", "Upload cookbook(s) specified by a Berksfile to the configured Chef Server." + def upload(*cookbook_names) berksfile = ::Berkshelf::Berksfile.from_file(options[:berksfile]) unless Berkshelf::Config.instance.chef.chef_server_url.present? msg = "Could not upload cookbooks: Missing Chef server_url." msg << " Generate or update your Berkshelf configuration that contains a valid Chef Server URL." @@ -176,24 +182,60 @@ msg = "Could not upload cookbooks: Missing Chef node_name." msg << " Generate or update your Berkshelf configuration that contains a valid Chef node_name." raise UploadFailure, msg end - berksfile.upload( + upload_options = { server_url: Berkshelf::Config.instance.chef.chef_server_url, client_name: Berkshelf::Config.instance.chef.node_name, client_key: Berkshelf::Config.instance.chef.client_key, ssl: { verify: (options[:ssl_verify].nil? ? Berkshelf::Config.instance.ssl.verify : options[:ssl_verify]) - } - ) - rescue Ridley::Errors::ClientKeyFileNotFound => e - msg = "Could not upload cookbooks: Missing Chef client key: '#{Berkshelf::Config.instance.chef.client_key}'." - msg << " Generate or update your Berkshelf configuration that contains a valid path to a Chef client key." - raise UploadFailure, msg + }, + cookbooks: cookbook_names + }.merge(options).symbolize_keys + + berksfile.upload(upload_options) end + method_option :berksfile, + type: :string, + default: File.join(Dir.pwd, Berkshelf::DEFAULT_FILENAME), + desc: "Path to a Berksfile to operate off of.", + aliases: "-b", + banner: "PATH" + method_option :except, + type: :array, + desc: "Exclude cookbooks that are in these groups.", + aliases: "-e" + method_option :only, + type: :array, + desc: "Only cookbooks that are in these groups.", + aliases: "-o" + desc "outdated [COOKBOOKS]", "Show all outdated cookbooks (exclusively from the community site)" + def outdated(*cookbook_names) + berksfile = ::Berkshelf::Berksfile.from_file(options[:berksfile]) + Berkshelf.formatter.msg "Listing outdated cookbooks with newer versions available..." + Berkshelf.formatter.msg "BETA: this feature will only pull differences from the community site and will" + Berkshelf.formatter.msg "BETA: ignore all other sources you may have defined" + Berkshelf.formatter.msg "" + + outdated_options = { + cookbooks: cookbook_names + }.merge(options).symbolize_keys + + outdated = berksfile.outdated(outdated_options) + + if outdated.empty? + Berkshelf.formatter.msg "All cookbooks up to date" + else + outdated.each do |cookbook, latest_version| + Berkshelf.formatter.msg "Cookbook '#{cookbook.name} (#{cookbook.version_constraint})' is outdated (#{latest_version})" + end + end + end + method_option :foodcritic, type: :boolean, desc: "Creates a Thorfile with Foodcritic support to lint test your cookbook" method_option :scmversion, type: :boolean, @@ -224,9 +266,42 @@ end ::Berkshelf::InitGenerator.new([path], options).invoke_all ::Berkshelf.formatter.msg "Successfully initialized" + end + + method_option :berksfile, + type: :string, + default: File.join(Dir.pwd, Berkshelf::DEFAULT_FILENAME), + desc: "Path to a Berksfile to operate off of.", + aliases: "-b", + banner: "PATH" + desc "list", "Show all of the cookbooks in the current Berkshelf" + def list + berksfile = ::Berkshelf::Berksfile.from_file(options[:berksfile]) + + Berkshelf.ui.say "Cookbooks installed by your Berksfile:" + Berkshelf.ui.mute { berksfile.resolve }.sort.each do |cookbook| + Berkshelf.ui.say " * #{cookbook.cookbook_name} (#{cookbook.version})" + end + end + + method_option :berksfile, + type: :string, + default: File.join(Dir.pwd, Berkshelf::DEFAULT_FILENAME), + desc: "Path to a Berksfile to operate off of.", + aliases: "-b", + banner: "PATH" + desc "show [COOKBOOK]", "Display the source path on the local file system for the given cookbook" + def show(name = nil) + return list if name.nil? + + berksfile = ::Berkshelf::Berksfile.from_file(options[:berksfile]) + cookbook = Berkshelf.ui.mute { berksfile.resolve }.find{ |cookbook| cookbook.cookbook_name == name } + + raise CookbookNotFound, "Cookbook '#{name}' was not installed by your Berksfile" unless cookbook + Berkshelf.ui.say(cookbook.path) end desc "version", "Display version and copyright information" def version Berkshelf.formatter.msg version_header