using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using k8s.Models;
namespace k8s
{
///
/// This is a utility class that helps you load objects from YAML files.
///
public static class Yaml
{
public class ByteArrayStringYamlConverter : IYamlTypeConverter
{
public bool Accepts(Type type)
{
return type == typeof(byte[]);
}
public object ReadYaml(IParser parser, Type type)
{
if (parser.Current is YamlDotNet.Core.Events.Scalar scalar)
{
try
{
if (string.IsNullOrEmpty(scalar.Value))
{
return null;
}
return Encoding.UTF8.GetBytes(scalar.Value);
}
finally
{
parser.MoveNext();
}
}
throw new InvalidOperationException(parser.Current?.ToString());
}
public void WriteYaml(IEmitter emitter, object value, Type type)
{
var obj = (byte[])value;
emitter.Emit(new YamlDotNet.Core.Events.Scalar(Encoding.UTF8.GetString(obj)));
}
}
///
/// Load a collection of objects from a stream asynchronously
///
///
/// The stream to load the objects from.
///
///
/// A map from / to Type. For example "v1/Pod" -> typeof(V1Pod)
///
public static async Task> LoadAllFromStreamAsync(Stream stream, Dictionary typeMap)
{
var reader = new StreamReader(stream);
var content = await reader.ReadToEndAsync().ConfigureAwait(false);
return LoadAllFromString(content, typeMap);
}
///
/// Load a collection of objects from a file asynchronously
///
///
/// The name of the file to load from.
///
///
/// A map from / to Type. For example "v1/Pod" -> typeof(V1Pod)
///
public static Task> LoadAllFromFileAsync(string fileName, Dictionary typeMap)
{
var reader = File.OpenRead(fileName);
return LoadAllFromStreamAsync(reader, typeMap);
}
///
/// Load a collection of objects from a string
///
///
/// The string to load the objects from.
///
///
/// A map from / to Type. For example "v1/Pod" -> typeof(V1Pod)
///
public static List