DocumentGetCommand.cs 6.2 KB

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