Пример #1
0
 public static CommandResult RemoveCommand(CommandLineProto proto, CommandLine args)
 {
     try
     {
         var  app       = ConsoleApp.Instance;
         var  options   = GetPathFolderOptions(args);
         bool isVerbose = args.HasSwitch(Names.VerboseSwitch, Names.VerboseMnemonic);
         bool isQuiet   = args.HasSwitch(Names.QuietSwitch, Names.QuietMnemonic);
         var  folders   = new PathFolders(options);
         bool commit    = true;
         folders.Fill();
         int defaultPos = proto.GetValue <int>(Names.PositionSwitch);
         int pos        = args.GetValue <int>(Names.PositionSwitch, Names.PositionMnemonic, defaultPos);
         int defaultLen = proto.GetValue <int>(Names.LengthSwitch);
         int len        = args.GetValue <int>(Names.LengthSwitch, Names.LengthMnemonic, defaultLen);
         if (pos < 1 || pos + len - 1 > folders.Count)
         {
             throw new ApplicationException("Position and length outside valid range for remove operation");
         }
         if (!isQuiet)
         {
             app.WriteError(isVerbose ? folders.ToVerboseString() : folders.ToString());
             app.WriteError();
             commit = app.GetConfirmation($"Remove {len} folder(s) from PATH starting at position {pos}?");
             app.WriteError();
         }
         if (commit)
         {
             int l = len;
             while (l-- > 0)
             {
                 folders.RemoveAt(pos - 1);
             }
         }
         if (options == PathFolderOptions.Process)
         {
             app.WriteOut(folders.ToInlineString());
         }
         else if (commit)
         {
             folders.Commit();
         }
         if (commit)
         {
             return(new CommandResult(true, $"{len} folder(s) removed from PATH"));
         }
         else
         {
             return(new CommandResult(false, "Remove operation canceled"));
         }
     }
     catch (Exception ex)
     {
         return(new CommandResult(false, "Unable to remove folders from PATH", ex));
     }
 }
Пример #2
0
        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));
            }
        }
Пример #3
0
        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));
            }
        }
Пример #4
0
        public static CommandResult AddCommand(CommandLineProto proto, CommandLine args)
        {
            var app = ConsoleApp.Instance;

            try
            {
                int  defaultPos = proto.GetValue <int>(Names.PositionSwitch);
                int  pos        = args.GetValue <int>(Names.PositionSwitch, Names.PositionMnemonic, defaultPos);
                var  options    = GetPathFolderOptions(args);
                bool isVerbose  = args.HasSwitch(Names.VerboseSwitch, Names.VerboseMnemonic);
                bool isQuiet    = args.HasSwitch(Names.QuietSwitch, Names.QuietMnemonic);
                var  folders    = new PathFolders(options);
                folders.Fill();
                var    added  = new PathFolders(options);
                int    parg   = 2;
                string path   = args.GetResolvedValue <string>(parg++);
                bool   commit = true;
                while (path != null)
                {
                    added.Add(new PathFolder(path));
                    path = args.GetResolvedValue <string>(parg++);
                }
                if (!isQuiet)
                {
                    app.WriteError();
                    app.WriteError("Add...");
                    app.WriteError();
                    app.WriteError(added.ToString(), false);
                    app.WriteError();
                    app.WriteError("to...");
                    app.WriteError();
                    app.WriteError(isVerbose ? folders.ToVerboseString() : folders.ToString());
                    app.WriteError();
                    commit = app.GetConfirmation($"at position {pos}?");
                    app.WriteError();
                }
                if (commit)
                {
                    int p = pos - 1;
                    foreach (var f in added)
                    {
                        folders.Insert(p++, f);
                    }
                }
                if (options == PathFolderOptions.Process)
                {
                    app.WriteOut(folders.ToInlineString());
                }
                else if (commit)
                {
                    folders.Commit();
                }
                if (commit)
                {
                    return(new CommandResult(true, $"{added.Count} folder(s) added to PATH at position {pos}"));
                }
                else
                {
                    return(new CommandResult(false, "Add folders operation canceled"));
                }
            }
            catch (Exception ex)
            {
                return(new CommandResult(false, "Unable to add folders to command", ex));
            }
        }
Пример #5
0
        public static CommandResult MoveCommand(CommandLineProto proto, CommandLine args)
        {
            CommandResult result = new CommandResult();

            try
            {
                var  app       = ConsoleApp.Instance;
                var  options   = GetPathFolderOptions(args);
                bool isVerbose = args.HasSwitch(Names.VerboseSwitch, Names.VerboseMnemonic);
                bool isQuiet   = args.HasSwitch(Names.QuietSwitch, Names.QuietMnemonic);
                var  folders   = new PathFolders(options);
                bool commit    = true;
                folders.Fill();
                int defaultPos    = proto.GetValue <int>(Names.PositionSwitch);
                int pos           = args.GetValue <int>(Names.PositionSwitch, Names.PositionMnemonic, defaultPos);
                int defaultLength = proto.GetValue <int>(Names.LengthSwitch);
                int len           = args.GetValue <int>(Names.LengthSwitch, Names.LengthMnemonic, defaultLength);
                int defaultTo     = proto.GetValue <int>(Names.ToSwitch);
                int to            = args.GetValue <int>(Names.ToSwitch, Names.ToMnemonic, defaultTo);
                if (pos < 1 || pos + len - 1 > folders.Count)
                {
                    throw new ApplicationException("Position and length outside valid range for move operation");
                }
                if (to < 1 || to > folders.Count)
                {
                    throw new ApplicationException("Move to position outside valid range");
                }
                if (pos == to)
                {
                    throw new ApplicationException("The source position and the destination position are the same");
                }
                if (!isQuiet)
                {
                    app.WriteError(isVerbose ? folders.ToVerboseString() : folders.ToString());
                    app.WriteError();
                    commit = app.GetConfirmation($"Move {len} folder(s) in PATH starting at position {pos} to position {to}?");
                    app.WriteError();
                }
                if (commit)
                {
                    int l = len;
                    int p = pos - 1;
                    int t = to - 1;
                    while (l-- > 0)
                    {
                        if (t < p)
                        {
                            folders.Insert(t, folders[p]);
                            folders.RemoveAt(p + 1);
                            ++p;
                            ++t;
                        }
                        else
                        {
                            folders.Insert(t + len, folders[p]);
                            folders.RemoveAt(p);
                        }
                    }
                }
                if (options == PathFolderOptions.Process)
                {
                    app.WriteOut(folders.ToInlineString());
                }
                else if (commit)
                {
                    folders.Commit();
                }
                if (commit)
                {
                    return(new CommandResult(true, $"{len} folders moved from position {pos} to position {to} in PATH"));
                }
                else
                {
                    return(new CommandResult(false, "Move operation canceled"));
                }
            }
            catch (Exception ex)
            {
                return(new CommandResult(false, "Unable to move folders in PATH", ex));
            }
        }