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 List<string> guiCommand = args.ToList(); guiCommand.Insert(0, "gui"); args = guiCommand.ToArray(); } BasicConfigurator.Configure(); LogManager.GetRepository().Threshold = Level.Warn; log.Debug("CKAN started"); Options cmdline; // If we're starting with no options then invoke the GUI instead. if (args.Length == 0) { return Gui(new GuiOptions(), args); } IUser user; try { cmdline = new Options(args); } catch (BadCommandKraken) { // Our help screen will already be shown. Let's add some extra data. user = new ConsoleUser(false); user.RaiseMessage("You are using CKAN version {0}", Meta.Version()); return Exit.BADOPT; } // Process commandline options. var options = (CommonOptions)cmdline.options; user = new ConsoleUser(options.Headless); CheckMonoVersion(user, 3, 1, 0); if ((Platform.IsUnix || Platform.IsMac) && CmdLineUtil.GetUID() == 0) { if (!options.AsRoot) { user.RaiseError(@"You are trying to run CKAN as root. This is a bad idea and there is absolutely no good reason to do it. Please run CKAN from a user account (or use --asroot if you are feeling brave)."); return Exit.ERROR; } else { user.RaiseMessage("Warning: Running CKAN as root!"); } } if (options.Debug) { LogManager.GetRepository().Threshold = Level.Debug; log.Info("Debug logging enabled"); } else if (options.Verbose) { LogManager.GetRepository().Threshold = Level.Info; log.Info("Verbose logging enabled"); } // Assign user-agent string if user has given us one if (options.NetUserAgent != null) { Net.UserAgentString = options.NetUserAgent; } // User provided KSP instance if (options.KSPdir != null && options.KSP != null) { user.RaiseMessage("--ksp and --kspdir can't be specified at the same time"); return Exit.BADOPT; } KSPManager manager= new KSPManager(user); if (options.KSP != null) { // Set a KSP directory by its alias. try { manager.SetCurrentInstance(options.KSP); } catch (InvalidKSPInstanceKraken) { user.RaiseMessage("Invalid KSP installation specified \"{0}\", use '--kspdir' to specify by path, or 'list-installs' to see known KSP installations", options.KSP); return Exit.BADOPT; } } else if (options.KSPdir != null) { // Set a KSP directory by its path manager.SetCurrentInstanceByPath(options.KSPdir); } else if (! (cmdline.action == "ksp" || cmdline.action == "version")) { // Find whatever our preferred instance is. // We don't do this on `ksp/version` commands, they don't need it. CKAN.KSP ksp = manager.GetPreferredInstance(); if (ksp == null) { user.RaiseMessage("I don't know where KSP is installed."); user.RaiseMessage("Use 'ckan ksp help' for assistance on setting this."); return Exit.ERROR; } else { log.InfoFormat("Using KSP install at {0}",ksp.GameDir()); } } #region Aliases switch (cmdline.action) { case "add": cmdline.action = "install"; break; case "uninstall": cmdline.action = "remove"; break; } #endregion 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 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 "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; } }
public static int Execute(GameInstanceManager 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 "instance": return((new GameInstance()).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 GameInstanceManager(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."); } }
public static int Main(string[] args) { BasicConfigurator.Configure(); LogManager.GetRepository().Threshold = Level.Warn; log.Debug("CKAN started"); IUser user = new ConsoleUser(); CheckMonoVersion(user, 3, 1, 0); // If we're starting with no options then invoke the GUI instead. if (args.Length == 0) { return Gui(); } Options cmdline; try { cmdline = new Options(args); } catch (BadCommandKraken) { // Our help screen will already be shown. Let's add some extra data. user.RaiseMessage("You are using CKAN version {0}", Meta.Version()); return Exit.BADOPT; } // Process commandline options. var options = (CommonOptions) cmdline.options; if (options.Debug) { LogManager.GetRepository().Threshold = Level.Debug; log.Info("Debug logging enabled"); } else if (options.Verbose) { LogManager.GetRepository().Threshold = Level.Info; log.Info("Verbose logging enabled"); } // Assign user-agent string if user has given us one if (options.NetUserAgent != null) { Net.UserAgentString = options.NetUserAgent; } // TODO: Allow the user to specify just a directory. // User provided KSP instance if (options.KSPdir != null && options.KSP != null) { user.RaiseMessage("--ksp and --kspdir can't be specified at the same time"); return Exit.BADOPT; } KSPManager manager= new KSPManager(user); if (options.KSP != null) { // Set a KSP directory by its alias. try { manager.SetCurrentInstance(options.KSP); } catch (InvalidKSPInstanceKraken) { user.RaiseMessage("Invalid KSP installation specified \"{0}\", use '--kspdir' to specify by path, or 'list-installs' to see known KSP installations", options.KSP); return Exit.BADOPT; } } else if (options.KSPdir != null) { // Set a KSP directory by its path manager.SetCurrentInstanceByPath(options.KSPdir); } else if (! (cmdline.action == "ksp" || cmdline.action == "version")) { // Find whatever our preferred instance is. // We don't do this on `ksp/version` commands, they don't need it. CKAN.KSP ksp = manager.GetPreferredInstance(); if (ksp == null) { user.RaiseMessage("I don't know where KSP is installed."); user.RaiseMessage("Use 'ckan ksp help' for assistance on setting this."); return Exit.ERROR; } else { log.InfoFormat("Using KSP install at {0}",ksp.GameDir()); } } #region Aliases switch (cmdline.action) { case "add": cmdline.action = "install"; break; case "uninstall": cmdline.action = "remove"; break; default: break; } #endregion switch (cmdline.action) { case "gui": return Gui(); case "version": return Version(user); case "update": return Update((UpdateOptions)options, RegistryManager.Instance(manager.CurrentInstance), manager.CurrentInstance, user); case "available": return Available(manager.CurrentInstance, user); case "install": Scan(manager.CurrentInstance); return Install((InstallOptions)cmdline.options, manager.CurrentInstance, user); case "scan": return Scan(manager.CurrentInstance); case "list": return List(user, manager.CurrentInstance); case "show": return Show((ShowOptions)cmdline.options, manager.CurrentInstance, user); case "remove": return Remove((RemoveOptions)cmdline.options, manager.CurrentInstance, user); case "upgrade": Scan(manager.CurrentInstance); var upgrade = new Upgrade(user); return upgrade.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); default: user.RaiseMessage("Unknown command, try --help"); return Exit.BADOPT; } }
/// <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, GameInstanceManager 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(); } }