PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Microsoft.AspNet.SignalR.Utils/Program.cs

https://github.com/mip1983/SignalR
C# | 133 lines | 113 code | 19 blank | 1 comment | 8 complexity | 39cd4c7c67170c15395c12e92038a4ad MD5 | raw file
Possible License(s): Apache-2.0, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace Microsoft.AspNet.SignalR.Utils
  6. {
  7. class Program
  8. {
  9. private static readonly ICommand[] _commands = new ICommand[]
  10. {
  11. new InstallPerformanceCountersCommand(PrintInfo, PrintSuccess, PrintWarning, PrintError),
  12. new UninstallPerformanceCountersCommand(PrintInfo, PrintSuccess, PrintWarning, PrintError),
  13. new GenerateHubProxyCommand(PrintInfo, PrintSuccess, PrintWarning, PrintError)
  14. };
  15. static void Main(string[] args)
  16. {
  17. PrintBanner();
  18. if (args.Length == 0)
  19. {
  20. PrintHelp();
  21. return;
  22. }
  23. var command = ParseCommand(args);
  24. if (command == null)
  25. {
  26. // Unrecognized command
  27. PrintError("Unrecognized command '" + args[0] + "'");
  28. PrintHelp();
  29. Environment.Exit(1); // non-zero is all we need for error
  30. return;
  31. }
  32. try
  33. {
  34. command.Execute(args);
  35. }
  36. catch (Exception ex)
  37. {
  38. ExitWithError(ex.ToString());
  39. return;
  40. }
  41. }
  42. private static ICommand ParseCommand(string[] args)
  43. {
  44. var name = args[0];
  45. var command = _commands.SingleOrDefault(c => c.Names.Contains(name, StringComparer.OrdinalIgnoreCase));
  46. return command;
  47. }
  48. private static void ExitWithError(string error = null)
  49. {
  50. PrintError(error);
  51. Environment.Exit(1); // non-zero is all we need for error
  52. }
  53. private static void PrintBanner()
  54. {
  55. var banner = "SignalR Utility Version: {0}";
  56. Console.WriteLine(String.Format(banner, Assembly.GetExecutingAssembly().GetName().Version));
  57. }
  58. private static void PrintHelp(string error = null)
  59. {
  60. PrintError(error);
  61. var help =
  62. @"usage: signalr <command> [args]
  63. Available commands:
  64. {0}
  65. For more information, visit https://github.com/SignalR/SignalR/wiki";
  66. var commands = String.Join(Environment.NewLine + " ", _commands.Select(c => String.Join(", ", c.Names.Select(n => n)) + " " + c.Help));
  67. Console.WriteLine(String.Format(help, commands));
  68. Console.WriteLine();
  69. }
  70. private static void PrintInfo(string message)
  71. {
  72. if (!String.IsNullOrWhiteSpace(message))
  73. {
  74. Console.WriteLine(message);
  75. }
  76. }
  77. private static void PrintWarning(string info)
  78. {
  79. if (!String.IsNullOrWhiteSpace(info))
  80. {
  81. Console.ForegroundColor = ConsoleColor.Yellow;
  82. Console.WriteLine();
  83. Console.WriteLine("Warning: " + info);
  84. Console.WriteLine();
  85. Console.ResetColor();
  86. }
  87. }
  88. private static void PrintError(string error)
  89. {
  90. if (!String.IsNullOrWhiteSpace(error))
  91. {
  92. Console.ForegroundColor = ConsoleColor.Red;
  93. Console.WriteLine();
  94. Console.WriteLine("Error: " + error);
  95. Console.WriteLine();
  96. Console.ResetColor();
  97. }
  98. }
  99. private static void PrintSuccess(string message)
  100. {
  101. if (!String.IsNullOrWhiteSpace(message))
  102. {
  103. Console.ForegroundColor = ConsoleColor.Green;
  104. Console.WriteLine();
  105. Console.WriteLine(message);
  106. Console.WriteLine();
  107. Console.ResetColor();
  108. }
  109. }
  110. }
  111. }