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(); 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(); 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; } } }