aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/StubsUtils/Tango.StubsUtils.SessionClient.CLI/Program.cs
blob: 60f4c3def98a0747404b5c687748b148263319c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.StubsUtils.Shared;

namespace Tango.StubsUtils.SessionClient.CLI
{
    class Program
    {
        private const string PIPE_NAME = "Tango_Stubs_Server";

        static void Main(string[] args)
        {
            String line = String.Empty;

            Console.WriteLine("Tango Stub Session Client v1.0");

            while (true)
            {
                Console.Write("> ");
                line = Console.ReadLine();

                if (line.ToLower() == "exit")
                {
                    Environment.Exit(0);
                }

                args = line.Split(' ');

                StubPackageResponseDTO response = null;

                try
                {
                    var _client = new NamedPipeClientStream(PIPE_NAME);
                    _client.Connect(1000);

                    var writer = new StreamWriter(_client);

                    var jsonRequest = String.Join("^", args);

                    writer.WriteLine(jsonRequest);
                    writer.Flush();

                    var reader = new StreamReader(_client);
                    String responseString = reader.ReadToEnd();
                    response = new StubPackageResponseDTO();
                    response.Status = responseString.StartsWith("Status: OK") ? StubPackageResponseStatus.OK : StubPackageResponseStatus.Error;
                    response.Message = responseString;
                }
                catch (Exception ex)
                {
                    OnError($"Status: Error\nError: Error communicating with the stubs service. {ex.Message}");
                    continue;
                }

                if (response.Status == StubPackageResponseStatus.OK)
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine(response.Message);
                }
                else
                {
                    OnError(response.Message);
                }
            }
        }

        private static void OnError(String error)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"{error}");
            Console.ForegroundColor = ConsoleColor.Gray;
        }
    }
}