2018-03-12 14:55:35 -07:00
|
|
|
using System.IO;
|
2018-03-31 07:02:29 +02:00
|
|
|
using System.Text;
|
2018-03-12 14:55:35 -07:00
|
|
|
using System.Threading.Tasks;
|
2018-03-31 07:02:29 +02:00
|
|
|
using YamlDotNet.Core;
|
|
|
|
|
using YamlDotNet.Core.Events;
|
2018-03-12 14:55:35 -07:00
|
|
|
using YamlDotNet.Serialization;
|
|
|
|
|
using YamlDotNet.Serialization.NamingConventions;
|
|
|
|
|
|
|
|
|
|
namespace k8s
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is a utility class that helps you load objects from YAML files.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
|
public class Yaml {
|
|
|
|
|
public static async Task<T> LoadFromStreamAsync<T>(Stream stream) {
|
|
|
|
|
var reader = new StreamReader(stream);
|
|
|
|
|
var content = await reader.ReadToEndAsync();
|
|
|
|
|
return LoadFromString<T>(content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task<T> LoadFromFileAsync<T> (string file) {
|
2018-03-31 07:02:29 +02:00
|
|
|
using (FileStream fs = File.OpenRead(file)) {
|
2018-03-12 14:55:35 -07:00
|
|
|
return await LoadFromStreamAsync<T>(fs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T LoadFromString<T>(string content) {
|
2018-03-31 07:02:29 +02:00
|
|
|
var deserializer =
|
2018-03-12 14:55:35 -07:00
|
|
|
new DeserializerBuilder()
|
|
|
|
|
.WithNamingConvention(new CamelCaseNamingConvention())
|
|
|
|
|
.Build();
|
|
|
|
|
var obj = deserializer.Deserialize<T>(content);
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
2018-03-31 07:02:29 +02:00
|
|
|
|
|
|
|
|
public static string SaveToString<T>(T value)
|
|
|
|
|
{
|
|
|
|
|
var stringBuilder = new StringBuilder();
|
|
|
|
|
var writer = new StringWriter(stringBuilder);
|
|
|
|
|
var emitter = new Emitter(writer);
|
|
|
|
|
|
|
|
|
|
var serializer =
|
|
|
|
|
new SerializerBuilder()
|
|
|
|
|
.WithNamingConvention(new CamelCaseNamingConvention())
|
|
|
|
|
.BuildValueSerializer();
|
|
|
|
|
emitter.Emit(new StreamStart());
|
|
|
|
|
emitter.Emit(new DocumentStart());
|
|
|
|
|
serializer.SerializeValue(emitter, value, typeof(T));
|
|
|
|
|
|
|
|
|
|
return stringBuilder.ToString();
|
|
|
|
|
}
|
2018-03-12 14:55:35 -07:00
|
|
|
}
|
|
|
|
|
}
|