コード例 #1
0
ファイル: CommandProcessor.cs プロジェクト: yyc12345/bpm
        public static bool Process(string command)
        {
            return(parser.ParseArguments <ExitOption, ConfigOption, SwitchOption, ClientOption, ImportOption, LsOption, ShowOption, AddpkgOption, EditpkgOption, DelpkgOption, AddverOption, EditverOption, DelverOption, HelpOption>(
                       CommandSplitter.Split(command))
                   .MapResult(
                       (ExitOption opt) => {
                if (opt.IsForce)
                {
                    return true;
                }
                else
                {
                    if (!General.IsMaintaining)
                    {
                        General.CoreTcpProcessor.StopListen();
                        Console.WriteLine("Waiting the release of resources...");
                        if (General.ManualResetEventList.Count != 0)
                        {
                            WaitHandle.WaitAll(General.ManualResetEventList.ToArray());
                        }
                    }
                    else
                    {
                        General.GeneralDatabase.Close();
                    }

                    General.RecordFileManager.Close();
                    return true;
                }
            },
                       (ConfigOption opt) => {
                if (opt.Key is null)
                {
                    foreach (var item in General.ConfigManager.Configuration.Keys)
                    {
                        Console.Write($"{item}: ");
                        Console.Write($"{General.ConfigManager.Configuration[item]}\n");
                    }
                }
                else
                {
                    if (opt.NewValue is null)
                    {
                        if (General.ConfigManager.Configuration.Keys.Contains(opt.Key))
                        {
                            Console.WriteLine(General.ConfigManager.Configuration[opt.Key]);
                        }
                    }
                    else
                    {
                        if (General.ConfigManager.Configuration.Keys.Contains(opt.Key))
                        {
                            General.ConfigManager.Configuration[opt.Key] = opt.NewValue;
                            General.ConfigManager.Save();
                            Console.WriteLine("New value has been applied");
                        }
                    }
                }
                return false;
            },
                       (SwitchOption opt) => {
                if (!General.IsMaintaining)
                {
                    General.CoreTcpProcessor.StopListen();
                    Console.WriteLine("Waiting the release of resources...");
                    if (General.ManualResetEventList.Count != 0)
                    {
                        WaitHandle.WaitAll(General.ManualResetEventList.ToArray());
                    }

                    General.GeneralDatabase.Open();

                    General.IsMaintaining = true;
                    ConsoleAssistance.WriteLine("Switch to maintain mode successfully.", ConsoleColor.Yellow);
                }
                else
                {
                    General.GeneralDatabase.Close();
                    //force update verify code
                    ConsoleAssistance.WriteLine("Updating verify code....", ConsoleColor.White);
                    General.VerifyBytes = SignVerifyHelper.SignData(Information.WorkPath.Enter("package.db").Path, Information.WorkPath.Enter("pri.key").Path);
                    General.ConfigManager.Configuration["VerifyBytes"] = Convert.ToBase64String(General.VerifyBytes);
                    General.ConfigManager.Save();

                    General.CoreTcpProcessor.StartListen();

                    General.IsMaintaining = false;
                    ConsoleAssistance.WriteLine("Switch to running mode successfully.", ConsoleColor.Yellow);
                }
                return false;
            },
                       (ClientOption opt) => {
                if (!CheckStatus(false))
                {
                    return false;
                }

                ConsoleAssistance.WriteLine($"Current client: {General.ManualResetEventList.Count}", ConsoleColor.Yellow);
                return false;
            },
                       (ImportOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }

                ConsoleAssistance.WriteLine("import is a dangerous command. It will load all script and run it without any error judgement! It couldn't be stopped before all of commands has been executed!", ConsoleColor.Yellow);
                var confirm = new Random().Next(100, 9999);
                ConsoleAssistance.WriteLine($"Type this random number to confirm your operation: {confirm}", ConsoleColor.Yellow);
                if (Console.ReadLine() == confirm.ToString())
                {
                    if (System.IO.File.Exists(opt.FilePath))
                    {
                        ImportStack.AppendImportedCommands(opt.FilePath);
                    }
                    else
                    {
                        ConsoleAssistance.WriteLine("Cannot find specific file", ConsoleColor.Red);
                    }
                }
                return false;
            },
                       (LsOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }

                if (opt.Condition is null)
                {
                    PackageManager.Ls(General.GeneralDatabase, "");
                }
                else
                {
                    PackageManager.Ls(General.GeneralDatabase, opt.Condition);
                }
                return false;
            },
                       (ShowOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }
                PackageManager.Show(General.GeneralDatabase, opt.FullPackageName);
                return false;
            },
                       (AddpkgOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }
                PackageManager.AddPackage(General.GeneralDatabase, opt);
                return false;
            },
                       (EditpkgOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }
                PackageManager.EditPackage(General.GeneralDatabase, opt);
                return false;
            },
                       (DelpkgOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }
                PackageManager.RemovePackage(General.GeneralDatabase, opt.Name);
                return false;
            },
                       (AddverOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }
                PackageManager.AddVersion(General.GeneralDatabase, opt);
                return false;
            },
                       (EditverOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }
                PackageManager.EditVersion(General.GeneralDatabase, opt);
                return false;
            },
                       (DelverOption opt) => {
                if (!CheckStatus(true))
                {
                    return false;
                }
                PackageManager.RemoveVersion(General.GeneralDatabase, opt.Name);
                return false;
            },
                       (HelpOption opt) => {
                OutputHelp();
                return false;
            },
                       errs => { ConsoleAssistance.WriteLine("Unknow command. Use help to find the correct command", ConsoleColor.Red); return false; }));
        }