protected override async Task <ExitCode> Run() { if (ID == -1) { Console.WriteLine("{0,-3} {1,-21} {2,-4}", "ID", "Time", "Values"); foreach (var x in BackupsHandler.Backups) { Console.WriteLine("{0,-3} {1,-21} {2,-4}", $"{x.ID}", $"{x.Time:G}", $"{x.Value.Count(c => c.Equals(';'))}"); } return(ExitCode.Ok); } var backup = BackupsHandler.LoadBackup(ID); if (backup == null) { return(CLIUtils.Exit($"Unable to find Backup with ID {ID}!", ExitCode.Error)); } CLIUtils.Log($"Loaded Backup with ID {backup.ID} from {backup.Time:f}"); try { Environment.SetEnvironmentVariable("Path", backup.Value, EnvironmentVariableTarget.Machine); return(ExitCode.Ok); } catch (Exception e) { CLIUtils.LogException(e, "Unable to set PATH variable!"); return(ExitCode.Error); } }
protected override async Task <ExitCode> Run() { string?current; try { current = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine); } catch (Exception e) { CLIUtils.LogException(e, "Unable to read PATH variable!"); return(ExitCode.Error); } if (current == null) { CLIUtils.Log("PATH variable is null!"); return(ExitCode.Error); } CLIUtils.Log("Creating Backup..."); BackupsHandler.SaveBackup(current); CLIUtils.Log("Cleaning variable..."); List <string> values = current.Split(";") .Select(x => { var r = x.Replace("/", "\\"); if (r.EndsWith("\\")) { r = r.Substring(0, r.Length - 1); } return(r); }) .ToList(); if (RemoveDuplicates) { var count = values.Count; values = values.Distinct().ToList(); CLIUtils.Log($"Removed {count-values.Count} duplicates"); } if (RemoveNotExisting) { var removed = values.RemoveAll(x => !Directory.Exists(x)); CLIUtils.Log($"Removed {removed} not existing paths"); } CLIUtils.Log($"Finished cleaning, before: {current.Split(";").Length}, after: {values.Count}"); try { Environment.SetEnvironmentVariable("Path", values.Aggregate((x, y) => $"{x};{y}"), EnvironmentVariableTarget.Machine); return(ExitCode.Ok); } catch (SecurityException securityException) { CLIUtils.LogException(securityException, "Unable to set PATH due to Security Exception, try running the program as Admin"); return(ExitCode.Error); } catch (Exception e) { CLIUtils.LogException(e, "Unable to set PATH variable!"); return(ExitCode.Error); } }