package com.sequenceiq.ambari.shell.commands;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
import org.springframework.shell.core.annotation.CliCommand;
import org.springframework.shell.core.annotation.CliOption;
import org.springframework.stereotype.Component;

import com.sequenceiq.ambari.client.AmbariClient;

@Component
public class BlueprintCommands implements CommandMarker {

  private AmbariClient client;

  @Autowired
  public BlueprintCommands(AmbariClient client) {
    this.client = client;
  }

  /**
   * Checks whether the blueprints command is available or not.
   *
   * @return true if available false otherwise
   */
  @CliAvailabilityIndicator({"blueprint list"})
  public boolean isBlueprintListCommandAvailable() {
    return true;
  }

  /**
   * Prints all the blueprints.
   *
   * @return list of blueprints
   */
  @CliCommand(value = "blueprint list", help = "Lists all known blueprints")
  public String listBlueprints() {
    return client.showBlueprints();
  }

  @CliAvailabilityIndicator(value = {"blueprint show"})
  public boolean isBlueprintShowCommandAvailable() {
    return true;
  }

  @CliCommand(value = {"blueprint show"}, help = "Shows the blueprint by its id")
  public String showBlueprint(
    @CliOption(key = {"id"}, mandatory = true, help = "Id of the blueprint") String id) {
    return client.showBlueprint(id);
  }

  @CliAvailabilityIndicator(value = "blueprint add")
  public boolean isBlueprintAddCommandAvailable() {
    return true;
  }

  @CliCommand(value = {"blueprint add"}, help = "Add a new blueprint from URL")
  public String addBlueprint(
    @CliOption(key = "url", mandatory = true, help = "URL of the blueprint to download from") String url) {
    return client.addBlueprint(url);
  }
}

@@ -40,16 +40,157 @@
 mands;%0A%0A
+import java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.IOException;%0Aimport java.net.URL;%0A%0Aimport org.apache.commons.io.IOUtils;%0A
 import o
@@ -1851,16 +1851,35 @@
 int 
-from URL
+with either --url or --file
 %22)%0A 
@@ -1939,35 +1939,36 @@
 l%22, mandatory = 
-tru
+fals
 e, help = %22URL o
@@ -2016,47 +2016,703 @@
  url
-) %7B%0A    return client.addBlueprint(url)
+,%0A    @CliOption(key = %22file%22, mandatory = false, help = %22File which contains the blueprint%22) File file) %7B%0A    String json = file == null ? readContent(url) : readContent(file);%0A    return client.addBlueprint(json) ? %22Blueprint added%22 : %22Cannot add blueprint%22;%0A  %7D%0A%0A  private String readContent(File file) %7B%0A    String content = null;%0A    try %7B%0A      content = IOUtils.toString(new FileInputStream(file));%0A    %7D catch (IOException e) %7B%0A      // not important%0A    %7D%0A    return content;%0A  %7D%0A%0A  private String readContent(String url) %7B%0A    String content = null;%0A    try %7B%0A      content = IOUtils.toString(new URL(url));%0A    %7D catch (IOException e) %7B%0A      // not important%0A    %7D%0A    return content
 ;%0A  

add blueprint from file | TAB completion works for folders too