Skip to content

Commit

Permalink
feat: add configuration service and its model class
Browse files Browse the repository at this point in the history
  • Loading branch information
alperensert committed Apr 7, 2024
1 parent a19699b commit 593d298
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/CloudflareDnsync.Abstractions/IDnsyncConfigService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace CloudflareDnsync.Abstractions;

public interface IDnsyncConfigService
{

}
43 changes: 43 additions & 0 deletions src/CloudflareDnsync.Models/DnsyConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;

namespace CloudflareDnsync.Models;

public sealed class DnsyConfiguration
{
[JsonProperty("name")]
public string Name { get; set; } = null!;

[JsonProperty("api_key")]
public string? ApiKey { get; set; }

[JsonProperty("api_email")]
public string? ApiEmail { get; set; }

[JsonProperty("token")]
public string? Token { get; set; }

[JsonProperty("record_id")]
public string Id { get; set; } = null!;

[JsonProperty("zone_id")]
public string ZoneId { get; set; } = null!;

[JsonProperty("proxy")]
public bool UseProxy { get; set; }

[JsonProperty("record_type")]
public RecordType Type { get; set; }

[JsonIgnore]
[MemberNotNullWhen(false, nameof(ApiKey))]
[MemberNotNullWhen(false, nameof(ApiEmail))]
[MemberNotNullWhen(true, nameof(Token))]
public bool IsUsingToken => !string.IsNullOrWhiteSpace(Token);

public enum RecordType
{
A,
AAAA
}
}
32 changes: 32 additions & 0 deletions src/CloudflareDnsync.Services/DnsyncConfigService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using CloudflareDnsync.Abstractions;
using CloudflareDnsync.Models;
using Newtonsoft.Json;

namespace CloudflareDnsync.Services;

public sealed class DnsyncConfigService : IDnsyncConfigService
{
private const string ConfigurationFile = ".dnsync.json";

private readonly string _configPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
ConfigurationFile);

public DnsyncConfigService()
{
if (!IsConfigExists)
{
var configCreationTask = CreateConfigAsync();
configCreationTask.Wait();
}
}

private Task CreateConfigAsync()
{
List<DnsyConfiguration> configurations = [];
var json = JsonConvert.SerializeObject(configurations, Formatting.Indented);
return File.WriteAllTextAsync(_configPath, json);
}

private bool IsConfigExists => File.Exists(_configPath);
}

0 comments on commit 593d298

Please sign in to comment.