// Create new connection profile public void AddProfile(string name, string host, string user, string password) { if (ProfileExists(name)) { throw new KanpachiProfileException($"Profile {name} already exists."); } KanpachiProfile profile = new KanpachiProfile(name, host, user); try{ if (GetActiveProfile(true).Name == name) { System.Console.WriteLine($"Profile {name} is already active."); return; } } catch (KanpachiProfileException) { profile.IsActive = true; // no active profile, so set this new one } if (password.Length > 0) { profile.PasswordDecrypted = password; profile.Password = SecUtils.EncryptProfile(profile); } WriteProfile(profile); Console.WriteLine($"Added profile {name}."); }
// Write profile to json file public void WriteProfile(KanpachiProfile profile) { if (!Directory.Exists(ProfilesPath)) { Directory.CreateDirectory(ProfilesPath); } using (StreamWriter f = File.CreateText(GetProfilePath(profile.Name))){ if (profile.PasswordDecrypted != null && profile.PasswordDecrypted.Length > 0) { profile.Password = SecUtils.EncryptProfile(profile); } f.Write(JsonConvert.SerializeObject(profile, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() })); } }