package cloudcmd.cld.commands;


import cloudcmd.common.MetaUtil;
import cloudcmd.common.StringUtil;
import cloudcmd.common.index.IndexStorageService;
import jpbetz.cli.*;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;
import java.util.Set;

@SubCommand(name = "find", description = "Query the index of archived files.")
public class Find implements Command
{
  @Arg(name = "tags", optional = true, isVararg = true)
  List<String> _tags;

  @Opt(opt = "p", longOpt = "path", description = "path to find by", required = false)
  String _path;

  @Opt(opt = "f", longOpt = "name", description = "filename to find by", required = false)
  String _filename;

  @Opt(opt = "e", longOpt = "ext", description = "file extension to find by", required = false)
  String _fileext;

  @Override
  public void exec(CommandContext commandLine) throws Exception
  {
    JSONObject filter = new JSONObject();

    if (_tags != null)
    {
      Set<String> tags = MetaUtil.prepareTags(_tags);
      if (tags.size() > 0) filter.put("tags", StringUtil.join(tags, " "));
    }

    if (_path != null) filter.put("path", _path);
    if (_filename != null) filter.put("filename", _filename);
    if (_fileext != null) filter.put("fileext", _fileext);

    JSONArray selections = IndexStorageService.instance().find(filter);

    System.out.println(selections.toString());
  }
}

@@ -680,16 +680,120 @@
 ename;%0A%0A
+  @Opt(opt = %22h%22, longOpt = %22hash%22, description = %22hash to find by%22, required = false)%0A  String _hash;%0A%0A
   @Opt(o
@@ -1358,16 +1358,66 @@
 ileext);
+%0A    if (_hash != null) filter.put(%22hash%22, _hash);
 %0A%0A    JS

add has