Skip to content

Commit 7842a7a

Browse files
authored
Adding Go Get command to go driver (#709)
1 parent dc65c64 commit 7842a7a

File tree

9 files changed

+54
-18
lines changed

9 files changed

+54
-18
lines changed

Diff for: build-info-extractor-go/src/main/java/org/jfrog/build/extractor/go/GoDriver.java

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class GoDriver implements Serializable {
2121
Arrays.asList("list", "-f", "\"{{with .Module}}{{.Path}} {{.Version}}{{end}}\"", "all");
2222
private static final List<String> GO_MOD_TIDY_CMD = Arrays.asList("mod", "tidy");
2323
private static final String GO_MOD_GRAPH_CMD = "mod graph";
24+
private static final String GO_GET_CMD = "get";
2425
private static final String GO_LIST_MODULE_CMD = "list -m";
2526
private static final String GO_VERSION_CMD = "version";
2627

@@ -106,6 +107,17 @@ public void modTidy(boolean verbose, boolean ignoreErrors) throws IOException {
106107
runCmd(argsList, verbose);
107108
}
108109

110+
/**
111+
* Run go get.
112+
* @param componentId - Component ID string. ( Example: github.com/jfrog/build-info-go@v1.8.7 )
113+
* @param verbose - True if should print the results to the log
114+
* @throws IOException - in case of any I/O error.
115+
*/
116+
public void get(String componentId, boolean verbose) throws IOException {
117+
List<String> argsList = new ArrayList<>(Arrays.asList(GO_GET_CMD, componentId));
118+
runCmd(argsList, verbose);
119+
}
120+
109121
/**
110122
* If ignoreErrors=false, run:
111123
* go list -f "{{with .Module}}{{.Path}} {{.Version}}{{end}}" all

Diff for: build-info-extractor-go/src/main/java/org/jfrog/build/extractor/go/extractor/GoCommand.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ abstract class GoCommand extends PackageManagerExtractor {
3030
// Module name, as specified in go.mod, is used for naming the relevant go package files.
3131
String moduleName;
3232
// Module id is used to determine which buildInfo's module should be used for the current go operation.
33-
// By default it's value is moduleNme, unless customize differently.
33+
// By default, it's value is moduleNme, unless customize differently.
3434
String buildInfoModuleId;
3535
Log logger;
3636

@@ -43,7 +43,7 @@ abstract class GoCommand extends PackageManagerExtractor {
4343

4444
protected void preparePrerequisites(String repo, ArtifactoryManager artifactoryManager) throws VersionException, IOException {
4545
validateArtifactoryVersion(artifactoryManager);
46-
validateRepoExists(repo, artifactoryManager, "The provided repo must be specified");
46+
validateRepoExists(repo, artifactoryManager);
4747
}
4848

4949
private void validateArtifactoryVersion(ArtifactoryManager artifactoryManager) throws VersionException, IOException {
@@ -53,14 +53,14 @@ private void validateArtifactoryVersion(ArtifactoryManager artifactoryManager) t
5353
throw new VersionException(message, VersionCompatibilityType.NOT_FOUND);
5454
}
5555
if (!version.isAtLeast(MIN_SUPPORTED_ARTIFACTORY_VERSION)) {
56-
String message = String.format("Couldn't execute Go task. Artifactory version is %s but must be at least %s.", version.toString(), MIN_SUPPORTED_ARTIFACTORY_VERSION.toString());
56+
String message = String.format("Couldn't execute Go task. Artifactory version is %s but must be at least %s.", version, MIN_SUPPORTED_ARTIFACTORY_VERSION);
5757
throw new VersionException(message, VersionCompatibilityType.INCOMPATIBLE);
5858
}
5959
}
6060

61-
private void validateRepoExists(String repo, ArtifactoryManager artifactoryManager, String repoNotSpecifiedMsg) throws IOException {
61+
private void validateRepoExists(String repo, ArtifactoryManager artifactoryManager) throws IOException {
6262
if (StringUtils.isBlank(repo)) {
63-
throw new IllegalArgumentException(repoNotSpecifiedMsg);
63+
throw new IllegalArgumentException("The provided repo must be specified");
6464
}
6565
if (!artifactoryManager.isRepositoryExist(repo)) {
6666
throw new IOException("Repo " + repo + " doesn't exist");

Diff for: build-info-extractor-go/src/main/java/org/jfrog/build/extractor/go/extractor/GoPublish.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.jfrog.build.extractor.go.GoDriver;
2222

2323
import java.io.File;
24-
import java.io.FileOutputStream;
2524
import java.io.IOException;
2625
import java.nio.file.Files;
2726
import java.nio.file.Path;
@@ -47,10 +46,10 @@ public class GoPublish extends GoCommand {
4746
private static final String PKG_MOD_FILE_EXTENSION = "mod";
4847
private static final String PKG_INFO_FILE_EXTENSION = "info";
4948

50-
private ArrayListMultimap<String, String> properties;
51-
private List<Artifact> artifactList = new ArrayList<>();
52-
private String deploymentRepo;
53-
private String version;
49+
private final ArrayListMultimap<String, String> properties;
50+
private final List<Artifact> artifactList = new ArrayList<>();
51+
private final String deploymentRepo;
52+
private final String version;
5453

5554
/**
5655
* Publish go package.
@@ -125,7 +124,7 @@ private void createAndDeployZip(ArtifactoryManager artifactoryManager) throws Ex
125124
File tmpZipFile = archiveProjectDir();
126125

127126
// Second, filter the raw zip file according to Go rules and create deployable zip can be later resolved.
128-
// We use the same code as Artifactory when he resolve a Go module directly from Github.
127+
// We use the same code as Artifactory when he resolves a Go module directly from GitHub.
129128
File deployableZipFile = File.createTempFile(LOCAL_PKG_FILENAME, PKG_ZIP_FILE_EXTENSION, path.toFile());
130129
try (GoZipBallStreamer pkgArchiver = new GoZipBallStreamer(new ZipFile(tmpZipFile), moduleName, version, logger)) {
131130
pkgArchiver.writeDeployableZip(deployableZipFile);
@@ -179,7 +178,7 @@ private File archiveProjectDir() throws IOException {
179178
private File writeInfoFile(String localInfoPath) throws IOException {
180179
File infoFile = new File(localInfoPath);
181180
ObjectMapper mapper = new ObjectMapper();
182-
Map<String, String> infoMap = new HashMap();
181+
Map<String, String> infoMap = new HashMap<>();
183182
Date date = new Date();
184183
Instant instant = date.toInstant();
185184

Diff for: build-info-extractor-go/src/main/java/org/jfrog/build/extractor/go/extractor/GoRun.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public BuildInfo execute() {
109109
preparePrerequisites(resolutionRepository, artifactoryClient);
110110
setResolverAsGoProxy(artifactoryClient);
111111
}
112-
// We create the GoDriver here as env might had changed.
112+
// We create the GoDriver here as env might have changed.
113113
this.goDriver = new GoDriver(GO_CLIENT_CMD, env, path.toFile(), logger);
114114
// First try to run 'go version' to make sure go is in PATH, and write the output to logger.
115115
goDriver.version(true);
@@ -124,7 +124,7 @@ public BuildInfo execute() {
124124

125125
/**
126126
* In order to use Artifactory as a resolver we need to set GOPROXY env var with Artifactory details.
127-
* Wa also support fallback to VCS in case pkg doesn't exist in Artifactort,
127+
* Wa also support fallback to VCS in case pkg doesn't exist in Artifactory,
128128
*/
129129
private void setResolverAsGoProxy(ArtifactoryManager artifactoryClient) throws Exception {
130130
String rtUrl = PackageManagerUtils.createArtifactoryUrlWithCredentials(artifactoryClient.getUrl(), resolverUsername, resolverPassword, ARTIFACTORY_GO_API + resolutionRepository);

Diff for: build-info-extractor-go/src/main/java/org/jfrog/build/extractor/go/extractor/GoVersionUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static boolean isCompatibleGoModuleNaming(String projectName, String vers
9494
}
9595

9696
/**
97-
* @return Sub module name in github projects
97+
* @return Sub module name in GitHub projects
9898
*/
9999
public static String getSubModule(String projectName) {
100100
if (StringUtils.isBlank(projectName)) {

Diff for: build-info-extractor-go/src/test/java/org/jfrog/build/extractor/go/GoDriverTest.java

+25
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.stream.Collectors;
1818

1919
import static org.testng.Assert.assertEquals;
20+
import static org.testng.Assert.assertTrue;
2021

2122
/**
2223
* @author yahavi
@@ -89,4 +90,28 @@ public void testTidyListUsedModulesErroneous() throws IOException {
8990
FileUtils.deleteDirectory(projectDir);
9091
}
9192
}
93+
94+
/**
95+
* Test "go get".
96+
*
97+
* @throws IOException in case of any I/O exception.
98+
*/
99+
@Test
100+
public void testGoGet() throws IOException {
101+
File projectDir = Files.createTempDirectory("").toFile();
102+
try {
103+
FileUtils.copyDirectory(PROJECT_1.toFile(), projectDir);
104+
GoDriver driver = new GoDriver(null, System.getenv(), projectDir, new NullLog());
105+
driver.modTidy(false, true);
106+
// Get dependency and expect it later on go modules list
107+
driver.get("rsc.io/sampler@v1.3.1", false);
108+
109+
// Run "go list -f {{with .Module}}{{.Path}} {{.Version}}{{end}} all"
110+
CommandResults results = driver.getUsedModules(false, false, false);
111+
Set<String> actualUsedModules = Arrays.stream(results.getRes().split("\\r?\\n")).map(String::trim).collect(Collectors.toSet());
112+
assertTrue(actualUsedModules.contains("rsc.io/sampler v1.3.1"));
113+
} finally {
114+
FileUtils.deleteDirectory(projectDir);
115+
}
116+
}
92117
}

Diff for: build-info-extractor-gradle/src/test/java/org/jfrog/gradle/plugin/artifactory/Utils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static void generateBuildInfoProperties(String contextUrl, String username, Stri
112112
* @throws IOException - In case of any IO error
113113
*/
114114
private static String generateBuildInfoPropertiesForServer(String contextUrl, String username, String password, String localRepo, String virtualRepo, String publications, boolean publishBuildInfo, Path source) throws IOException {
115-
String content = new String(Files.readAllBytes(source), StandardCharsets.UTF_8);
115+
String content = new String(Files.readAllBytes(source), StandardCharsets.UTF_8.name());
116116
Map<String, String> valuesMap = new HashMap<String, String>() {{
117117
put("publications", publications);
118118
put("contextUrl", contextUrl);

Diff for: build-info-extractor-pip/src/test/java/org/jfrog/build/extractor/pip/extractor/PipLogParserTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void pipLogParserTest() throws IOException {
4040
// Read pip log.
4141
InputStream pipLogStream = this.getClass().getResourceAsStream("/pipLogParser/pipLog.txt");
4242
try {
43-
String pipLog = IOUtils.toString(pipLogStream, StandardCharsets.UTF_8);
43+
String pipLog = IOUtils.toString(pipLogStream, StandardCharsets.UTF_8.name());
4444
// Parse.
4545
Map<String, String> actualMap = PipLogParser.parse(pipLog, log);
4646
// Validate.

Diff for: build-info-extractor/src/testFixtures/java/org/jfrog/build/IntegrationTestsBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ protected void createTestRepo(String repoKey) throws IOException {
158158
if (repoConfigInputStream == null) {
159159
throw new IOException("Couldn't find repository settings in " + path);
160160
}
161-
String json = IOUtils.toString(repoConfigInputStream, StandardCharsets.UTF_8);
161+
String json = IOUtils.toString(repoConfigInputStream, StandardCharsets.UTF_8.name());
162162
artifactoryManager.createRepository(repoKey, stringSubstitutor.replace(json));
163163
}
164164
}

0 commit comments

Comments
 (0)