# class Chef::Knife::ClientCreate

### Public Instance Methods

run() click to toggle source

    # File lib/chef/knife/client_create.rb, line 48
    def run
      @client_name = @name_args[0]

      if @client_name.nil?
        show_usage
        ui.fatal("You must specify a client name")
        exit 1
      end

      client = Chef::ApiClient.new
      client.name(@client_name)
      client.admin(config[:admin])
      client.validator(config[:validator])

      output = edit_data(client)

      # Chef::ApiClient.save will try to create a client and if it exists will update it instead silently
      client = output.save

      # We only get a private_key on client creation, not on client update.
      if client['private_key']
        ui.info("Created #{output}")

        if config[:file]
          File.open(config[:file], "w") do |f|
            f.print(client['private_key'])
          end
        else
          puts client['private_key']
        end
      else
        ui.error "Client '#{client['name']}' already exists"
        exit 1
      end
    end