provision 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env ucode
  2. // SPDX-License-Identifier: GPL-2.0+
  3. /*
  4. * Copyright (C) 2025 Felix Fietkau <[email protected]>
  5. */
  6. 'use strict';
  7. import { basename } from "fs";
  8. import * as provision from "provision";
  9. const usage_message = `Usage: ${basename(sourcepath())} <command> [<args>]
  10. Commands:
  11. - get [<path>]: Get string value at <path> (or all if no path given)
  12. - set <path> <value> Set string value at <path> to <value>
  13. - get_json [<path>]: Get JSON data at <path> (or all if no path given)
  14. - set_json <path> <value> Set JSON value at <path> to <value>
  15. - delete <path> Delete value at <path>
  16. - reset Clear provision data
  17. - create Create provisioning partition
  18. - destroy Destroy provisioning partition
  19. `;
  20. function usage()
  21. {
  22. warn(usage_message);
  23. exit(1);
  24. }
  25. if (!length(ARGV))
  26. usage();
  27. const create_error_msg = `Provisioning partition could not be created.
  28. This is only supported on devices with UBI for now.
  29. If there was not enough space, please reflash using the sysugrade -P parameter
  30. `;
  31. let ctx;
  32. if (ARGV[0] == "create") {
  33. ctx = provision.create();
  34. if (!ctx) {
  35. warn(create_error_msg);
  36. exit(1);
  37. }
  38. ctx.reset();
  39. ctx.commit();
  40. exit(0);
  41. } else {
  42. ctx = provision.open();
  43. if (!ctx) {
  44. warn(`Provisioning partition not found. Try ${basename(sourcepath())} enable\n`);
  45. exit(1);
  46. }
  47. }
  48. ctx.init();
  49. let cmd = shift(ARGV);
  50. switch (cmd) {
  51. case "get":
  52. let val = ctx.get(ARGV[0]);
  53. val ??= "";
  54. print(val + "\n");
  55. break;
  56. case "get_json":
  57. printf("%.J\n", ctx.get(ARGV[0]));
  58. break;
  59. case "set_json":
  60. if (length(ARGV) != 2)
  61. usage();
  62. ARGV[1] = json(ARGV[1]);
  63. if (ARGV[1] == null) {
  64. warn('Invalid JSON argument\n');
  65. exit(1);
  66. }
  67. // fallthrough
  68. case "set":
  69. if (length(ARGV) != 2)
  70. usage();
  71. if (!ctx.set(ARGV[0], ARGV[1])) {
  72. warn('Set failed\n');
  73. exit(1);
  74. }
  75. ctx.commit();
  76. break;
  77. case "delete":
  78. if (length(ARGV) != 1)
  79. usage();
  80. if (!ctx.set(ARGV[0])) {
  81. warn('Delete failed\n');
  82. exit(1);
  83. }
  84. ctx.commit();
  85. break;
  86. case "reset":
  87. ctx.reset();
  88. ctx.commit();
  89. break;
  90. case "destroy":
  91. ctx.destroy();
  92. break;
  93. default:
  94. usage();
  95. }