aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/DataStore/Tango.DataStore.CLI/Program.cs
blob: db4f5462040cd348cd2709078ea31a24c0d305a1 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using CommandLine;
using ConsoleTables;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.DataStore.Web;
using Tango.Web;

namespace Tango.DataStore.CLI
{
    class Program
    {
        static void Main(string[] args)
        {
            var console = new DataStoreConsole();

            var result = Parser.Default.ParseArguments<GetOptions, PutOptions>(args)
            .WithParsed<GetOptions>((options) =>
            {
                console.Get(options);
            })
            .WithParsed<PutOptions>((options) =>
            {
                console.Put(options);
            })
            .WithNotParsed((errors) =>
            {

            });

            if (Debugger.IsAttached)
            {
                Console.WriteLine();
                Console.WriteLine("Press return to exit...");
                Console.ReadLine();
            }
        }
    }

    public class DataStoreConsole
    {
        public void Get(GetOptions options)
        {
            try
            {
                if (options.MachineSerialNumber != null)
                {
                    Console.WriteLine($"Retrieving data store values for '{options.MachineSerialNumber}'...");
                }
                else
                {
                    Console.WriteLine("Retrieving global data store values...");
                }

                var client = CreateClient(options.Email, options.Password, options.Environment);

                var items = client.Get(options.MachineSerialNumber, options.Collection, options.Key).ToList();

                ConsoleTable table = new ConsoleTable("COLLECTION", "KEY", "DATA TYPE", "STATE", "GLOBAL", "LOCAL");

                foreach (var item in items)
                {
                    table.AddRow(item.Collection, item.Key, item.DataType, item.Type, item.GlobalValue.ToStringSafe().ToOneLine(), item.LocalValue.ToStringSafe().ToOneLine());
                }

                Console.WriteLine();
                Console.WriteLine("DATA STORE RESULTS:");
                Console.WriteLine();

                table.Write();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.FlattenMessage());
            }
        }

        public void Put(PutOptions options)
        {

        }

        private DataStoreClient CreateClient(String email, String password, DeploymentSlot slot)
        {
            String token = String.Empty;

            HttpClient http = new HttpClient();
            DataStoreClient dsClient = new DataStoreClient(slot.ToAddress(), http);
            var response = dsClient.Login(new LoginRequest()
            {
                Email = "roy@twine-s.com",
                Password = "1Creativity",
            });
            http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", response.Token);

            return dsClient;
        }
    }
}