ImportPanel.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <div class="modal-card">
  3. <!-- Modal-Card Header Start -->
  4. <header class="modal-card-head">
  5. <div class="flex1">
  6. <h3 class="title is-4 has-text-weight-normal">Import From Docker CLI</h3>
  7. </div>
  8. </header>
  9. <!-- Modal-Card Header End -->
  10. <!-- Modal-Card Body Start -->
  11. <section class="modal-card-body">
  12. <b-field label="Command Line" :type="{ 'is-danger': parseError}" :message="errors">
  13. <b-input maxlength="800" type="textarea" class="import-area" v-model="dockerCliCommands"></b-input>
  14. </b-field>
  15. </section>
  16. <!-- Modal-Card Body End -->
  17. <!-- Modal-Card Footer Start-->
  18. <footer class="modal-card-foot is-flex is-align-items-center">
  19. <div class="flex1"></div>
  20. <div>
  21. <b-button label="Cancel" @click="$emit('close')" rounded />
  22. <b-button label="Sumbit" type="is-dark" @click="emitSubmit" rounded />
  23. </div>
  24. </footer>
  25. <!-- Modal-Card Footer End -->
  26. </div>
  27. </template>
  28. <script>
  29. import parser from 'yargs-parser'
  30. export default {
  31. data() {
  32. return {
  33. dockerCliCommands: "",
  34. parseError: false,
  35. errors: "",
  36. }
  37. },
  38. props: {
  39. initData: Object,
  40. netWorks: Array
  41. },
  42. created() {
  43. console.log(this.netWorks);
  44. },
  45. methods: {
  46. /**
  47. * @description: Emit Event to tell parent Update
  48. * @param {*}
  49. * @return {*} void
  50. */
  51. emitSubmit() {
  52. if (this.parseCli()) {
  53. this.errors = ""
  54. this.$emit('update', this.initData)
  55. this.$emit('close')
  56. } else {
  57. this.errors = "Please fill correct command line"
  58. this.parseError = true;
  59. }
  60. },
  61. /**
  62. * @description: Parse Import Docker Cli Commands
  63. * @return {Boolean}
  64. */
  65. parseCli() {
  66. const formattedInput = this.dockerCliCommands.replace(/\<[^\>]*\>/g, 'Custom_data').replace(/[\r\n]/g, "").replace(/\\/g, "\\ ").trim();
  67. const parsedInput = parser(formattedInput)
  68. console.log(parsedInput);
  69. const { _: command, ...params } = parsedInput;
  70. if (command[0] !== 'docker' || (command[1] !== 'run' && command[1] !== 'create')) {
  71. return false
  72. } else {
  73. //Envs
  74. this.initData.envs = this.makeArray(parsedInput.e).map(item => {
  75. let ii = item.split("=");
  76. return {
  77. container: ii[0],
  78. host: ii[1]
  79. }
  80. })
  81. //Ports
  82. this.initData.ports = this.makeArray(parsedInput.p).map(item => {
  83. let pArray = item.split(":")
  84. let endArray = pArray[1].split("/")
  85. let protocol = (endArray[1]) ? endArray[1] : 'tcp';
  86. return {
  87. container: endArray[0],
  88. host: pArray[0],
  89. protocol: protocol
  90. }
  91. })
  92. //Volume
  93. this.initData.volumes = this.makeArray(parsedInput.v).map(item => {
  94. let ii = item.split(":");
  95. return {
  96. container: ii[1],
  97. host: ii[0]
  98. }
  99. })
  100. // Devices
  101. this.initData.devices = this.makeArray(parsedInput.device).map(item => {
  102. let ii = item.split(":");
  103. return {
  104. container: ii[1],
  105. host: ii[0]
  106. }
  107. })
  108. //Network
  109. if (parsedInput.network != undefined) {
  110. let network = (parsedInput.network == 'physical') ? 'macvlan' : parsedInput.network;
  111. let seletNetworks = this.netWorks.filter(item => {
  112. if (item.driver == network) {
  113. return true
  114. }
  115. })
  116. if (seletNetworks.length > 0) {
  117. this.initData.network_model = seletNetworks[0].networks[0].id;
  118. }
  119. }
  120. //Image
  121. this.initData.image = [...command].pop()
  122. //Label
  123. if (parsedInput.name != undefined) {
  124. this.initData.label = parsedInput.name.replace(/^\S/, s => s.toUpperCase())
  125. }
  126. //Restart
  127. if (parsedInput.restart != undefined) {
  128. this.initData.restart = parsedInput.restart
  129. }
  130. return true
  131. }
  132. },
  133. /**
  134. * @description: Make String to Array
  135. * @param {*}
  136. * @return {Array}
  137. */
  138. makeArray(foo) {
  139. let newArray = (typeof (foo) == "string") ? [foo] : foo
  140. return (newArray == undefined) ? [] : newArray
  141. }
  142. },
  143. }
  144. </script>
  145. <style>
  146. </style>