|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace VirtualClient |
| 5 | +{ |
| 6 | + using System.Threading.Tasks; |
| 7 | + using System.Threading; |
| 8 | + using System; |
| 9 | + |
| 10 | + internal class InstallCommand |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// Executes the default monitor execution command. |
| 14 | + /// </summary> |
| 15 | + /// <param name="args">The arguments provided to the application on the command line.</param> |
| 16 | + /// <param name="cancellationTokenSource">Provides a token that can be used to cancel the command operations.</param> |
| 17 | + /// <returns>The exit code for the command operations.</returns> |
| 18 | + public async Task<int> ExecuteAsync(string[] args, CancellationTokenSource cancellationTokenSource) |
| 19 | + { |
| 20 | + |
| 21 | + int exitCode = 0; |
| 22 | + |
| 23 | + try |
| 24 | + { |
| 25 | + CancellationToken cancellationToken = cancellationTokenSource.Token; |
| 26 | + Guid dependencyId = Guid.NewGuid(); |
| 27 | + |
| 28 | + Console.Out.WriteLine(); |
| 29 | + Console.Out.WriteLine($"Setup/Install Dependency: {dependencyId}"); |
| 30 | + Console.Out.WriteLine($"Version: 2.0.0"); |
| 31 | + Console.Out.WriteLine($"-------------------------------------------------------"); |
| 32 | + |
| 33 | + try |
| 34 | + { |
| 35 | + Console.Out.Write($"Installing"); |
| 36 | + DateTime randomEndTime = DateTime.UtcNow.AddSeconds(Random.Shared.Next(10, 20)); |
| 37 | + |
| 38 | + while (DateTime.UtcNow < randomEndTime) |
| 39 | + { |
| 40 | + if (cancellationToken.IsCancellationRequested) |
| 41 | + { |
| 42 | + break; |
| 43 | + } |
| 44 | + |
| 45 | + Console.Out.Write("."); |
| 46 | + await Task.Delay(2000); |
| 47 | + } |
| 48 | + |
| 49 | + if (!cancellationToken.IsCancellationRequested) |
| 50 | + { |
| 51 | + Console.Out.WriteLine(); |
| 52 | + Console.Out.WriteLine("Installation Successful"); |
| 53 | + } |
| 54 | + } |
| 55 | + finally |
| 56 | + { |
| 57 | + Console.Out.WriteLine(); |
| 58 | + } |
| 59 | + } |
| 60 | + catch (Exception exc) |
| 61 | + { |
| 62 | + Console.Error.WriteLine(); |
| 63 | + Console.Error.WriteLine("Installation FAILED"); |
| 64 | + Console.Error.WriteLine(); |
| 65 | + Console.Error.WriteLine(exc.Message); |
| 66 | + Console.Error.WriteLine(exc.StackTrace); |
| 67 | + Console.Error.WriteLine(); |
| 68 | + exitCode = 1; |
| 69 | + } |
| 70 | + |
| 71 | + return exitCode; |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments