Пример #1
0
        /// <summary>
        /// Run the subcommand
        /// </summary>
        /// <param name="mgr">Manager to provide game instances</param>
        /// <param name="opts">Command line parameters paritally handled by parser</param>
        /// <param name="unparsed">Command line parameters not yet handled by parser</param>
        /// <returns>
        /// Exit code
        /// </returns>
        public int RunSubCommand(GameInstanceManager mgr, CommonOptions opts, SubCommandOptions unparsed)
        {
            string[] args     = unparsed.options.ToArray();
            int      exitCode = Exit.OK;

            // Parse and process our sub-verbs
            Parser.Default.ParseArgumentsStrict(args, new MarkSubOptions(), (string option, object suboptions) =>
            {
                // ParseArgumentsStrict calls us unconditionally, even with bad arguments
                if (!string.IsNullOrEmpty(option) && suboptions != null)
                {
                    CommonOptions options = (CommonOptions)suboptions;
                    options.Merge(opts);
                    user     = new ConsoleUser(options.Headless);
                    manager  = mgr ?? new GameInstanceManager(user);
                    exitCode = options.Handle(manager, user);
                    if (exitCode != Exit.OK)
                    {
                        return;
                    }

                    switch (option)
                    {
                    case "auto":
                        exitCode = MarkAuto((MarkAutoOptions)suboptions, true, option, "auto-installed");
                        break;

                    case "user":
                        exitCode = MarkAuto((MarkAutoOptions)suboptions, false, option, "user-selected");
                        break;

                    default:
                        user.RaiseMessage("Unknown command: mark {0}", option);
                        exitCode = Exit.BADOPT;
                        break;
                    }
                }
            }, () => { exitCode = MainClass.AfterHelp(); });
            return(exitCode);
        }
Пример #2
0
        /// <summary>
        /// Run the subcommand
        /// </summary>
        /// <param name="mgr">Manager to provide game instances</param>
        /// <param name="opts">Command line parameters paritally handled by parser</param>
        /// <param name="unparsed">Command line parameters not yet handled by parser</param>
        /// <returns>
        /// Exit code
        /// </returns>
        public int RunSubCommand(KSPManager manager, CommonOptions opts, SubCommandOptions unparsed)
        {
            string[] args     = unparsed.options.ToArray();
            int      exitCode = Exit.OK;

            Parser.Default.ParseArgumentsStrict(args, new AuthTokenSubOptions(), (string option, object suboptions) =>
            {
                if (!string.IsNullOrEmpty(option) && suboptions != null)
                {
                    CommonOptions options = (CommonOptions)suboptions;
                    options.Merge(opts);
                    user = new ConsoleUser(options.Headless);
                    if (manager == null)
                    {
                        manager = new KSPManager(user);
                    }
                    exitCode = options.Handle(manager, user);
                    if (exitCode == Exit.OK)
                    {
                        switch (option)
                        {
                        case "list":
                            exitCode = listAuthTokens(options);
                            break;

                        case "add":
                            exitCode = addAuthToken((AddAuthTokenOptions)options);
                            break;

                        case "remove":
                            exitCode = removeAuthToken((RemoveAuthTokenOptions)options);
                            break;
                        }
                    }
                }
            }, () => { exitCode = MainClass.AfterHelp(); });
            return(exitCode);
        }
Пример #3
0
        public int RunSubCommand(KSPManager manager, CommonOptions opts, SubCommandOptions unparsed)
        {
            int exitCode = Exit.OK;

            // Parse and process our sub-verbs
            Parser.Default.ParseArgumentsStrict(unparsed.options.ToArray(), new RepairSubOptions(), (string option, object suboptions) =>
            {
                // ParseArgumentsStrict calls us unconditionally, even with bad arguments
                if (!string.IsNullOrEmpty(option) && suboptions != null)
                {
                    CommonOptions options = (CommonOptions)suboptions;
                    options.Merge(opts);
                    User = new ConsoleUser(options.Headless);
                    if (manager == null)
                    {
                        manager = new KSPManager(User);
                    }
                    exitCode = options.Handle(manager, User);
                    if (exitCode != Exit.OK)
                    {
                        return;
                    }

                    switch (option)
                    {
                    case "registry":
                        exitCode = Registry(MainClass.GetGameInstance(manager));
                        break;

                    default:
                        User.RaiseMessage("Unknown command: repair {0}", option);
                        exitCode = Exit.BADOPT;
                        break;
                    }
                }
            }, () => { exitCode = MainClass.AfterHelp(); });
            return(exitCode);
        }
Пример #4
0
        public int RunCommand(GameInstanceManager manager, object raw_options)
        {
            CommonOptions opts = raw_options as CommonOptions;
            bool          done = false;

            while (!done)
            {
                // Prompt if not in headless mode
                if (!(opts?.Headless ?? false))
                {
                    Console.Write(
                        manager.CurrentInstance != null
                            ? $"CKAN {Meta.GetVersion()}: {manager.CurrentInstance.game.ShortName} {manager.CurrentInstance.Version()} ({manager.CurrentInstance.Name})> "
                            : $"CKAN {Meta.GetVersion()}> "
                        );
                }
                // Get input
                string command = Console.ReadLine();
                if (command == null || command == exitCommand)
                {
                    done = true;
                }
                else if (command != "")
                {
                    // Parse input as if it was a normal command line,
                    // but with a persistent GameInstanceManager object.
                    int cmdExitCode = MainClass.Execute(manager, opts, command.Split(' '));
                    if ((opts?.Headless ?? false) && cmdExitCode != Exit.OK)
                    {
                        // Pass failure codes to calling process in headless mode
                        // (in interactive mode the user can see the error and try again)
                        return(cmdExitCode);
                    }
                }
            }
            return(Exit.OK);
        }
Пример #5
0
        private int listAuthTokens(CommonOptions opts)
        {
            List <string> hosts = new List <string>(ServiceLocator.Container.Resolve <IConfiguration>().GetAuthTokenHosts());

            if (hosts.Count > 0)
            {
                int longestHostLen  = hostHeader.Length;
                int longestTokenLen = tokenHeader.Length;
                foreach (string host in hosts)
                {
                    longestHostLen = Math.Max(longestHostLen, host.Length);
                    string token;
                    if (ServiceLocator.Container.Resolve <IConfiguration>().TryGetAuthToken(host, out token))
                    {
                        longestTokenLen = Math.Max(longestTokenLen, token.Length);
                    }
                }
                // Create format string: {0,-longestHostLen}  {1,-longestTokenLen}
                string fmt = string.Format("{0}0,-{2}{1}  {0}1,-{3}{1}",
                                           "{", "}", longestHostLen, longestTokenLen);
                user.RaiseMessage(fmt, hostHeader, tokenHeader);
                user.RaiseMessage(fmt,
                                  new string('-', longestHostLen),
                                  new string('-', longestTokenLen)
                                  );
                foreach (string host in hosts)
                {
                    string token;
                    if (ServiceLocator.Container.Resolve <IConfiguration>().TryGetAuthToken(host, out token))
                    {
                        user.RaiseMessage(fmt, host, token);
                    }
                }
            }
            return(Exit.OK);
        }
