11using System . Diagnostics ;
22using UniGetUI . Core . Data ;
33using UniGetUI . Core . Tools ;
4+ using YamlDotNet . Core . Tokens ;
45using YamlDotNet . Serialization ;
56
67namespace UniGetUI . Core . SettingsEngine . SecureSettings ;
78
89public static class SecureSettings
910{
1011 // Various predefined secure settings keys
11- public const string ALLOW_CLI_ARGUMENTS = "AllowCLIArguments" ;
12- public const string ALLOW_IMPORTING_CLI_ARGUMENTS = "AllowImportingCLIArguments" ;
13- public const string ALLOW_PREPOST_OPERATIONS = "AllowPrePostInstallCommands" ;
14- public const string ALLOW_IMPORT_PREPOST_OPERATIONS = "AllowImportingPrePostInstallCommands" ;
15- public const string FORCE_USER_GSUDO = "ForceUserGSudo" ;
12+ public enum K
13+ {
14+ AllowCLIArguments ,
15+ AllowImportingCLIArguments ,
16+ AllowPrePostOpCommand ,
17+ AllowImportPrePostOpCommands ,
18+ ForceUserGSudo ,
19+ Unset
20+ } ;
21+
22+ public static string ResolveKey ( K key )
23+ {
24+ return key switch
25+ {
26+ K . AllowCLIArguments => "AllowCLIArguments" ,
27+ K . AllowImportingCLIArguments => "AllowImportingCLIArguments" ,
28+ K . AllowPrePostOpCommand => "AllowPrePostInstallCommands" ,
29+ K . AllowImportPrePostOpCommands => "AllowImportingPrePostInstallCommands" ,
30+ K . ForceUserGSudo => "ForceUserGSudo" ,
31+
32+ K . Unset => throw new InvalidDataException ( "SecureSettings key was unset!" ) ,
33+ _ => throw new KeyNotFoundException ( $ "The SecureSettings key { key } was not found on the ResolveKey map")
34+ } ;
35+ }
1636
1737
1838 private static readonly Dictionary < string , bool > _cache = new ( ) ;
@@ -23,9 +43,9 @@ public static class Args
2343 public const string DISABLE_FOR_USER = "--disable-secure-setting-for-user" ;
2444 }
2545
26- public static bool Get ( string setting )
46+ public static bool Get ( K key )
2747 {
28- string purifiedSetting = CoreTools . MakeValidFileName ( setting ) ;
48+ string purifiedSetting = CoreTools . MakeValidFileName ( ResolveKey ( key ) ) ;
2949 if ( _cache . TryGetValue ( purifiedSetting , out var value ) )
3050 {
3151 return value ;
@@ -48,9 +68,9 @@ public static bool Get(string setting)
4868 return exists ;
4969 }
5070
51- public static async Task < bool > TrySet ( string setting , bool enabled )
71+ public static async Task < bool > TrySet ( K key , bool enabled )
5272 {
53- string purifiedSetting = CoreTools . MakeValidFileName ( setting ) ;
73+ string purifiedSetting = CoreTools . MakeValidFileName ( ResolveKey ( key ) ) ;
5474 _cache . Remove ( purifiedSetting ) ;
5575
5676 string purifiedUser = CoreTools . MakeValidFileName ( Environment . UserName ) ;
0 commit comments