コード例 #1
0
        public void ProgramArguments_Constructor_OverrideAppSetting()
        {
            using (ShimsContext.Create())
            {
                // Need to shim the AppSettings collection since previous tests may have wrote particular values to
                // the collection, which is statically shared across tests.
                var appSettings = new NameValueCollection {{"savetemplate", "false"}};
                ShimConfigurationManager.AppSettingsGet = () => appSettings;

                var args = new [] { "/savetemplate", "true" };

                var programArgs = new ProgramArguments(args);

                CollectionAssert.Contains(programArgs.ArgumentMap.Keys, "savetemplate");
                Assert.AreEqual(programArgs.ArgumentMap["savetemplate"], "true");
            }
        }
コード例 #2
0
        public void ProgramArguments_Constructor_BasicProcessingSuccess()
        {
            using (ShimsContext.Create())
            {
                // Need to shim the AppSettings collection since previous tests may have wrote particular values to
                // the collection, which is statically shared across tests.
                ShimConfigurationManager.AppSettingsGet = () => new NameValueCollection();

                var args = new [] {"/action", "someaction", "/savetemplate", "false"};

                var programArgs = new ProgramArguments(args);

                CollectionAssert.Contains(programArgs.ArgumentMap.Keys, "action");
                CollectionAssert.Contains(programArgs.ArgumentMap.Keys, "savetemplate");
                CollectionAssert.DoesNotContain(programArgs.ArgumentMap.Keys, "someaction");
                CollectionAssert.DoesNotContain(programArgs.ArgumentMap.Keys, "false");
                Assert.AreEqual(programArgs.ArgumentMap["action"], "someaction");
                Assert.AreEqual(programArgs.ArgumentMap["savetemplate"], "false");
            }
        }
コード例 #3
0
        /// <summary>
        /// Main entry point to the SktapCLI application.
        /// </summary>
        /// <param name="args">Command-line arguments that were specified.</param>
        /// <returns>0 if the program succeeded, a value other than 0 if it failed.</returns>
        public static int Main(string[] args)
        {
            const int resultError = -1;

            InitializeLogFile();

            _logger.LogInfo("Starting new run...");

            try
            {
                _logger.LogInfo("Application configuration parameters:\n\n{0}", SkytapApi.ConfigParams.ToString());

                var inputs = new ProgramArguments(args);
                var commands = new Dictionary<string, ICommand>();

                _logger.LogInfo("EXE command-line: {0}", inputs.ToString());

                /* Populate Command Dictionary */
                var types = from t in Assembly.GetExecutingAssembly().GetTypes()
                            where t.GetInterfaces().Contains(typeof(ICommand)) && t.GetConstructor(Type.EmptyTypes) != null
                            select t;

                foreach (var type in types)
                {
                    var commandInstance = Activator.CreateInstance(type) as ICommand;
                    Debug.Assert(commandInstance != null);

                    commands.Add(commandInstance.Name.ToLowerInvariant(), commandInstance);
                }

                /* Validate action is valid, also that we have a username and password */
                if (!HasNeededParams(inputs, commands))
                {
                    PrintUsage(commands.Values.ToArray());

                    _logger.LogError(Resources.Program_Main_ERROR_DidNotSpecifyAllProgramArguments, inputs.ToString());

                    return resultError;
                }

                /* Validate the action requested has the arguments it needs */
                ICommand command = commands[inputs.ArgumentMap["action"]];

                if (!command.ValidateArgs(inputs.ArgumentMap))
                {
                    PrintUsage(new[] { command });

                    _logger.LogError(Resources.Program_Main_ERROR_InvalidCommandLineArgs, inputs.ToString());

                    return resultError;
                }

                /* All looks good, lets run it */
                return command.Invoke(inputs.ArgumentMap);
            }
            catch (Exception e)
            {
                _logger.LogError("Exception thrown: {0}\n", e.Message);
                _logger.LogError("Stack Trace: {0}\n", e.StackTrace);
                _logger.LogError("Arguments: {0}\n", String.Join(" ", args));

                return resultError;
            }
            finally
            {
                ShutdownLogFile();
            }
        }
コード例 #4
0
 private static bool HasNeededParams(ProgramArguments inputs, Dictionary<String, ICommand> commands)
 {
     return (inputs.ArgumentMap.ContainsKey("action") && inputs.ArgumentMap.ContainsKey("username") &&
             inputs.ArgumentMap.ContainsKey("password") && commands.Keys.Contains(inputs.ArgumentMap["action"].ToLowerInvariant()) &&
             !String.IsNullOrEmpty(inputs.ArgumentMap["username"]) && !String.IsNullOrEmpty(inputs.ArgumentMap["password"]));
 }