Пример #6
0
        public static int Execute(KSPManager manager, CommonOptions opts, string[] args)
        {
            // We shouldn't instantiate Options if it's a subcommand.
            // It breaks command-specific help, for starters.
            try
            {
                switch (args[0])
                {
                case "repair":
                    return((new Repair()).RunSubCommand(manager, opts, new SubCommandOptions(args)));

                case "ksp":
                    return((new KSP()).RunSubCommand(manager, opts, new SubCommandOptions(args)));

                case "compat":
                    return((new Compat()).RunSubCommand(manager, opts, new SubCommandOptions(args)));

                case "repo":
                    return((new Repo()).RunSubCommand(manager, opts, new SubCommandOptions(args)));

                case "authtoken":
                    return((new AuthToken()).RunSubCommand(manager, opts, new SubCommandOptions(args)));

                case "cache":
                    return((new Cache()).RunSubCommand(manager, opts, new SubCommandOptions(args)));

                case "mark":
                    return((new Mark()).RunSubCommand(manager, opts, new SubCommandOptions(args)));
                }
            }
            catch (NoGameInstanceKraken)
            {
                return(printMissingInstanceError(new ConsoleUser(false)));
            }
            finally
            {
                log.Info("CKAN exiting.");
            }

            Options cmdline;

            try
            {
                cmdline = new Options(args);
            }
            catch (BadCommandKraken)
            {
                return(AfterHelp());
            }
            finally
            {
                log.Info("CKAN exiting.");
            }

            // Process commandline options.
            CommonOptions options = (CommonOptions)cmdline.options;

            options.Merge(opts);
            IUser user = new ConsoleUser(options.Headless);

            if (manager == null)
            {
                manager = new KSPManager(user);
            }
            else
            {
                manager.User = user;
            }

            try
            {
                int exitCode = options.Handle(manager, user);
                if (exitCode != Exit.OK)
                {
                    return(exitCode);
                }
                // Don't bother with instances or registries yet because some commands don't need them.
                return(RunSimpleAction(cmdline, options, args, user, manager));
            }
            finally
            {
                log.Info("CKAN exiting.");
            }
        }
Пример #7
0
 private static int ConsoleUi(KSPManager manager, CommonOptions opts, string[] args)
 {
     // Debug/verbose output just messes up the screen
     LogManager.GetRepository().Threshold = Level.Warn;
     return(CKAN.ConsoleUI.ConsoleUI.Main_(args, manager, opts.Debug));
 }
Пример #8
0
        /// <summary>
        /// Run whatever action the user has provided
        /// </summary>
        /// <returns>The exit status that should be returned to the system.</returns>
        private static int RunSimpleAction(Options cmdline, CommonOptions options, string[] args, IUser user, KSPManager manager)
        {
            try
            {
                switch (cmdline.action)
                {
                case "gui":
                    return(Gui(manager, (GuiOptions)options, args));

                case "consoleui":
                    return(ConsoleUi(manager, options, args));

                case "prompt":
                    return(new Prompt().RunCommand(manager, cmdline.options));

                case "version":
                    return(Version(user));

                case "update":
                    return((new Update(manager, user)).RunCommand(GetGameInstance(manager), cmdline.options));

                case "available":
                    return((new Available(user)).RunCommand(GetGameInstance(manager), cmdline.options));

                case "add":
                case "install":
                    Scan(GetGameInstance(manager), user, cmdline.action);
                    return((new Install(manager, user)).RunCommand(GetGameInstance(manager), cmdline.options));

                case "scan":
                    return(Scan(GetGameInstance(manager), user));

                case "list":
                    return((new List(user)).RunCommand(GetGameInstance(manager), cmdline.options));

                case "show":
                    return((new Show(user)).RunCommand(GetGameInstance(manager), cmdline.options));

                case "replace":
                    Scan(GetGameInstance(manager), user, cmdline.action);
                    return((new Replace(manager, user)).RunCommand(GetGameInstance(manager), (ReplaceOptions)cmdline.options));

                case "upgrade":
                    Scan(GetGameInstance(manager), user, cmdline.action);
                    return((new Upgrade(manager, user)).RunCommand(GetGameInstance(manager), cmdline.options));

                case "search":
                    return((new Search(user)).RunCommand(GetGameInstance(manager), options));

                case "uninstall":
                case "remove":
                    return((new Remove(manager, user)).RunCommand(GetGameInstance(manager), cmdline.options));

                case "import":
                    return((new Import(manager, user)).RunCommand(GetGameInstance(manager), options));

                case "clean":
                    return(Clean(manager.Cache));

                case "compare":
                    return((new Compare(user)).RunCommand(cmdline.options));

                default:
                    user.RaiseMessage("Unknown command, try --help");
                    return(Exit.BADOPT);
                }
            }
            catch (NoGameInstanceKraken)
            {
                return(printMissingInstanceError(user));
            }
            finally
            {
                RegistryManager.DisposeAll();
            }
        }
Пример #9
0
        // This is required by ISubCommand
        public int RunSubCommand(SubCommandOptions unparsed)
        {
            string[] args = unparsed.options.ToArray();

            #region Aliases

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "use":
                    args[i] = "default";
                    break;

                default:
                    break;
                }
            }

            #endregion

            int exitCode = Exit.OK;
            // Parse and process our sub-verbs
            Parser.Default.ParseArgumentsStrict(args, new KSPSubOptions(), (string option, object suboptions) =>
            {
                // ParseArgumentsStrict calls us unconditionally, even with bad arguments
                if (!string.IsNullOrEmpty(option) && suboptions != null)
                {
                    CommonOptions options = (CommonOptions)suboptions;
                    User     = new ConsoleUser(options.Headless);
                    Manager  = new KSPManager(User);
                    exitCode = options.Handle(Manager, User);
                    if (exitCode != Exit.OK)
                    {
                        return;
                    }

                    switch (option)
                    {
                    case "list":
                        exitCode = ListInstalls();
                        break;

                    case "add":
                        exitCode = AddInstall((AddOptions)suboptions);
                        break;

                    case "rename":
                        exitCode = RenameInstall((RenameOptions)suboptions);
                        break;

                    case "forget":
                        exitCode = ForgetInstall((ForgetOptions)suboptions);
                        break;

                    case "use":
                    case "default":
                        exitCode = SetDefaultInstall((DefaultOptions)suboptions);
                        break;

                    default:
                        User.RaiseMessage("Unknown command: ksp {0}", option);
                        exitCode = Exit.BADOPT;
                        break;
                    }
                }
            }, () => { exitCode = MainClass.AfterHelp(); });
            RegistryManager.DisposeAll();
            return(exitCode);
        }
Пример #10
0
        public static int Main(string[] args)
        {
            // Launch debugger if the "--debugger" flag is present in the command line arguments.
            // We want to do this as early as possible so just check the flag manually, rather than doing the
            // more robust argument parsing.
            if (args.Any(i => i == "--debugger"))
            {
                Debugger.Launch();
            }

            if (args.Length == 1 && args.Any(i => i == "--verbose" || i == "--debug"))
            {
                // Start the gui with logging enabled #437
                var guiCommand = args.ToList();
                guiCommand.Insert(0, "gui");
                args = guiCommand.ToArray();
            }

            Logging.Initialize();
            log.Info("CKAN started.");

            // If we're starting with no options then invoke the GUI instead.
            if (args.Length == 0)
            {
                return(Gui(new GuiOptions(), args));
            }

            // We shouldn't instantiate Options if it's a subcommand.
            // It breaks command-specific help, for starters.
            try
            {
                switch (args[0])
                {
                case "repair":
                    var repair = new Repair();
                    return(repair.RunSubCommand(new SubCommandOptions(args)));

                case "ksp":
                    var ksp = new KSP();
                    return(ksp.RunSubCommand(new SubCommandOptions(args)));

                case "compat":
                    var compat = new CompatSubCommand();
                    return(compat.RunSubCommand(new SubCommandOptions(args)));

                case "repo":
                    var repo = new Repo();
                    return(repo.RunSubCommand(new SubCommandOptions(args)));
                }
            }
            catch (NoGameInstanceKraken)
            {
                return(printMissingInstanceError(new ConsoleUser(false)));
            }
            finally
            {
                log.Info("CKAN exiting.");
            }

            Options cmdline;

            try
            {
                cmdline = new Options(args);
            }
            catch (BadCommandKraken)
            {
                return(AfterHelp());
            }
            finally
            {
                log.Info("CKAN exiting.");
            }

            // Process commandline options.
            CommonOptions options = (CommonOptions)cmdline.options;
            IUser         user    = new ConsoleUser(options.Headless);
            KSPManager    manager = new KSPManager(user);

            try
            {
                int exitCode = options.Handle(manager, user);
                if (exitCode != Exit.OK)
                {
                    return(exitCode);
                }
                // Don't bother with instances or registries yet because some commands don't need them.
                return(RunSimpleAction(cmdline, options, args, user, manager));
            }
            finally
            {
                log.Info("CKAN exiting.");
            }
        }
