DocumentGetCommand.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using BO.Console.Commands.Base;
  5. using BO.Console.Extensions;
  6. using Quadarax.Foundation.Core.Logging;
  7. using Quadarax.Foundation.Core.QConsole;
  8. using Quadarax.Foundation.Core.QConsole.Argument;
  9. using Quadarax.Foundation.Core.QConsole.Attributes;
  10. using Quadarax.Foundation.Core.QConsole.Value;
  11. using Quadarax.Foundation.Core.Value;
  12. using Quadarax.Foundation.Core.Value.Extensions;
  13. namespace BO.Console.Commands.Document
  14. {
  15. [CommandDefinition]
  16. internal class DocumentGetCommand : AbstractWrkGetCommand
  17. {
  18. #region *** Constants ***
  19. private const string CS_CMD_DOCGET_NAME = "document_get";
  20. private const string CS_CMD_DOCGET_DESCR = "Get single document with artifacts by specification.";
  21. protected const string CS_ARG_NAME_NOCONTENT = "nocontent";
  22. private const string CS_ARG_HINT_NOCONTENT = "<no_content_saved>";
  23. private const string CS_ARG_DESC_NOCONTENT = "Flag if set skip content saving. Related to arguments '" + CS_ARG_NAME_OUTDIR + "' and '" + CS_ARG_NAME_FORCE + "'.";
  24. protected const string CS_ARG_NAME_OUTDIR = "outdir";
  25. private const string CS_ARG_HINT_OUTDIR = "<output_directory>";
  26. private const string CS_ARG_DESC_OUTDIR = "Alternative output directory to save content. If not exists attempts to create it. If not set use current directory.";
  27. #endregion
  28. #region *** Properties ***
  29. public override string Name => CS_CMD_DOCGET_NAME;
  30. public override string Description => CS_CMD_DOCGET_DESCR;
  31. protected bool IsNoContent { get; set; }
  32. protected string OutputDirectory { get; set; }
  33. #endregion
  34. #region *** Constructor ***
  35. public DocumentGetCommand(Engine engine) : base(engine)
  36. {
  37. }
  38. #endregion
  39. #region *** EXECUTE ***
  40. protected override Result OnExecute()
  41. {
  42. var document = Client.GetWorkspaceDocumentAsync(ApiKey, ApiKeyPassword,IdentificationType, IdentificationValue,UserName).Result;
  43. if (document == null)
  44. throw new Exception($"Document {IdentificationType}: '{IdentificationValue}' not found!");
  45. WriteDtoToConsole(document);
  46. if (!IsNoContent)
  47. {
  48. //var tasksDownload = new List<Task>();
  49. foreach (var art in document.Artifacts)
  50. {
  51. //tasksDownload.Add(Task.Factory.StartNew(async () =>
  52. //{
  53. var bWrite = false;
  54. var fullFileName = art.GetFullFileName(FileSystem, OutputDirectory);
  55. if (FileSystem.File.Exists(fullFileName))
  56. {
  57. if (IsForce.GetValueOrDefault(false))
  58. {
  59. WriteDebugInfo($"Artifact file '{fullFileName}' already exists. Override.");
  60. FileSystem.File.Delete(fullFileName);
  61. WriteDebugInfo($"Artifact file '{fullFileName}' deleted.");
  62. bWrite = true;
  63. }
  64. }
  65. else
  66. {
  67. bWrite = true;
  68. }
  69. if (bWrite)
  70. {
  71. using (var fw = FileSystem.File.OpenWrite(fullFileName))
  72. {
  73. var content = Client.GetWorkspaceDocumentContentAsync(ApiKey, ApiKeyPassword,
  74. document.Id, art.Id, UserName).Result;
  75. if (content != null)
  76. {
  77. content.CopyTo(fw);
  78. fw.Flush();
  79. WriteCaption($"Artifact content file '{art.GetFileName(FileSystem)}' saved.");
  80. }
  81. else
  82. {
  83. WriteError($"Artifact content file '{art.GetFileName(FileSystem)}' returns empty content! Skip.");
  84. }
  85. }
  86. }
  87. else
  88. {
  89. WriteError($"Artifact content file '{art.GetFileName(FileSystem)}' already exists! Skip.");
  90. }
  91. //}));
  92. }
  93. //Task.WaitAll(tasksDownload.ToArray());
  94. }
  95. else
  96. {
  97. WriteInfo("Artifacts content saving skip.");
  98. }
  99. return new Result();
  100. }
  101. #endregion
  102. #region *** Private operations ***
  103. protected override void OnValidateArguments()
  104. {
  105. base.OnValidateArguments();
  106. IsNoContent = GetArgumentValueOrDefault<bool>(CS_ARG_NAME_NOCONTENT);
  107. if (!IsNoContent)
  108. {
  109. OutputDirectory = GetArgumentValueOrDefault<string>(CS_ARG_NAME_OUTDIR).EnsurePathNonBackslash();
  110. if (!FileSystem.Directory.Exists(OutputDirectory))
  111. {
  112. Log(LogSeverityEnum.Debug, $"Output directory '{OutputDirectory}' not exists. Creating...");
  113. FileSystem.Directory.CreateDirectory(OutputDirectory);
  114. Log(LogSeverityEnum.Info, $"Output directory '{OutputDirectory}' created.");
  115. }
  116. }
  117. }
  118. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  119. {
  120. var result = base.OnSetupArguments().ToList();
  121. result.Add(new NamedArgument(CS_ARG_NAME_OUTDIR, CS_ARG_DESC_OUTDIR, CS_ARG_HINT_OUTDIR, TypeValuesEnum.String, FileSystem.Directory.GetCurrentDirectory(), false));
  122. result.Add(new FlagArgument(CS_ARG_NAME_NOCONTENT, CS_ARG_DESC_NOCONTENT, CS_ARG_HINT_NOCONTENT, false));
  123. AppendUserArgument(result);
  124. AppendForceArgument(result);
  125. return result;
  126. }
  127. #endregion
  128. }
  129. }