public static PathFolderOptions GetPathFolderOptions(CommandLine args)
        {
            bool isMachine = args.HasSwitch(Names.MachineSwitch, Names.MachineMnemonic);
            bool isUser    = args.HasSwitch(Names.UserSwitch, Names.UserMnemonic);

            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && (isMachine || isUser))
            {
                throw new ArgumentException("--machine and --user options are valid only on Windows platforms");
            }
            PathFolderOptions options = PathFolderOptions.Process;

            if (isUser)
            {
                options = PathFolderOptions.User;
            }
            if (isMachine)
            {
                options = PathFolderOptions.Machine;
            }
            if (isMachine && isUser)
            {
                throw new ApplicationException($"--{Names.MachineSwitch} and --{Names.UserSwitch} cannot be used together.");
            }
            return(options);
        }
Exemple #2
0
        private EnvironmentVariableTarget GetEnvironmentVariableTarget(PathFolderOptions options)
        {
            switch (options)
            {
            case PathFolderOptions.Process:
                return(EnvironmentVariableTarget.Process);

            case PathFolderOptions.User:
                return(EnvironmentVariableTarget.User);

            case PathFolderOptions.Machine:
                return(EnvironmentVariableTarget.Machine);

            default:
                return(EnvironmentVariableTarget.Process);
            }
        }
        public static CommandResult ListCommand(CommandLineProto proto, CommandLine args)
        {
            var app = ConsoleApp.Instance;

            try
            {
                PathFolderOptions options = GetPathFolderOptions(args);
                PathFolders       folders = new PathFolders(options);
                folders.Fill();
                var    f0      = folders.ToArray();
                string pattern = args.GetResolvedValue <string>(Names.FilterSwitch, Names.FilterMnemonic);
                if (pattern != null)
                {
                    Regex r = new Regex(pattern);
                    f0 = (from f in f0 where r.IsMatch(f.Path) select f).ToArray();
                }
                if (args.HasSwitch(Names.SortSwitch))
                {
                    Array.Sort(f0);
                }
                folders = new PathFolders(f0);
                bool isVerbose = args.HasSwitch(Names.VerboseSwitch, Names.VerboseMnemonic);
                bool isInline  = args.HasSwitch(Names.InlineSwitch, Names.InlineMnemonic);
                if (isVerbose && isInline)
                {
                    return(new CommandResult(false, "--Verbose and --Inline are incompatible options"));
                }
                if (!isInline)
                {
                    app.WriteOut(isVerbose ? folders.ToVerboseString() : folders.ToString());
                }
                else
                {
                    app.WriteOut(folders.ToInlineString());
                }
                return(new CommandResult());
            }
            catch (Exception ex)
            {
                return(new CommandResult(false, "Unable to list PATH elements", ex));
            }
        }
Exemple #4
0
 public PathFolders(PathFolderOptions options = PathFolderOptions.Process)
 {
     _options = options;
 }
        public static CommandResult CleanCommand(CommandLineProto proto, CommandLine args)
        {
            var app = ConsoleApp.Instance;

            try
            {
                PathFolderOptions options   = GetPathFolderOptions(args);
                CommandResult     result    = new CommandResult();
                PathFolders       foriginal = new PathFolders(options);
                PathFolders       fvalid    = new PathFolders(options);
                PathFolders       finvalid  = new PathFolders(options);
                bool isVerbose = args.HasSwitch(Names.VerboseSwitch, Names.VerboseMnemonic);
                bool isQuiet   = args.HasSwitch(Names.QuietSwitch, Names.QuietMnemonic);
                bool commit    = true;
                foriginal.Fill();
                foreach (PathFolder f0 in foriginal)
                {
                    if (f0.IsValid)
                    {
                        fvalid.Add(f0);
                    }
                    else
                    {
                        finvalid.Add(f0);
                    }
                }
                if (finvalid.Count == 0)
                {
                    if (options == PathFolderOptions.Process)
                    {
                        app.WriteOut(fvalid.ToInlineString());
                    }
                    return(new CommandResult(true, "No invalid folders in path"));
                }
                if (!isQuiet)
                {
                    app.WriteError(isVerbose ? finvalid.ToVerboseString() : finvalid.ToString());
                    commit = app.GetConfirmation("Remove folders from PATH?");
                    app.WriteError();
                }
                if (options == PathFolderOptions.Process && commit)
                {
                    app.WriteOut(fvalid.ToInlineString());
                }
                else if (options == PathFolderOptions.Process)
                {
                    app.WriteOut(foriginal.ToInlineString());
                }
                else if (commit)
                {
                    fvalid.Commit();
                }
                if (commit)
                {
                    return(new CommandResult(true, $"{finvalid.Count} folder(s) removed from PATH"));
                }
                else
                {
                    return(new CommandResult(false, "User canceled operation"));
                }
            }
            catch (Exception ex)
            {
                return(new CommandResult(false, "Unable to clean invalid PATH elements", ex));
            }
        }