예제 #1
0
        public ServiceState Execute(string command, int id, bool fullinformation = false)
        {
            ServiceOptions options = ServiceOptions.Parse(command, id, Options.FullInformation || fullinformation);

            if (options == null)
            {
                ServiceState state = new ServiceState(_executer, options, Caller);
                if (fullinformation)
                {
                    state.WriteLine("cant parse given command: " + command);
                }
                return(state);
            }
            else
            {
                return(_executer.Execute(options, Caller));
            }
        }
        public ServiceState Execute(ServiceOptions options, ICaller caller)
        {
            ServiceState state   = new ServiceState(this, options, caller);
            Service      service = Services.FindCreateByKey <Service>(options.Namespace, options.Name);

            if (state.RuntimeError = (service == null) && options.FullInformation)
            {
                _errorlog.Pass(this, "service/missing", s => string.Format(s, options));
            }
            else if (Execute(service, options, state) && state.Options.FullInformation)
            {
                _errorlog.Pass(this, "service/success", s => string.Format(s, service));
            }
            else if (state.Options.FullInformation)
            {
                _errorlog.Pass(this, "service/failed", s => string.Format(s, service));
            }
            return(state);
        }
        public static ServiceOptions Parse(string input, int id, bool fullinformation)
        {
            StringParser parser = new StringParser(input);

            parser.SkipIf(_space);
            string fullname = parser.ReadUntil(_space, _mark).ToLower();

            string[]       splittedName = fullname.Split(new[] { _splitter }, StringSplitOptions.RemoveEmptyEntries);
            ServiceOptions options;

            if (splittedName.Length > 2)
            {
                return(null); // throw new NotSupportedException($"{nameof(Parse)} - Das Format wird nicht unterstützt.");
            }
            else if (splittedName.Length > 1)
            {
                options = new ServiceOptions(input, splittedName[0], splittedName[1], id, fullinformation);
            }
            else if (splittedName.Length > 0)
            {
                options = new ServiceOptions(input, string.Empty, splittedName[0], id, fullinformation);
            }
            else
            {
                return(null); //throw new NotImplementedException($"{nameof(Parse)} - kein Name.");
            }
            while (!parser.End)
            {
                parser.SkipUntil(_mark);
                if (parser.Peek(1) == _mark)
                {
                    string argname  = parser.SkipIf(_mark).SkipIf(_space).ReadUntil(_equal, _mark).ToLower();
                    string argparam = string.Empty;
                    if (parser.Peek() == _equal)
                    {
                        argparam = parser.Skip(_equal, 1).ReadUntilNext(_mark).ToLower();
                        if (parser.Peek() != _space)
                        {
                            argparam += parser.ReadUntil(_mark).ToLower();
                        }
                    }
                    options.AddArgs(argname, argparam, true);
                }
                else
                {
                    bool   isschortcut = true;
                    string argname     = parser.SkipIf(_mark).SkipIf(_space).ReadUntilNext(_space, _mark).ToLower();
                    string argparam    = parser.SkipIf(_space).ReadUntilNext(_mark).ToLower();
                    if (parser.Peek() != _space)
                    {
                        argparam += parser.ReadUntil(_mark);
                    }
                    if (argname.Length == 0)
                    {
                        StringParser parser2 = new StringParser(argparam);
                        argname  = parser2.SkipIf(_space).ReadUntilAndSkip(_space);
                        argparam = parser2.ReadToEnd();
                    }
                    else if (argname.Contains(_equal.ToString()))
                    {
                        StringParser parser2 = new StringParser(argname);
                        argname     = parser2.SkipIf(_space).ReadUntilAndSkip(_equal);
                        argparam    = parser2.ReadToEnd() + argparam;
                        isschortcut = false;
                    }
                    options.AddArgs(argname, argparam, !isschortcut);
                }
            }
            return(options);
        }
예제 #4
0
 public ServiceState(ServiceInterpreter executer, ServiceOptions options, ICaller caller) : base(caller.Output)
 {
     Options   = options;
     _executer = executer;
     Caller    = caller;
 }
        private bool Execute(Service service, ServiceOptions options, ServiceState state)
        {
            if (state.Options.FullInformation)
            {
                _errorlog.Pass(this, "service/name", s => string.Format(s, options.Command));
                _errorlog.Pass(this, "service/name", s => string.Format(s, options));
            }
            try
            {
                service.Command.PreExecute.Invoke(state);
            }
            catch (Exception exception)
            {
                state.RuntimeError = true;
                if (state.Error && options.FullInformation)
                {
                    _errorlog.Pass(this, "service/preexecute/failed", s => string.Format(s, service));
                }
                state.WriteLine(exception.Message);
            }
            List <string> requiered     = new List <string>(service.Command.GetRquieredArguments());
            List <string> listfoundargs = new List <string>();
            Action        argexecutes   = null;

            foreach (ArgumentInfo arg in options.Args)
            {
                if (state.Options.FullInformation)
                {
                    _errorlog.Pass(this, "argument/parameter", s => string.Format(s, arg.Key, arg.Value));
                }
                Argument <ServiceState> argument = null;
                if (arg.IsFullname)
                {
                    argument = service.Command.Find(arg.Key);
                }
                else if (arg.Key.Length > 1)
                {
                    _errorlog.Pass(this, "argument/shortcut/notvalid", s => string.Format(s, arg.Key));
                }
                else
                {
                    argument = service.Command.Find(arg.Key[0]);
                }
                if (argument == null)
                {
                    state.ParseError = true;
                    _errorlog.Pass(this, "service/argument/missing", s => string.Format(s, arg.Key, service));
                }
                else if (listfoundargs.Contains(arg.Key))
                {
                    state.RuntimeError = true;
                    _errorlog.Pass(this, "argument/duplicate", s => string.Format(s, arg.Key));
                }
                else if (string.IsNullOrWhiteSpace(arg.Value) && !argument.Empty)
                {
                    state.RuntimeError = true;
                    _errorlog.Pass(this, "argument/parameter/missing", s => string.Format(s, arg.Key));
                }
                else if (argument.Empty && !string.IsNullOrWhiteSpace(arg.Value))
                {
                    state.RuntimeError = true;
                    _errorlog.Pass(this, "argument/parameter/empty", s => string.Format(s, arg.Key));
                }
                else
                {
                    listfoundargs.Add(arg.Key);
                    argexecutes += () => argument.Action(state, arg.Value);
                }
                if (argument != null && argument.Requiered)
                {
                    requiered.Remove(argument.ToString());
                }
            }
            if (requiered.Count > 0)
            {
                foreach (string arg in requiered)
                {
                    _errorlog.Pass(this, "argument/requiered/missing", s => string.Format(s, arg));
                }
                state.RuntimeError = true;
            }
            else if (state.Success)
            {
                try
                {
                    argexecutes?.Invoke();
                    if (state.Options.FullInformation)
                    {
                        _errorlog.Pass(this, "service/executing", s => string.Format(s, service.ToString()));
                    }
                    if (state.Success)
                    {
                        service.Command.PostExecute.Invoke(state);
                    }
                }
                catch (Exception exception)
                {
                    state.RuntimeError = true;
                    state.WriteLine(exception.Message);
                }
            }
            return(state.Success);
        }