/// <summary> /// Execution the command invocation by attempting /// to add a new host entry. /// </summary> protected override void ProcessRecord() { var service = HostFileService; var entries = service.GetEntries(); // can't add a duplicate. Throw error... if (entries.Any(x => x.Hostname.AreHostFileStringEqual(Hostname))) { throw new DuplicateHostException(Hostname); } var entry = new HostFileEntry(); entry.Hostname = Hostname; entry.Address = Address; entry.SetTypeFromAddress(); var entryList = new List <HostFileEntry>(); entryList.AddRange(entries); entryList.Add(entry); service.WriteEntries(entryList); WriteObject(entry); Log.WriteLog($"Added host to host file: {entry.ToString()}"); // END FUNCTION }
/// <summary> /// Executes command invocation depending on /// parameter set used. /// </summary> protected override void ProcessRecord() { var service = ServiceManager .Get <IHostFileDataService>(); var entries = service.GetEntries(); // ensure host exists before updating. if (!entries.Any(x => x.Hostname.AreHostFileStringEqual(Hostname))) { throw new MissingHostException(Hostname); } // locate and update entry. var toChange = entries.Single(x => x.Hostname.AreHostFileStringEqual(Hostname)); var oldState = new HostFileEntry() { Address = toChange.Address, Hostname = toChange.Hostname, Type = toChange.Type }; // udpdate address... toChange.Address = Address; var unchanged = entries.Where(x => !x.Hostname.AreHostFileStringEqual(Hostname)); var composite = new List <HostFileEntry>(); composite.AddRange(unchanged); composite.Add(toChange); // write all back out to disk. service.WriteEntries(composite); Log.WriteLog($"Updated host file entry: {oldState.ToString()} to {toChange.ToString()}"); // END FUNCTION }