-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add configuration service and its model class
- Loading branch information
1 parent
a19699b
commit 593d298
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace CloudflareDnsync.Abstractions; | ||
|
||
public interface IDnsyncConfigService | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |