Skip to content

extract more possible Docker env keys #1349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.CachedValue;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiModificationTracker;
import com.intellij.psi.util.*;
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -158,21 +155,33 @@ private static void visitEnvironment(@NotNull Project project, @NotNull Consumer

for (String file : DOCKER_FILES) {
for (PsiFile psiFile : FilenameIndex.getFilesByName(project, file, GlobalSearchScope.allScope(project))) {
// ENV DOCKERFILE_FOO /bar
Matcher matcher = Pattern.compile("ENV\\s+([^\\s]*)\\s+").matcher(psiFile.getText());
while(matcher.find()){
consumer.accept(Pair.create(matcher.group(1), psiFile));
}

// ENV ADMIN_USER_DOCKERFILE="mark"
// ENV ADMIN_USER_DOCKERFILE ="mark"
matcher = Pattern.compile("ENV\\s+([\\w+]*)\\s*=").matcher(psiFile.getText());
while(matcher.find()){
consumer.accept(Pair.create(matcher.group(1), psiFile));
}
}
}
}

/**
* environment:
* - FOOBAR=0
*
* environment:
* FOOBAR: 0
*/
private static void visitEnvironmentSquenceItems(@NotNull Consumer<Pair<String, PsiElement>> consumer, @NotNull YAMLKeyValue yamlKeyValue) {
YAMLKeyValue environment = YamlHelper.getYamlKeyValue(yamlKeyValue, "environment");
if (environment != null) {
// FOOBAR=0
for (YAMLSequenceItem yamlSequenceItem : YamlHelper.getSequenceItems(environment)) {
YAMLValue value = yamlSequenceItem.getValue();
if (value instanceof YAMLScalar) {
Expand All @@ -185,6 +194,14 @@ private static void visitEnvironmentSquenceItems(@NotNull Consumer<Pair<String,
}
}
}

// FOOBAR: 0
YAMLMapping childOfType = PsiTreeUtil.getChildOfType(environment, YAMLMapping.class);
if (childOfType != null) {
for (Map.Entry<String, YAMLValue> entry : YamlHelper.getYamlArrayKeyMap(childOfType).entrySet()) {
consumer.accept(Pair.create(entry.getKey(), entry.getValue()));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,28 @@ public static YAMLKeyValue getYamlKeyValue(@NotNull YAMLMapping yamlHash, String
return getYamlKeyValue(yamlHash, keyName, false);
}

/**
* test:
* DEBUG_WEB_1: 1
* DEBUG_WEB_2: 1
*/
@NotNull
public static Map<String, YAMLValue> getYamlArrayKeyMap(@NotNull YAMLMapping yamlHash) {
Map<String, YAMLValue> keys = new HashMap<>();

for(YAMLKeyValue yamlKeyValue: PsiTreeUtil.getChildrenOfAnyType(yamlHash, YAMLKeyValue.class)) {
String keyText = yamlKeyValue.getKeyText();
if (StringUtils.isNotBlank(keyText)) {
YAMLValue value = yamlKeyValue.getValue();
if (value != null) {
keys.put(keyText, value);
}
}
}

return keys;
}

@Nullable
public static String getYamlKeyValueAsString(@NotNull YAMLMapping yamlHash, @NotNull String keyName) {
YAMLKeyValue yamlKeyValue = getYamlKeyValue(yamlHash, keyName, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public String getTestDataPath() {
}

public void testGetEnvironmentVariables() {
assertContainsElements(DotEnvUtil.getEnvironmentVariables(getProject()), "foobar", "DEBUG_WEB", "DEBUG_SERVICES", "DOCKERFILE_FOO");
assertContainsElements(DotEnvUtil.getEnvironmentVariables(getProject()), "foobar", "DEBUG_WEB", "DEBUG_SERVICES", "DOCKERFILE_FOO", "DEBUG_WEB_2", "DEBUG_SERVICES_2", "ADMIN_USER_DOCKERFILE");
}

public void testGetEnvironmentVariableTargets() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ FROM busybox
ENV DOCKERFILE_FOO /bar
WORKDIR ${foo} # WORKDIR /bar
ADD . $foo # ADD . /bar
COPY \$foo /quux # COPY $foo /quux
COPY \$foo /quux # COPY $foo /quux
ENV ADMIN_USER_DOCKERFILE="mark"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ services:
foobar:
environment:
- DEBUG_SERVICES=1
foobar2:
environment:
DEBUG_SERVICES_2: 1

web:
environment:
- DEBUG_WEB=1
- DEBUG_WEB=1

web2:
environment:
DEBUG_WEB_2: 1