Пример #11
0
        /// <summary>
        /// Run whatever action the user has provided
        /// </summary>
        /// <returns>The exit status that should be returned to the system.</returns>
        private static int RunAction(Options cmdline, CommonOptions options, string[] args, IUser user, KSPManager manager)
        {
            switch (cmdline.action)
            {
            case "gui":
                return(Gui((GuiOptions)options, args));

            case "version":
                return(Version(user));

            case "update":
                return((new Update(user)).RunCommand(manager.CurrentInstance, (UpdateOptions)cmdline.options));

            case "available":
                return((new Available(user)).RunCommand(manager.CurrentInstance, (AvailableOptions)cmdline.options));

            //return Available(manager.CurrentInstance, user);

            case "install":
                Scan(manager.CurrentInstance, user, cmdline.action);
                return((new Install(user)).RunCommand(manager.CurrentInstance, (InstallOptions)cmdline.options));

            case "scan":
                return(Scan(manager.CurrentInstance, user));

            case "list":
                return((new List(user)).RunCommand(manager.CurrentInstance, (ListOptions)cmdline.options));

            case "show":
                return((new Show(user)).RunCommand(manager.CurrentInstance, (ShowOptions)cmdline.options));

            case "search":
                return((new Search(user)).RunCommand(manager.CurrentInstance, options));

            case "remove":
                return((new Remove(user)).RunCommand(manager.CurrentInstance, cmdline.options));

            case "upgrade":
                Scan(manager.CurrentInstance, user, cmdline.action);
                return((new Upgrade(user)).RunCommand(manager.CurrentInstance, cmdline.options));

            case "clean":
                return(Clean(manager.CurrentInstance));

            case "repair":
                var repair = new Repair(manager.CurrentInstance, user);
                return(repair.RunSubCommand((SubCommandOptions)cmdline.options));

            case "ksp":
                var ksp = new KSP(manager, user);
                return(ksp.RunSubCommand((SubCommandOptions)cmdline.options));

            case "compat":
                var compat = new CompatSubCommand(manager, user);
                return(compat.RunSubCommand((SubCommandOptions)cmdline.options));

            case "repo":
                var repo = new Repo(manager, user);
                return(repo.RunSubCommand((SubCommandOptions)cmdline.options));

            case "compare":
                return((new Compare(user)).RunCommand(manager.CurrentInstance, cmdline.options));

            default:
                user.RaiseMessage("Unknown command, try --help");
                return(Exit.BADOPT);
            }
        }
Пример #12
0
Файл: Repo.cs Проект: zilti/CKAN
        // This is required by ISubCommand
        public int RunSubCommand(KSPManager manager, CommonOptions opts, SubCommandOptions unparsed)
        {
            string[] args = unparsed.options.ToArray();

            #region Aliases

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "remove":
                    args[i] = "forget";
                    break;
                }
            }

            #endregion

            int exitCode = Exit.OK;

            // Parse and process our sub-verbs
            Parser.Default.ParseArgumentsStrict(args, new RepoSubOptions(), (string option, object suboptions) =>
            {
                // ParseArgumentsStrict calls us unconditionally, even with bad arguments
                if (!string.IsNullOrEmpty(option) && suboptions != null)
                {
                    CommonOptions options = (CommonOptions)suboptions;
                    options.Merge(opts);
                    User     = new ConsoleUser(options.Headless);
                    Manager  = manager ?? new KSPManager(User);
                    exitCode = options.Handle(Manager, User);
                    if (exitCode != Exit.OK)
                    {
                        return;
                    }

                    switch (option)
                    {
                    case "available":
                        exitCode = AvailableRepositories();
                        break;

                    case "list":
                        exitCode = ListRepositories();
                        break;

                    case "add":
                        exitCode = AddRepository((AddOptions)suboptions);
                        break;

                    case "remove":
                    case "forget":
                        exitCode = ForgetRepository((ForgetOptions)suboptions);
                        break;

                    case "default":
                        exitCode = DefaultRepository((DefaultOptions)suboptions);
                        break;

                    default:
                        User.RaiseMessage("Unknown command: repo {0}", option);
                        exitCode = Exit.BADOPT;
                        break;
                    }
                }
            }, () => { exitCode = MainClass.AfterHelp(); });
            return(exitCode);
        }