Skip to content

Commit

Permalink
PIG_CONTEXT_LOCATION changed to PIG_CONTEXT_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
michalszynkiewicz committed Aug 11, 2020
1 parent 0590814 commit 74f3acc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ __pycache__
**/.settings
.vscode

.bacon

.cache
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Constant {

public static final String CONFIG_ENV = "PNC_CONFIG_PATH";

public static final String PIG_CONTEXT_LOCATION = "PIG_CONTEXT_LOCATION";
public static final String PIG_CONTEXT_DIR = "PIG_CONTEXT_DIR";

public static final String CACHE_FILE = "saved-user.json";

Expand Down
26 changes: 13 additions & 13 deletions pig/src/main/java/org/jboss/pnc/bacon/pig/impl/PigContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Map;
import java.util.Optional;

import com.fasterxml.jackson.databind.SerializationFeature;
import org.jboss.pnc.bacon.pig.impl.config.PigConfiguration;
import org.jboss.pnc.bacon.pig.impl.documents.Deliverables;
import org.jboss.pnc.bacon.pig.impl.pnc.ImportResult;
Expand All @@ -44,7 +45,7 @@
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import static org.jboss.pnc.bacon.common.Constant.PIG_CONTEXT_LOCATION;
import static org.jboss.pnc.bacon.common.Constant.PIG_CONTEXT_DIR;

/**
* TODO: consider saving the latest reached state to not repeat the steps already performed
Expand All @@ -59,6 +60,7 @@ public class PigContext {

static {
jsonMapper = new ObjectMapper();
jsonMapper.enable(SerializationFeature.INDENT_OUTPUT);
jsonMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
jsonMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
jsonMapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);
Expand Down Expand Up @@ -180,30 +182,28 @@ public static PigContext init(boolean clean, String configDir, Optional<String>

private static PigContext readContext(boolean clean) {
PigContext result;
String ctxLocationEnv = System.getenv(PIG_CONTEXT_LOCATION);
Path contextLocation = ctxLocationEnv == null ? Paths.get(".bacon/pig-context.json")
: Paths.get(ctxLocationEnv);
if (!clean && Files.exists(contextLocation)) {

try (InputStream input = Files.newInputStream(contextLocation)) {
String ctxLocationEnv = System.getenv(PIG_CONTEXT_DIR);
Path contextDir = ctxLocationEnv == null ? Paths.get(".bacon") : Paths.get(ctxLocationEnv);
Path contextJson = contextDir.resolve("pig-context.json");
if (!clean && Files.exists(contextJson)) {
try (InputStream input = Files.newInputStream(contextJson)) {
result = jsonMapper.readerFor(PigContext.class).readValue(input);
} catch (IOException e) {
throw new RuntimeException("failed to read PigContext from " + contextLocation.toAbsolutePath(), e);
throw new RuntimeException("failed to read PigContext from " + contextDir.toAbsolutePath(), e);
}
} else {
result = new PigContext();
}

Path ctxDir = contextLocation.getParent();
if (!Files.exists(ctxDir)) {
if (!Files.exists(contextDir)) {
try {
Files.createDirectories(ctxDir);
Files.createDirectories(contextDir);
} catch (IOException e) {
throw new RuntimeException(
"Failed to create a directory to store the pig context: " + ctxDir.toAbsolutePath());
"Failed to create a directory to store the pig context: " + contextDir.toAbsolutePath());
}
}
result.setContextLocation(contextLocation.toAbsolutePath().toString());
result.setContextLocation(contextJson.toAbsolutePath().toString());

return result;
}
Expand Down

0 comments on commit 74f3acc

Please sign in to comment.