aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/StubsUtils/Tango.StubsUtils.Client.CLI/Program.cs
blob: 9fd588906b17877b1d923aa1192f6e6a1387f36c (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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using Tango.StubsUtils.Shared;

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

        static void Main(string[] args)
        {
            StubPackageResponseDTO response = null;

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

                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)
            {
                ExitError($"Error communicating with the stubs service. {ex.Message}");
            }

            if (response.Status == StubPackageResponseStatus.OK)
            {
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine(response.Message);
                Environment.Exit(0);
            }
            else
            {
                ExitError(response.Message);
            }
        }

        private static void ExitError(String error)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Error.WriteLine($"{error}");
            Console.ForegroundColor = ConsoleColor.Gray;
            Environment.Exit(-1);
        }
    }
}