Configuration.vala 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Invercargill;
  2. using Invercargill.DataStructures;
  3. using Invercargill.Mapping;
  4. namespace Usm {
  5. /**
  6. * Errors raised while reading a {@link Configuration}.
  7. */
  8. public errordomain ConfigurationError {
  9. /**
  10. * The "system_package_manager" section defined only one of its required argv arrays.
  11. */
  12. INCOMPLETE_SYSTEM_PACKAGE_MANAGER
  13. }
  14. public class Configuration {
  15. public bool is_managed { get; set; }
  16. public ManagedConfiguration? managed_config { get; set; }
  17. public Paths? paths { get; set; }
  18. public SystemPackageManagerConfig? system_package_manager { get; set; }
  19. /**
  20. * Seconds a single repository transfer may run before it is
  21. * aborted; consumed by {@link CurlRepositoryClient} through
  22. * {@link Repository.get_client}.
  23. */
  24. public int download_timeout { get; set; default = 300; }
  25. public static PropertyMapper<Configuration> get_mapper() {
  26. return PropertyMapper.build_for<Configuration>(cfg => {
  27. cfg.map<bool>("is_managed", o => o.is_managed, (o, v) => o.is_managed = v);
  28. cfg.map<int>("download_timeout", o => o.download_timeout, (o, v) => o.download_timeout = v)
  29. .undefined_when(o => o.download_timeout == 0)
  30. .when_undefined(o => o.download_timeout = 300);
  31. cfg.map_properties_with<ManagedConfiguration>("managed", o => o.managed_config, (o, v) => o.managed_config = v, ManagedConfiguration.get_mapper())
  32. .undefined_when(o => o.managed_config == null)
  33. .when_undefined(o => o.managed_config = null);
  34. cfg.map_properties_with<Paths>("paths", o => o.paths, (o, v) => o.paths = v, Paths.get_mapper())
  35. .undefined_when(o => o.paths == null)
  36. .when_undefined(o => o.paths = new Paths.usm_environ());
  37. cfg.map_properties_with<SystemPackageManagerConfig>("system_package_manager", o => o.system_package_manager, (o, v) => o.system_package_manager = v, SystemPackageManagerConfig.get_mapper())
  38. .undefined_when(o => o.system_package_manager == null)
  39. .when_undefined(o => o.system_package_manager = null);
  40. cfg.set_constructor(() => new Configuration());
  41. });
  42. }
  43. public Configuration.from_paths(Paths paths) throws Error {
  44. var element = new InvercargillJson.JsonElement.from_file(Path.build_filename(paths.usm_config_dir, "usm.config"));
  45. get_mapper().map_into(this, element.as<Invercargill.Properties>());
  46. validate();
  47. }
  48. private void validate() throws Error {
  49. if(system_package_manager == null) {
  50. return;
  51. }
  52. var query_defined = system_package_manager.query != null;
  53. var install_defined = system_package_manager.install != null;
  54. if(query_defined != install_defined) {
  55. throw new ConfigurationError.INCOMPLETE_SYSTEM_PACKAGE_MANAGER(
  56. @"The \"system_package_manager\" section of usm.config must define both \"query\" and \"install\" argv arrays, but only \"$(query_defined ? "query" : "install")\" was found."
  57. );
  58. }
  59. }
  60. public static bool check_managed(Paths paths) {
  61. var config_path = Path.build_filename(paths.usm_config_dir, "usm.config");
  62. if(!File.new_for_path(config_path).query_exists()) {
  63. return false;
  64. }
  65. try {
  66. return new Configuration.from_paths(paths).is_managed;
  67. }
  68. catch(Error e) {
  69. warning(@"Error reading configuration: $(e.message)");
  70. return false;
  71. }
  72. }
  73. }
  74. public class ManagedConfiguration {
  75. public string state_path { get; set; }
  76. public static PropertyMapper<ManagedConfiguration> get_mapper() {
  77. return PropertyMapper.build_for<ManagedConfiguration>(cfg => {
  78. cfg.map<string>("state_path", o => o.state_path, (o, v) => o.state_path = v);
  79. cfg.set_constructor(() => new ManagedConfiguration());
  80. });
  81. }
  82. }
  83. /**
  84. * argv arrays used to invoke the configured system package manager helper.
  85. *
  86. * Values are plain argv arrays (no shell parsing); see {@link SystemPackageManager}
  87. * for the helper contracts. Both {@link query} and {@link install} must be defined
  88. * together or omitted entirely, which {@link Configuration} validates on load.
  89. */
  90. public class SystemPackageManagerConfig {
  91. public Vector<string>? query { get; set; }
  92. public Vector<string>? install { get; set; }
  93. public static PropertyMapper<SystemPackageManagerConfig> get_mapper() {
  94. return PropertyMapper.build_for<SystemPackageManagerConfig>(cfg => {
  95. cfg.map_many<string>("query", o => o.query ?? new Vector<string>(), (o, v) => o.query = v.to_vector())
  96. .undefined_when(o => o.query == null)
  97. .when_undefined(o => o.query = null);
  98. cfg.map_many<string>("install", o => o.install ?? new Vector<string>(), (o, v) => o.install = v.to_vector())
  99. .undefined_when(o => o.install == null)
  100. .when_undefined(o => o.install = null);
  101. cfg.set_constructor(() => new SystemPackageManagerConfig());
  102. });
  103. }
  104. }
  105. }