Skip to content

Commit

Permalink
fix: creds as args via cmd (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ authored Jan 16, 2025
1 parent 5331d62 commit 4d48fc6
Show file tree
Hide file tree
Showing 29 changed files with 235 additions and 175 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ NewAction<BaseProperties, ClientTm> tmDownload(
NewAction<ProjectProperties, ClientTask> taskAdd(
boolean noProgress, String title, Integer type, String language, List<String> files, String branch, Long workflowStep,
String description, Boolean skipAssignedStrings, Boolean includePreTranslatedStringsOnly,
List<Long> labels, ProjectClient projectClient, boolean plainView);
List<Long> labels, boolean plainView);

NewAction<ProjectProperties, ClientDistribution> distributionList(boolean plainView);

NewAction<ProjectProperties, ClientDistribution> distributionAdd(boolean noProgress, boolean plainView, String name, ExportMode exportMode, List<String> files, List<Integer> bundleIds, String branch, ProjectClient projectClient);
NewAction<ProjectProperties, ClientDistribution> distributionAdd(boolean noProgress, boolean plainView, String name, ExportMode exportMode, List<String> files, List<Integer> bundleIds, String branch);

NewAction<ProjectProperties, ClientDistribution> distributionRelease(boolean noProgress, boolean plainView, String hash, ProjectClient projectClient);
NewAction<ProjectProperties, ClientDistribution> distributionRelease(boolean noProgress, boolean plainView, String hash);

NewAction<ProjectProperties, ClientComment> commentList(boolean plainView, boolean isVerbose, String stringId, com.crowdin.client.stringcomments.model.Type type, com.crowdin.client.issues.model.Type issueType, IssueStatus status);

Expand Down Expand Up @@ -126,7 +126,7 @@ NewAction<PropertiesWithFiles, ProjectClient> preTranslate(

NewAction<ProjectProperties, ClientScreenshot> screenshotList(Long stringId, boolean plainView);

NewAction<ProjectProperties, ClientScreenshot> screenshotUpload(File file, String branchName, List<String> labelNames, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress, ProjectClient projectClient);
NewAction<ProjectProperties, ClientScreenshot> screenshotUpload(File file, String branchName, List<String> labelNames, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress);

NewAction<ProjectProperties, ClientScreenshot> screenshotDelete(Long id);

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/crowdin/cli/commands/actions/CliActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ public NewAction<ProjectProperties, ClientTask> taskList(boolean plainView, bool
@Override
public NewAction<ProjectProperties, ClientTask> taskAdd(boolean noProgress, String title, Integer type, String language,
List<String> files, String branch, Long workflowStep, String description, Boolean skipAssignedStrings,
Boolean includePreTranslatedStringsOnly, List<Long> labels, ProjectClient projectClient, boolean plainView
Boolean includePreTranslatedStringsOnly, List<Long> labels, boolean plainView
) {
return new TaskAddAction(noProgress, title, type, language, files, branch, workflowStep, description, skipAssignedStrings, includePreTranslatedStringsOnly, labels, projectClient, plainView);
return new TaskAddAction(noProgress, title, type, language, files, branch, workflowStep, description, skipAssignedStrings, includePreTranslatedStringsOnly, labels, plainView);
}

@Override
Expand All @@ -183,13 +183,13 @@ public NewAction<ProjectProperties, ClientDistribution> distributionList(boolean
}

@Override
public NewAction<ProjectProperties, ClientDistribution> distributionAdd(boolean noProgress, boolean plainView, String name, ExportMode exportMode, List<String> files, List<Integer> bundleIds, String branch, ProjectClient projectClient) {
return new DistributionAddAction(noProgress, plainView, name, exportMode, files, bundleIds, branch, projectClient);
public NewAction<ProjectProperties, ClientDistribution> distributionAdd(boolean noProgress, boolean plainView, String name, ExportMode exportMode, List<String> files, List<Integer> bundleIds, String branch) {
return new DistributionAddAction(noProgress, plainView, name, exportMode, files, bundleIds, branch);
}

@Override
public NewAction<ProjectProperties, ClientDistribution> distributionRelease(boolean noProgress, boolean plainView, String hash, ProjectClient projectClient) {
return new DistributionReleaseAction(noProgress, plainView, hash, projectClient);
public NewAction<ProjectProperties, ClientDistribution> distributionRelease(boolean noProgress, boolean plainView, String hash) {
return new DistributionReleaseAction(noProgress, plainView, hash);
}

@Override
Expand Down Expand Up @@ -264,8 +264,8 @@ public NewAction<ProjectProperties, ClientScreenshot> screenshotList(Long string
}

@Override
public NewAction<ProjectProperties, ClientScreenshot> screenshotUpload(File file, String branchName, List<String> labelNames, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress, ProjectClient projectClient) {
return new ScreenshotUploadAction(file, branchName, labelNames, filePath, directoryPath, autoTag, plainView, noProgress, projectClient);
public NewAction<ProjectProperties, ClientScreenshot> screenshotUpload(File file, String branchName, List<String> labelNames, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress) {
return new ScreenshotUploadAction(file, branchName, labelNames, filePath, directoryPath, autoTag, plainView, noProgress);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.crowdin.cli.client.ClientDistribution;
import com.crowdin.cli.client.CrowdinProjectFull;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.commands.functionality.RequestBuilder;
import com.crowdin.cli.commands.picocli.ExitCodeExceptionMapper;
import com.crowdin.cli.commands.picocli.GenericActCommand;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.utils.Utils;
import com.crowdin.cli.utils.console.ConsoleSpinner;
Expand Down Expand Up @@ -41,16 +41,15 @@ class DistributionAddAction implements NewAction<ProjectProperties, ClientDistri
private List<Integer> bundleIds;
private String branch;

private ProjectClient projectClient;

@Override
public void act(Outputter out, ProjectProperties pb, ClientDistribution client) {
var projectClient = GenericActCommand.getProjectClient(pb);
CrowdinProjectFull project = ConsoleSpinner.execute(
out,
"message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress,
this.plainView,
() -> this.projectClient.downloadFullProject(this.branch)
() -> projectClient.downloadFullProject(this.branch)
);
boolean isStringsBasedProject = Objects.equals(project.getType(), Type.STRINGS_BASED);

Expand All @@ -64,12 +63,12 @@ public void act(Outputter out, ProjectProperties pb, ClientDistribution client)
List<String> projectFiles = project.getFiles().stream()
.filter(file -> branch == null || file.getBranchId().equals(projectBranches.get(branch)))
.map(FileInfo::getPath)
.collect(Collectors.toList());
.toList();
List<String> notExistingFiles = files.stream()
.map(file -> branch == null ? file : Paths.get(branch, file).toString())
.map(Utils::sepAtStart)
.filter(file -> !projectFiles.contains(file))
.collect(Collectors.toList());
.toList();
if (!notExistingFiles.isEmpty()) {
throw new ExitCodeExceptionMapper.NotFoundException(notExistingFiles.stream().map(Utils::noSepAtStart)
.map(file -> String.format(RESOURCE_BUNDLE.getString("error.file_not_found"), file))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.crowdin.cli.client.ClientDistribution;
import com.crowdin.cli.client.CrowdinProjectInfo;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.commands.picocli.GenericActCommand;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.utils.console.ConsoleSpinner;
import com.crowdin.client.distributions.model.DistributionRelease;
Expand All @@ -24,16 +24,16 @@ class DistributionReleaseAction implements NewAction<ProjectProperties, ClientDi
private boolean plainView;
private String hash;

private ProjectClient projectClient;

@Override
public void act(Outputter out, ProjectProperties pb, ClientDistribution client) {
var projectClient = GenericActCommand.getProjectClient(pb);

CrowdinProjectInfo project = ConsoleSpinner.execute(
out,
"message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress,
this.plainView,
() -> this.projectClient.downloadProjectInfo()
projectClient::downloadProjectInfo
);
boolean isStringsBasedProject = Objects.equals(project.getType(), Type.STRINGS_BASED);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.commands.functionality.RequestBuilder;
import com.crowdin.cli.commands.picocli.ExitCodeExceptionMapper;
import com.crowdin.cli.commands.picocli.GenericActCommand;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.utils.Utils;
import com.crowdin.cli.utils.console.ConsoleSpinner;
Expand Down Expand Up @@ -46,16 +47,15 @@ class ScreenshotUploadAction implements NewAction<ProjectProperties, ClientScree
private final boolean plainView;
private final boolean noProgress;

private ProjectClient projectClient;

@Override
public void act(Outputter out, ProjectProperties properties, ClientScreenshot client) {
var projectClient = GenericActCommand.getProjectClient(properties);
CrowdinProjectFull project = ConsoleSpinner.execute(
out,
"message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress,
this.plainView,
() -> this.projectClient.downloadFullProject());
projectClient::downloadFullProject);

Long branchId = null;
if (nonNull(branchName)) {
Expand Down Expand Up @@ -84,7 +84,7 @@ public void act(Outputter out, ProjectProperties properties, ClientScreenshot cl
Optional<Screenshot> existingScreenshot = screenshotList.stream().findFirst();
if (existingScreenshot.isPresent()) {
UpdateScreenshotRequest request = new UpdateScreenshotRequest();
request.setStorageId(uploadToStorage(file));
request.setStorageId(uploadToStorage(projectClient, file));
request.setName(file.getName());
request.setUsePreviousTags(!autoTag);
try {
Expand All @@ -111,13 +111,13 @@ public void act(Outputter out, ProjectProperties properties, ClientScreenshot cl

AddScreenshotRequest request = new AddScreenshotRequest();
if (nonNull(labelNames) && !labelNames.isEmpty()) {
request.setLabelIds(prepareLabelIds());
request.setLabelIds(prepareLabelIds(projectClient));
}

request.setBranchId(branchId);
request.setDirectoryId(directoryId);
request.setFileId(fileId);
request.setStorageId(uploadToStorage(file));
request.setStorageId(uploadToStorage(projectClient, file));
request.setName(file.getName());
request.setAutoTag(autoTag);

Expand All @@ -140,17 +140,17 @@ public void act(Outputter out, ProjectProperties properties, ClientScreenshot cl
}
}

private Long uploadToStorage(File fileToUpload) {
private Long uploadToStorage(ProjectClient projectClient, File fileToUpload) {
Long storageId;
try (InputStream fileStream = Files.newInputStream(fileToUpload.toPath())) {
storageId = this.projectClient.uploadStorage(fileToUpload.getName(), fileStream);
storageId = projectClient.uploadStorage(fileToUpload.getName(), fileStream);
} catch (Exception e) {
throw ExitCodeExceptionMapper.remap(e, String.format(RESOURCE_BUNDLE.getString("error.upload_to_storage"), fileToUpload.getName()));
}
return storageId;
}

private Long[] prepareLabelIds() {
private Long[] prepareLabelIds(ProjectClient projectClient) {
Map<String, Long> labels = projectClient.listLabels().stream()
.collect(Collectors.toMap(Label::getTitle, Label::getId));
labelNames.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import com.crowdin.cli.client.ClientTask;
import com.crowdin.cli.client.CrowdinProjectFull;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;

import com.crowdin.cli.commands.functionality.ProjectFilesUtils;
import com.crowdin.cli.commands.functionality.PropertiesBeanUtils;
import com.crowdin.cli.commands.picocli.ExitCodeExceptionMapper;
import com.crowdin.cli.commands.picocli.GenericActCommand;
import com.crowdin.cli.properties.ProjectProperties;

import com.crowdin.cli.utils.Utils;
Expand Down Expand Up @@ -38,16 +38,16 @@ class TaskAddAction implements NewAction<ProjectProperties, ClientTask> {
private Boolean skipAssignedStrings;
private Boolean includePreTranslatedStringsOnly;
private List<Long> labels;
private ProjectClient projectClient;
private boolean plainView;

@Override
public void act(Outputter out, ProjectProperties pb, ClientTask client) {
var projectClient = GenericActCommand.getProjectClient(pb);
boolean isOrganization = PropertiesBeanUtils.isOrganization(pb.getBaseUrl());
Task task;
AddTaskRequest addTaskRequest;
CrowdinProjectFull project = ConsoleSpinner.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress, this.plainView, () -> this.projectClient.downloadFullProject(this.branch));
this.noProgress, this.plainView, () -> projectClient.downloadFullProject(this.branch));

List<Long> fileIds = new ArrayList<>();
Map<String, FileInfo> paths = ProjectFilesUtils.buildFilePaths(project.getDirectories(), project.getBranches(), project.getFileInfos());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.crowdin.cli.commands.picocli;

import com.crowdin.cli.client.ClientBundle;
import com.crowdin.cli.client.Clients;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.ProjectParams;
import com.crowdin.cli.properties.ProjectProperties;
Expand All @@ -24,6 +22,6 @@ protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders,

@Override
protected ClientBundle getClient(ProjectProperties properties) {
return Clients.getClientBundle(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId());
return GenericActCommand.getBundleClient(properties);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.crowdin.cli.commands.picocli;

import com.crowdin.cli.client.ClientComment;
import com.crowdin.cli.client.Clients;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.ProjectParams;
import com.crowdin.cli.properties.ProjectProperties;
Expand All @@ -23,6 +22,6 @@ protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders,

@Override
protected ClientComment getClient(ProjectProperties properties) {
return Clients.getClientComment(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId());
return GenericActCommand.getCommentClient(properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.crowdin.cli.client.ClientDistribution;
import com.crowdin.cli.client.Clients;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.ProjectParams;
import com.crowdin.cli.properties.ProjectProperties;
Expand All @@ -26,8 +25,4 @@ protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders,
protected ClientDistribution getClient(ProjectProperties properties) {
return Clients.getClientDistribution(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId());
}

protected ProjectClient getProjectClient(ProjectProperties properties) {
return Clients.getProjectClient(properties.getApiToken(), properties.getBaseUrl(), Long.parseLong(properties.getProjectId()));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.crowdin.cli.commands.picocli;

import com.crowdin.cli.client.ClientGlossary;
import com.crowdin.cli.client.Clients;
import com.crowdin.cli.properties.BaseProperties;

public abstract class ActCommandGlossary extends ActCommand<ClientGlossary> {

@Override
protected ClientGlossary getClient(BaseProperties properties) {
return Clients.getClientGlossary(properties.getApiToken(), properties.getBaseUrl());
return GenericActCommand.getGlossaryClient(properties);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.crowdin.cli.commands.picocli;

import com.crowdin.cli.client.ClientLabel;
import com.crowdin.cli.client.ClientScreenshot;
import com.crowdin.cli.client.Clients;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.ProjectParams;
import com.crowdin.cli.properties.ProjectProperties;
Expand All @@ -25,6 +22,6 @@ protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders,

@Override
protected ClientLabel getClient(ProjectProperties properties) {
return Clients.getClientLabel(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId());
return GenericActCommand.getLabelClient(properties);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.crowdin.cli.commands.picocli;

import com.crowdin.cli.client.Clients;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.ProjectProperties;
Expand All @@ -23,6 +22,6 @@ protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders,

@Override
protected ProjectClient getClient(ProjectProperties properties) {
return Clients.getProjectClient(properties.getApiToken(), properties.getBaseUrl(), Long.parseLong(properties.getProjectId()));
return GenericActCommand.getProjectClient(properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.crowdin.cli.client.ClientScreenshot;
import com.crowdin.cli.client.Clients;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.ProjectParams;
import com.crowdin.cli.properties.ProjectProperties;
Expand All @@ -26,8 +25,4 @@ protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders,
protected ClientScreenshot getClient(ProjectProperties properties) {
return Clients.getClientScreenshot(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId());
}

protected ProjectClient getProjectClient(ProjectProperties properties) {
return Clients.getProjectClient(properties.getApiToken(), properties.getBaseUrl(), Long.parseLong(properties.getProjectId()));
}
}
Loading

0 comments on commit 4d48fc6

Please sign in to comment.