Exemplo n.º 1
0
        /// <summary>
        ///     Check and eventually fix the command settings
        /// </summary>
        /// <param name="command"></param>
        /// <returns>false if the command is not correctly configured</returns>
        private bool IsCommandValid(string command)
        {
            if (!_externalCommandConfig.RunInbackground.ContainsKey(command))
            {
                Log.Warn().WriteLine("Found missing runInbackground for {0}", command);
                // Fix it
                _externalCommandConfig.RunInbackground.Add(command, true);
            }
            if (!_externalCommandConfig.Argument.ContainsKey(command))
            {
                Log.Warn().WriteLine("Found missing argument for {0}", command);
                // Fix it
                _externalCommandConfig.Argument.Add(command, "{0}");
            }
            if (!_externalCommandConfig.Commandline.ContainsKey(command))
            {
                Log.Warn().WriteLine("Found missing commandline for {0}", command);
                return(false);
            }
            var commandline = FilenameHelper.FillVariables(_externalCommandConfig.Commandline[command], true);

            commandline = FilenameHelper.FillCmdVariables(commandline, true);

            if (File.Exists(commandline))
            {
                return(true);
            }
            Log.Warn().WriteLine("Found 'invalid' commandline {0} for command {1}", _externalCommandConfig.Commandline[command], command);
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     The actual executing code for the external command
        /// </summary>
        /// <param name="commando"></param>
        /// <param name="fullPath"></param>
        /// <param name="verb"></param>
        /// <param name="output"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        private static int CallExternalCommand(string commando, string fullPath, string verb, out string output, out string error)
        {
            var commandline = Config.Commandline[commando];
            var arguments   = Config.Argument[commando];

            output = null;
            error  = null;
            if (!string.IsNullOrEmpty(commandline))
            {
                using (var process = new Process())
                {
                    // Fix variables
                    commandline = FilenameHelper.FillVariables(commandline, true);
                    commandline = FilenameHelper.FillCmdVariables(commandline);

                    arguments = FilenameHelper.FillVariables(arguments, false);
                    arguments = FilenameHelper.FillCmdVariables(arguments, false);

                    process.StartInfo.FileName        = FilenameHelper.FillCmdVariables(commandline);
                    process.StartInfo.Arguments       = FormatArguments(arguments, fullPath);
                    process.StartInfo.UseShellExecute = false;
                    if (Config.RedirectStandardOutput)
                    {
                        process.StartInfo.RedirectStandardOutput = true;
                    }
                    if (Config.RedirectStandardError)
                    {
                        process.StartInfo.RedirectStandardError = true;
                    }
                    if (verb != null)
                    {
                        process.StartInfo.Verb = verb;
                    }
                    Log.Info().WriteLine("Starting : {0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
                    process.Start();
                    process.WaitForExit();
                    if (Config.RedirectStandardOutput)
                    {
                        output = process.StandardOutput.ReadToEnd();
                        if (Config.ShowStandardOutputInLog && output.Trim().Length > 0)
                        {
                            Log.Info().WriteLine("Output:\n{0}", output);
                        }
                    }
                    if (Config.RedirectStandardError)
                    {
                        error = process.StandardError.ReadToEnd();
                        if (error.Trim().Length > 0)
                        {
                            Log.Warn().WriteLine("Error:\n{0}", error);
                        }
                    }
                    Log.Info().WriteLine("Finished : {0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
                    return(process.ExitCode);
                }
            }
            return(-1);
        }
Exemplo n.º 3
0
        private void OkButtonState()
        {
            // Assume OK
            buttonOk.Enabled              = true;
            textBox_name.BackColor        = Color.White;
            textBox_commandline.BackColor = Color.White;
            textBox_arguments.BackColor   = Color.White;
            // Is there a text in the name field
            if (string.IsNullOrEmpty(textBox_name.Text))
            {
                buttonOk.Enabled = false;
            }
            // Check if commandname is unique
            if (_commando == null && !string.IsNullOrEmpty(textBox_name.Text) && ExternalCommandConfig.Commands.Contains(textBox_name.Text))
            {
                buttonOk.Enabled       = false;
                textBox_name.BackColor = Color.Red;
            }
            // Is there a text in the commandline field
            if (string.IsNullOrEmpty(textBox_commandline.Text))
            {
                buttonOk.Enabled = false;
            }

            if (!string.IsNullOrEmpty(textBox_commandline.Text))
            {
                // Added this to be more flexible, using the Greenshot var format
                var cmdPath = FilenameHelper.FillVariables(textBox_commandline.Text, true);
                // And also replace the "DOS" Variables
                cmdPath = FilenameHelper.FillCmdVariables(cmdPath, true);
                // Is the command available?
                if (!File.Exists(cmdPath))
                {
                    buttonOk.Enabled = false;
                    textBox_commandline.BackColor = Color.Red;
                }
            }
            // Are the arguments in a valid format?
            try
            {
                var arguments = FilenameHelper.FillVariables(textBox_arguments.Text, false);
                arguments = FilenameHelper.FillCmdVariables(arguments, false);

                ExternalCommandDestination.FormatArguments(arguments, string.Empty);
            }
            catch
            {
                buttonOk.Enabled            = false;
                textBox_arguments.BackColor = Color.Red;
            }
        }