Skip to content

Commit

Permalink
Fix knife tag create/list/delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuo Yan committed Apr 8, 2011
1 parent 833676f commit 38b05e7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 16 deletions.
31 changes: 26 additions & 5 deletions chef/lib/chef/knife/tag_create.rb
@@ -1,3 +1,23 @@
#
# Author:: Ryan Davis (<ryand-ruby@zenspider.com>)
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Author:: Nuo Yan (<nuo@opscode.com>)
# Copyright:: Copyright (c) 2011 Ryan Davis and Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/knife'

class Chef
Expand All @@ -12,19 +32,20 @@ class TagCreate < Knife

def run
name = @name_args[0]
tags = @name_args[1..-1].join(",").split(/\s*,\s*/)
tags = @name_args[1..-1]

unless name or tags.empty?
if name.nil? || tags.nil? || tags.empty?
show_usage
# TODO: blah blah
ui.fatal("You must specify a node name")
ui.fatal("You must specify a node name and at least one tag.")
exit 1
end

node = Chef::Node.load name
tags.each do |tag|
node.tags << tag
(node.tags << tag).uniq!
end
node.save
ui.info("Created tags #{tags.join(", ")} for node #{name}.")
end
end
end
Expand Down
39 changes: 34 additions & 5 deletions chef/lib/chef/knife/tag_delete.rb
@@ -1,3 +1,23 @@
#
# Author:: Ryan Davis (<ryand-ruby@zenspider.com>)
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Author:: Nuo Yan (<nuo@opscode.com>)
# Copyright:: Copyright (c) 2011 Ryan Davis and Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/knife'

class Chef
Expand All @@ -12,19 +32,28 @@ class TagDelete < Knife

def run
name = @name_args[0]
tags = @name_args[1..-1].join(",").split(/\s*,\s*/)
tags = @name_args[1..-1]

unless name or tags.empty?
if name.nil? || tags.nil? || tags.empty?
show_usage
# TODO: blah blah
ui.fatal("You must specify a node name")
ui.fatal("You must specify a node name and at least one tag.")
exit 1
end

node = Chef::Node.load name
deleted_tags = Array.new
tags.each do |tag|
node.tags.delete tag
unless node.tags.delete(tag).nil?
deleted_tags << tag
end
end
node.save
message = if deleted_tags.empty?
"Nothing has changed. The tags requested to be deleted do not exist."
else
"Deleted the following tags: #{deleted_tags.join(", ")}."
end
ui.info(message)
end
end
end
Expand Down
30 changes: 24 additions & 6 deletions chef/lib/chef/knife/tag_list.rb
@@ -1,3 +1,23 @@
#
# Author:: Ryan Davis (<ryand-ruby@zenspider.com>)
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Author:: Nuo Yan (<nuo@opscode.com>)
# Copyright:: Copyright (c) 2011 Ryan Davis and Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/knife'

class Chef
Expand All @@ -12,17 +32,15 @@ class TagList < Knife

def run
name = @name_args[0]
tags = @name_args[1..-1].join(",").split(/\s*,\s*/)

unless name or tags.empty?
if name.nil?
show_usage
# TODO: blah blah
ui.fatal("You must specify a node name")
ui.fatal("You must specify a node name.")
exit 1
end

node = Chef::Node.load name
output node.tags
node = Chef::Node.load(name)
output(node.tags)
end
end
end
Expand Down

0 comments on commit 38b05e7

Please sign in to comment.