Exemple #1
0
        /// <summary>Config command
        /// </summary>
        public static CommandLineApplication ConfigCommand(this CommandLineApplication app, IServiceProvider serviceProvider, IConfiguration configuration)
        {
            var console = serviceProvider.GetService <IConsole>();

            app.Command("config", commandLine =>
            {
                commandLine.ResponseFileHandling = ResponseFileHandling.ParseArgsAsLineSeparated;
                commandLine.Description          = "Config some global configuration,such as 'ak','sk'...";
                //set config value
                //s3-cli config info
                //commandLine.Command("info", configCommand =>
                //{
                //    configCommand.OnExecute(() =>
                //    {
                //        Console.WriteLine("--- s3-cli info ---");
                //        Console.WriteLine("Vendor:{0}", configuration.GetSection("S3CommandLineOption")["Vendor"]);
                //        Console.WriteLine("AccessKeyId:{0}", configuration.GetSection("S3CommandLineOption")["AccessKeyId"]);
                //        Console.WriteLine("SecretAccessKey:{0}", configuration.GetSection("S3CommandLineOption")["SecretAccessKey"]);
                //        Console.WriteLine("ServerUrl:{0}", configuration.GetSection("S3CommandLineOption")["ServerUrl"]);
                //        Console.WriteLine("DefaultBucket:{0}", configuration.GetSection("S3CommandLineOption")["DefaultBucket"]);
                //        Console.WriteLine("ForcePathStyle:{0}", configuration.GetSection("S3CommandLineOption")["ForcePathStyle"]);
                //        Console.WriteLine("SignatureVersion:{0}", configuration.GetSection("S3CommandLineOption")["SignatureVersion"]);
                //        Console.WriteLine("TemporaryPath:{0}", configuration.GetSection("S3CommandLineOption")["TemporaryPath"]);
                //    });
                //});

                //s3-cli config set -ak 123 -sk 232323 -s http://192.168.0.3 -f true -sv 2.0
                commandLine.Command("set", configCommand =>
                {
                    var vendorOption          = configCommand.Option("-v|--vendor", "Set vendor(Amazon|KS3)", CommandOptionType.SingleOrNoValue);
                    var accessKeyIdOption     = configCommand.Option("-ak|--accesskey_id", "Set AccessKeyId", CommandOptionType.SingleOrNoValue);
                    var secretAccessKeyOption = configCommand.Option("-sk|--secret_accesskey", "Set SecretAccessKey", CommandOptionType.SingleOrNoValue);
                    var serverUrlOption       = configCommand.Option("-s|--server_url", "Set ServerUrl", CommandOptionType.SingleOrNoValue);
                    var defaultBucketOption   = configCommand.Option("-b|--bucket", "Set DefaultBucket", CommandOptionType.SingleOrNoValue);
                    var forcePathStyleOption  = configCommand.Option <bool>("-f|--force_path", "Set ForcePathStyle", CommandOptionType.SingleOrNoValue);

                    var useChunkEncodingOption = configCommand.Option <bool>("-u|--use_chunk_encoding", "Set UseChunkEncoding", CommandOptionType.SingleOrNoValue);

                    var signatureVersionOption = configCommand.Option("-sv|--sign_version", "Set SignatureVersion(2.0)", CommandOptionType.SingleOrNoValue);
                    var temporaryPathOption    = configCommand.Option("-t|--temporary", "Set TemporaryPath", CommandOptionType.SingleOrNoValue);

                    configCommand.OnExecute(() =>
                    {
                        //读取配置信息

                        var filePath            = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
                        var s3CommandLineOption = new S3CommandLineOption()
                        {
                            Vendor           = vendorOption.HasValue() ? vendorOption.Value() : configuration.GetSection("S3CommandLineOption")["Vendor"],
                            AccessKeyId      = accessKeyIdOption.HasValue() ? accessKeyIdOption.Value() : configuration.GetSection("S3CommandLineOption")["AccessKeyId"],
                            SecretAccessKey  = secretAccessKeyOption.HasValue() ? secretAccessKeyOption.Value() : configuration.GetSection("S3CommandLineOption")["SecretAccessKey"],
                            ServerUrl        = serverUrlOption.HasValue() ? serverUrlOption.Value() : configuration.GetSection("S3CommandLineOption")["ServerUrl"],
                            DefaultBucket    = defaultBucketOption.HasValue() ? defaultBucketOption.Value() : configuration.GetSection("S3CommandLineOption")["DefaultBucket"],
                            ForcePathStyle   = forcePathStyleOption.HasValue() ? forcePathStyleOption.ParsedValue : true,
                            UseChunkEncoding = useChunkEncodingOption.HasValue() ? useChunkEncodingOption.ParsedValue : false,
                            SignatureVersion = signatureVersionOption.HasValue() ? signatureVersionOption.Value() : "",
                            TemporaryPath    = temporaryPathOption.HasValue() ? temporaryPathOption.Value() : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data")
                        };

                        var serializeOption = new SerializeOption()
                        {
                            S3CommandLineOption = s3CommandLineOption
                        };

                        var json = JsonSerializer.Serialize(serializeOption);
                        File.WriteAllBytes(filePath, Encoding.UTF8.GetBytes(json));

                        console.WriteLine("update appsetting!");
                    });
                });
            });
            return(app);
        }
Exemple #2
0
 public S3CommandLineService(IOptions <S3CommandLineOption> options, IS3ClientFactory s3ClientFactory, IConsole console)
 {
     _option          = options.Value;
     _s3ClientFactory = s3ClientFactory;
     _console         = console;
 }