40 lines
1.3 KiB
C#
40 lines
1.3 KiB
C#
using System.Text.Json;
|
|
|
|
namespace NozCli.Core.Config;
|
|
|
|
public class LocalConfig
|
|
{
|
|
public string ServerUrl { get; set; } = string.Empty;
|
|
public string Token { get; set; } = string.Empty;
|
|
|
|
// ~/.config/noz/config.json
|
|
private static readonly string ConfigPath = Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
|
|
".config", "noz", "config.json"
|
|
);
|
|
|
|
public static LocalConfig? Load()
|
|
{
|
|
if (!File.Exists(ConfigPath)) return null;
|
|
var json = File.ReadAllText(ConfigPath);
|
|
return JsonSerializer.Deserialize<LocalConfig>(json, JsonOptions);
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
Directory.CreateDirectory(Path.GetDirectoryName(ConfigPath)!);
|
|
// 0600 — only owner can read
|
|
var json = JsonSerializer.Serialize(this, JsonOptions);
|
|
File.WriteAllText(ConfigPath, json);
|
|
if (!OperatingSystem.IsWindows())
|
|
File.SetUnixFileMode(ConfigPath, UnixFileMode.UserRead | UnixFileMode.UserWrite);
|
|
}
|
|
|
|
public static bool Exists() => File.Exists(ConfigPath);
|
|
|
|
private static readonly JsonSerializerOptions JsonOptions = new()
|
|
{
|
|
WriteIndented = true,
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
|
|
};
|
|
}
|