Skip to content

Commit

Permalink
Interpreting $TOKEN's in port parsing with .env file values
Browse files Browse the repository at this point in the history
  • Loading branch information
syko9000 committed Dec 5, 2023
1 parent bec5460 commit 7591a7c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions frontend/src/components/Container.vue
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,46 @@ export default defineComponent({
},
methods: {
parsePort(port) {
port = this.interpretField(port);
let hostname = this.$root.info.primaryHostname || location.hostname;
return parseDockerPort(port, hostname);
},
remove() {
delete this.jsonObject.services[this.name];
},
getEnvFileValue(key, defaultValue) {
let pattern = RegExp(`^${key}\\s*[=:]\\s*(?<value>.*(?=\\s#)|.*$)`, "mgi");
let match = pattern.exec(this.$parent.$parent.stack.composeENV);
let value = defaultValue;
if (match) {
value = match[1].trim(); // remove outer whitespace
// If wrapped in quotes, remove and unescape matching quotes
match = /^(['"])(.*(?<!\\))\1$/.exec(value);
if (match) {
value = match[2].replace("\\" + match[1], match[1]);
}
}
return value;
},
interpretField(value) {
let pattern = /(?<!\$)\$(?:\{(?<fullname>.*?)\}|(?<name>\w+))/g;
let match;
while ((match = pattern.exec(value)) !== null) {
pattern.lastIndex = 0;
let name = match.groups.name;
let defaultValue = "";
if (!name) {
name = match.groups.fullname.match(/^\w+/)[0];
let defaultValueMatch = match.groups.fullname.match(/-(.*)$/);
if (defaultValueMatch) {
defaultValue = defaultValueMatch[1];
}
}
let envValue = this.getEnvFileValue(name, defaultValue);
value = value.substring(0, match.index) + envValue + value.substring(match.index + match[0].length);
}
return value;
}
}
});
</script>
Expand Down

0 comments on commit 7591a7c

Please sign in to comment.