private static async Task GetSchemasAsync()

in CommandLine/XblConfig/Program.cs [226:290]


        private static async Task<int> GetSchemasAsync(GetSchemasOptions options)
        {
            if (string.IsNullOrEmpty(options.Type))
            {
                // Get the schema types.
                Console.WriteLine("Obtaining document schema types.");
                Console.WriteLine();

                ConfigResponse<IEnumerable<string>> schemaTypes = await ConfigurationManager.GetSchemaTypesAsync();
                Console.WriteLine(ObjectPrinter.Print(schemaTypes.Result));
                if (!string.IsNullOrEmpty(options.Destination))
                {
                    foreach (string schemaType in schemaTypes.Result)
                    {
                        EnsureDirectory(options.Destination);
                        ConfigResponse<IEnumerable<SchemaVersion>> versions = await ConfigurationManager.GetSchemaVersionsAsync(schemaType);
                        foreach (SchemaVersion version in versions.Result)
                        {
                            ConfigResponse<Stream> schema = await ConfigurationManager.GetSchemaAsync(schemaType, version.Version);
                            string path = Path.Combine(options.Destination, $"{schemaType.ToLowerInvariant()}_{version.Version}.xsd");
                            using (FileStream fileStream = File.Create(path))
                            {
                                await schema.Result.CopyToAsync(fileStream);
                            }
                        }
                    }
                }
            }
            else if (options.Version <= 0)
            {
                // Get the schema versions.
                Console.WriteLine($"Obtaining document schema versions for type {options.Type}.");
                Console.WriteLine();

                ConfigResponse<IEnumerable<SchemaVersion>> schemaVersions = await ConfigurationManager.GetSchemaVersionsAsync(options.Type);
                Console.WriteLine(ObjectPrinter.Print(schemaVersions.Result));
            }
            else
            {
                Console.WriteLine($"Obtaining document schema {options.Type} for version {options.Version}.");
                Console.WriteLine();

                ConfigResponse<Stream> schema = await ConfigurationManager.GetSchemaAsync(options.Type, options.Version);

                if (string.IsNullOrEmpty(options.Destination))
                {
                    // The destination wasn't specified. Output the schema to stdout.
                    await schema.Result.CopyToAsync(Console.OpenStandardOutput());
                }
                else
                {
                    // The destination exists. Save the file to the directory.
                    EnsureDirectory(options.Destination);
                    string path = Path.Combine(options.Destination, $"{options.Type.ToLowerInvariant()}_{options.Version}.xsd");
                    using (FileStream fileStream = File.Create(path))
                    {
                        await schema.Result.CopyToAsync(fileStream);
                    }

                    Console.WriteLine($"Schema saved as {path}");
                }
            }

            return 0;
        }