Example #1
0
        public async Task HandleAsync(ICliContext context, CommandPipelineHandlerDelegate next, CancellationToken cancellationToken)
        {
            //Get current CLI mode and input directives
            Type currentModeType = _applicationLifetime.CurrentModeType !;
            IReadOnlyList <DirectiveInput> directives = context.Input.Directives;

            //Initialize collections
            List <IDirective>          directivesInstances          = new List <IDirective>();
            List <IPipelinedDirective> pipelinedDirectivesInstances = new List <IPipelinedDirective>();

            //Process directive input
            foreach (DirectiveInput directiveInput in directives)
            {
                // Try to get the directive matching the input or fallback to default
                DirectiveSchema directive = context.RootSchema.TryFindDirective(directiveInput.Name) ?? throw ArgumentBindingExceptions.UnknownDirectiveName(directiveInput);

                // Handle interactive directives not supported in current mode
                if (!directive.CanBeExecutedInMode(currentModeType))
                {
                    throw ModeEndUserExceptions.DirectiveExecutedInInvalidMode(directive, currentModeType);
                }

                // Get directive instance
                IDirective instance = (IDirective)_serviceProvider.GetRequiredService(directive.Type);

                //Initialize directive
                await instance.OnInitializedAsync(cancellationToken);

                //Add directive to list
                directivesInstances.Add(instance);

                if (directive.IsPipelinedDirective && instance is IPipelinedDirective pd)
                {
                    pipelinedDirectivesInstances.Add(pd);
                }
            }

            //Set directives lists in context
            CliContext internalCliContext = (CliContext)context;

            internalCliContext.Directives          = directivesInstances;
            internalCliContext.PipelinedDirectives = pipelinedDirectivesInstances;

            await next();
        }