Exemplo n.º 1
0
        public int Execute(CommandLineApplication cmd)
        {
            bool   showKey = _showKey.HasValue();
            string keyFile = _keyFile.Value;

            StrongNamePublicKey publicKey;

            try
            {
                publicKey = _keyLoader.LoadPublicKey(keyFile);
            }
            catch (Exception error)
            {
                cmd.Error.WriteLine("ERROR: {0}", error.Message);
                return(ExitCodes.FromException(error));
            }

            if (showKey)
            {
                cmd.Out.WriteLine("Public key (hash algorithm {0}) is:");
                cmd.Out.WriteLine(publicKey.CreatePublicKey().ToHexString());
                cmd.Out.WriteLine();
            }

            cmd.Out.WriteLine("Public key token is:");
            cmd.Out.WriteLine(publicKey.CreatePublicKeyToken().ToHexString());

            return(ExitCodes.Success);
        }
Exemplo n.º 2
0
        public int Execute(CommandLineApplication cmd)
        {
            bool force = _force.HasValue();

            AssemblyHashAlgorithm hashAlgorithm = ParseAssemblyHashAlgorithm(_hashAlgorithm.ParsedValue);

            string keyFile       = _keyFile.Value;
            string publicKeyFile = _publicKeyFile.Value;

            try
            {
                if (File.Exists(publicKeyFile) && !force)
                {
                    throw new FileAlreadyExistsException(publicKeyFile);
                }

                StrongNameKey key = _keyLoader.LoadKey(keyFile);

                byte[] publicKey = key.WithHashAlgorithm(hashAlgorithm)
                                   .PublicKey;

                File.WriteAllBytes(publicKeyFile, publicKey);
            }
            catch (Exception error)
            {
                cmd.Error.WriteLine("ERROR: {0}", error.Message);
                return(ExitCodes.FromException(error));
            }

            cmd.Out.WriteLine("Public key written to '{0}'.", publicKeyFile);
            return(ExitCodes.Success);
        }
Exemplo n.º 3
0
        public int Execute(CommandLineApplication cmd)
        {
            bool   force   = _force.HasValue();
            int?   keySize = _keySize.HasValue() ? _keySize.ParsedValue : (int?)null;
            string keyFile = _keyFile.Value;

            try
            {
                if (File.Exists(keyFile) && !force)
                {
                    throw new FileAlreadyExistsException(keyFile);
                }

                StrongNameKey key = _keyGenerator.Generate(keySize);
                File.WriteAllBytes(keyFile, key.CreateStrongName());
            }
            catch (Exception error)
            {
                cmd.Error.WriteLine("ERROR: {0}", error.Message);
                return(ExitCodes.FromException(error));
            }

            cmd.Out.WriteLine("Key pair written to '{0}'.", keyFile);
            return(ExitCodes.Success);
        }
Exemplo n.º 4
0
        /// <inheritdoc/>
        public async Task <int> ExecuteCommandAsync(IEnumerable <string> commandLineArguments)
        {
            _logger.LogInformation("Executing command '{CommandLineArguments}'.", commandLineArguments);

            using (IServiceScope serviceScope = _serviceScopeFactory.CreateScope())
            {
                IServiceProvider provider     = serviceScope.ServiceProvider;
                ICliContext      cliContext   = provider.GetRequiredService <ICliContext>();
                Guid             cliContextId = cliContext.Id;

                _logger.LogDebug("New scope created with CliContext {CliContextId}.", cliContextId);

                CommandInput input = CommandInputResolver.Parse(commandLineArguments, _rootSchemaAccessor.RootSchema.GetCommandNames());
                ((CliContext)cliContext).Input = input;

                try
                {
                    // Execute middleware pipeline
                    await RunPipelineAsync(provider, cliContext);
                }
                catch (Exception ex)
                {
                    _logger.LogDebug("Exception occured. Trying to find exception handler.");

                    IEnumerable <ICliExceptionHandler> exceptionHandlers = provider.GetServices <ICliExceptionHandler>();
                    foreach (ICliExceptionHandler handler in exceptionHandlers)
                    {
                        if (handler.HandleException(ex))
                        {
                            _logger.LogDebug(ex, "Exception handled by {ExceptionHandlerType}.", handler.GetType().FullName);

                            return(ExitCodes.FromException(ex));
                        }
                    }

                    _logger.LogCritical(ex, "Unhandled exception during command execution.");

                    throw;
                }
                finally
                {
                    _logger.LogDebug("Disposed scope with CliContext {CliContextId}.", cliContextId);
                }

                return(cliContext.ExitCode ?? ExitCodes.Error);
            }
        }
Exemplo n.º 5
0
        public int Execute(CommandLineApplication cmd)
        {
            bool   delaySign       = _delaySign.HasValue();
            bool   force           = _force.HasValue();
            string keyFile         = _keyFile.Value;
            string assemblyFile    = _assemblyFile.Value;
            string outAssemblyFile = !string.IsNullOrEmpty(_outAssemblyFile.Value)
                ? _outAssemblyFile.Value
                : null;

            try
            {
                if (File.Exists(outAssemblyFile) && !force)
                {
                    throw new FileAlreadyExistsException(outAssemblyFile);
                }

                if (delaySign)
                {
                    StrongNamePublicKey publicKey = _keyLoader.LoadPublicKey(keyFile);
                    _signer.DelaySignAssembly(assemblyFile, publicKey, outAssemblyFile);
                }
                else
                {
                    StrongNameKey key = _keyLoader.LoadKey(keyFile);
                    _signer.SignAssembly(assemblyFile, key, outAssemblyFile);
                }
            }
            catch (Exception error)
            {
                cmd.Error.WriteLine("ERROR: {0}", error.Message);
                return(ExitCodes.FromException(error));
            }

            cmd.Out.WriteLine("Assembly '{0}' successfully signed.", outAssemblyFile);
            return(ExitCodes.Success);
        }