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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
using ConsoleTables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Cryptography;
using Tango.DataStore.Web;
using Tango.Settings;
using Tango.Web;
namespace Tango.DataStore.CLI
{
public class DataStoreConsole
{
public void Get(GetOptions options)
{
try
{
ApplyAutoLogin(options);
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", "PROTO TYPE", "STATE", "GLOBAL", "LOCAL");
foreach (var item in items)
{
table.AddRow(item.Collection, item.Key, item.DataType, item.ProtoMessageType != MessageType.None ? item.ProtoMessageType.ToString() : null, 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)
{
try
{
ApplyAutoLogin(options);
if (options.MachineSerialNumber != null)
{
Console.WriteLine($"Storing data store value for '{options.MachineSerialNumber}'...");
}
else
{
Console.WriteLine("Storing global data store value...");
}
var client = CreateClient(options.Email, options.Password, options.Environment);
if (options.DataType == Web.DataType.Proto)
{
options.Value = options.Value.ToStringOrEmpty().Replace("'", "\"");
}
client.Put(new DataStoreWebPutItem()
{
Collection = options.Collection,
Key = options.Key,
DataType = options.DataType,
MachineSerialNumber = options.MachineSerialNumber,
ProtoMessageType = options.ProtoMessageType,
Value = options.Value
});
Console.WriteLine($"Item '{options.Key}' stored successfully.");
}
catch (Exception ex)
{
Console.WriteLine(ex.FlattenMessage());
}
}
public void AutoLogin(LoginConfig options)
{
MachineLevelCryptographer crypt = new MachineLevelCryptographer();
var settings = SettingsManager.Default.GetOrCreate<DataStoreUtilSettings>();
settings.Email = options.Email;
settings.Password = crypt.Encrypt(options.Password);
settings.Save();
}
private void ApplyAutoLogin(OptionsBase options)
{
if (options.Email == null && options.Password == null)
{
MachineLevelCryptographer crypt = new MachineLevelCryptographer();
var settings = SettingsManager.Default.GetOrCreate<DataStoreUtilSettings>();
options.Email = settings.Email;
options.Password = crypt.Decrypt(settings.Password);
}
}
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 = email,
Password = password,
});
http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", response.Token);
return dsClient;
}
}
}
|