Skip to content
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

feat: label command #661

Merged
merged 2 commits into from
Oct 11, 2023
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
15 changes: 15 additions & 0 deletions src/main/java/com/crowdin/cli/client/ClientLabel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.crowdin.cli.client;

import com.crowdin.client.labels.model.AddLabelRequest;
import com.crowdin.client.labels.model.Label;

import java.util.List;

public interface ClientLabel extends Client {

List<Label> listLabels();

Label addLabel(AddLabelRequest request);

void deleteLabel(Long id);
}
5 changes: 5 additions & 0 deletions src/main/java/com/crowdin/cli/client/Clients.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public static ClientScreenshot getClientScreenshot(String apiToken, String baseU
return new CrowdinClientScreenshot(client, projectId);
}

public static ClientLabel getClientLabel(String apiToken, String baseUrl, String projectId) {
com.crowdin.client.Client client = prepareClient(apiToken, baseUrl);
return new CrowdinClientLabel(client, projectId);
}

// mb divide args to move token and url to constructor?
public static ProjectClient getProjectClient(String apiToken, String baseUrl, long projectId) {
com.crowdin.client.Client client = prepareClient(apiToken, baseUrl);
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/crowdin/cli/client/CrowdinClientLabel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.crowdin.cli.client;

import com.crowdin.client.Client;
import com.crowdin.client.labels.model.AddLabelRequest;
import com.crowdin.client.labels.model.Label;
import lombok.AllArgsConstructor;

import java.util.List;

import static java.lang.Long.parseLong;

@AllArgsConstructor
public class CrowdinClientLabel extends CrowdinClientCore implements ClientLabel {

private final Client client;
private final String projectId;

@Override
public List<Label> listLabels() {
return executeRequestFullList((limit, offset) -> this.client.getLabelsApi()
.listLabels(parseLong(this.projectId), limit, offset));
}

@Override
public Label addLabel(AddLabelRequest request) {
return executeRequest(() -> this.client.getLabelsApi()
.addLabel(parseLong(this.projectId), request)
.getData());
}

@Override
public void deleteLabel(Long id) {
executeRequest(() -> {
this.client.getLabelsApi().deleteLabel(parseLong(this.projectId), id);
return null;
});
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,10 @@ NewAction<PropertiesWithFiles, ProjectClient> preTranslate(
NewAction<ProjectProperties, ClientScreenshot> screenshotUpload(File file, String branchName, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress, ProjectClient projectClient);

NewAction<ProjectProperties, ClientScreenshot> screenshotDelete(String name);

NewAction<ProjectProperties, ClientLabel> labelList(boolean plainView, boolean isVerbose);

NewAction<ProjectProperties, ClientLabel> labelAdd(String title, boolean plainView);

NewAction<ProjectProperties, ClientLabel> labelDelete(String title);
}
15 changes: 15 additions & 0 deletions src/main/java/com/crowdin/cli/commands/actions/CliActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,19 @@ public NewAction<ProjectProperties, ClientScreenshot> screenshotUpload(File file
public NewAction<ProjectProperties, ClientScreenshot> screenshotDelete(String name) {
return new ScreenshotDeleteAction(name);
}

@Override
public NewAction<ProjectProperties, ClientLabel> labelList(boolean plainView, boolean isVerbose) {
return new LabelListAction(plainView, isVerbose);
}

@Override
public NewAction<ProjectProperties, ClientLabel> labelAdd(String title, boolean plainView) {
return new LabelAddAction(title, plainView);
}

@Override
public NewAction<ProjectProperties, ClientLabel> labelDelete(String title) {
return new LabelDeleteAction(title);
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/crowdin/cli/commands/actions/LabelAddAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.crowdin.cli.commands.actions;

import com.crowdin.cli.client.ClientLabel;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.utils.console.ExecutionStatus;
import com.crowdin.client.labels.model.AddLabelRequest;
import com.crowdin.client.labels.model.Label;
import lombok.AllArgsConstructor;

import java.util.Map;
import java.util.stream.Collectors;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.SKIPPED;

@AllArgsConstructor
class LabelAddAction implements NewAction<ProjectProperties, ClientLabel> {

private final String title;
private final boolean plainView;

@Override
public void act(Outputter out, ProjectProperties properties, ClientLabel client) {
Map<String, Long> labels = client.listLabels().stream()
.collect(Collectors.toMap(Label::getTitle, Label::getId));
if (!labels.containsKey(title)) {
AddLabelRequest request = new AddLabelRequest();
request.setTitle(title);
Label label = client.addLabel(request);
if (!plainView) {
out.println(ExecutionStatus.OK.withIcon(
String.format(RESOURCE_BUNDLE.getString("message.label.added"), label.getId(), label.getTitle())
));
} else {
out.println(title);

Check warning on line 37 in src/main/java/com/crowdin/cli/commands/actions/LabelAddAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/LabelAddAction.java#L37

Added line #L37 was not covered by tests
}
} else {
if (!plainView) {
out.println(SKIPPED.withIcon(String.format(RESOURCE_BUNDLE.getString("message.label.already_exists"), title)));
} else {
out.println(String.format(RESOURCE_BUNDLE.getString("message.label_already_exists"), title));

Check warning on line 43 in src/main/java/com/crowdin/cli/commands/actions/LabelAddAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/LabelAddAction.java#L43

Added line #L43 was not covered by tests
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.crowdin.cli.commands.actions;

import com.crowdin.cli.client.ClientLabel;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.utils.console.ExecutionStatus;
import com.crowdin.client.labels.model.Label;
import lombok.AllArgsConstructor;

import java.util.Map;
import java.util.stream.Collectors;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;

@AllArgsConstructor
class LabelDeleteAction implements NewAction<ProjectProperties, ClientLabel> {

private final String title;

@Override
public void act(Outputter out, ProjectProperties properties, ClientLabel client) {
Map<String, Long> labels = client.listLabels().stream()
.collect(Collectors.toMap(Label::getTitle, Label::getId));
if (!labels.containsKey(title)) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.label.not_found"));
}
client.deleteLabel(labels.get(title));
out.println(ExecutionStatus.OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.label.deleted"), title)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.crowdin.cli.commands.actions;

import com.crowdin.cli.client.ClientLabel;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.client.labels.model.Label;
import lombok.AllArgsConstructor;

import java.util.List;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;

@AllArgsConstructor
class LabelListAction implements NewAction<ProjectProperties, ClientLabel> {

private final boolean plainView;
private final boolean isVerbose;

@Override
public void act(Outputter out, ProjectProperties properties, ClientLabel client) {
List<Label> labels = client.listLabels();
for (Label label : labels) {
if (!plainView || isVerbose) {
out.println(String.format(RESOURCE_BUNDLE.getString("message.label.list"),
label.getId(), label.getTitle()));
} else {
out.println(label.getTitle());

Check warning on line 29 in src/main/java/com/crowdin/cli/commands/actions/LabelListAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/LabelListAction.java#L29

Added line #L29 was not covered by tests
}
}
if (labels.isEmpty()) {
if (!plainView && !isVerbose) {
out.println(OK.withIcon(RESOURCE_BUNDLE.getString("message.label.list_empty")));
} else {
out.println(RESOURCE_BUNDLE.getString("message.label.list_empty"));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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;
import com.crowdin.cli.properties.PropertiesBuilders;
import picocli.CommandLine;

public abstract class ActCommandLabel extends GenericActCommand<ProjectProperties, ClientLabel> {

@CommandLine.Mixin
private ConfigurationFilesProperties properties;

@CommandLine.ArgGroup(exclusive = false, headingKey = "params.heading")
private ProjectParams params;

@Override
protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders, Outputter out) {
return propertiesBuilders.buildProjectProperties(out, properties.getConfigFile(), properties.getIdentityFile(), params);
}

@Override
protected ClientLabel getClient(ProjectProperties properties) {
return Clients.getClientLabel(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ public final class CommandNames {
public static final String SCREENSHOT_LIST = "list";
public static final String SCREENSHOT_UPLOAD = "upload";
public static final String SCREENSHOT_DELETE = "delete";

public static final String LABEL = "label";
public static final String LABEL_LIST = "list";
public static final String LABEL_ADD = "add";
public static final String LABEL_DELETE = "delete";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.crowdin.cli.commands.picocli;

import com.crowdin.cli.client.ClientLabel;
import com.crowdin.cli.commands.Actions;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.properties.ProjectProperties;
import picocli.CommandLine;

@CommandLine.Command(
sortOptions = false,
name = CommandNames.LABEL_ADD
)
public class LabelAddSubcommand extends ActCommandLabel {

@CommandLine.Parameters(descriptionKey = "crowdin.label.title")
protected String title;

@CommandLine.Option(names = {"--plain"}, descriptionKey = "crowdin.list.usage.plain")
protected boolean plainView;

@Override
protected NewAction<ProjectProperties, ClientLabel> getAction(Actions actions) {
return actions.labelAdd(title, plainView);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.crowdin.cli.commands.picocli;

import com.crowdin.cli.client.ClientLabel;
import com.crowdin.cli.commands.Actions;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.properties.ProjectProperties;
import picocli.CommandLine;

@CommandLine.Command(
sortOptions = false,
name = CommandNames.LABEL_DELETE
)
public class LabelDeleteSubcommand extends ActCommandLabel {

@CommandLine.Parameters(descriptionKey = "crowdin.label.title")
protected String title;

@Override
protected NewAction<ProjectProperties, ClientLabel> getAction(Actions actions) {
return actions.labelDelete(title);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.crowdin.cli.commands.picocli;

import com.crowdin.cli.client.ClientLabel;
import com.crowdin.cli.commands.Actions;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.properties.ProjectProperties;
import picocli.CommandLine;

@CommandLine.Command(
sortOptions = false,
name = CommandNames.LABEL_LIST
)
class LabelListSubcommand extends ActCommandLabel {

@CommandLine.Option(names = {"--plain"}, descriptionKey = "crowdin.list.usage.plain")
protected boolean plainView;

@Override
protected NewAction<ProjectProperties, ClientLabel> getAction(Actions actions) {
return actions.labelList(this.plainView, this.isVerbose);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.crowdin.cli.commands.picocli;

import picocli.CommandLine;

@CommandLine.Command(
name = CommandNames.LABEL,
subcommands = {
LabelListSubcommand.class,
LabelAddSubcommand.class,
LabelDeleteSubcommand.class
}
)
class LabelSubcommand extends HelpCommand {

@Override
protected CommandLine getCommand(CommandLine rootCommand) {
return rootCommand.getSubcommands().get(CommandNames.LABEL);

Check warning on line 17 in src/main/java/com/crowdin/cli/commands/picocli/LabelSubcommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/picocli/LabelSubcommand.java#L17

Added line #L17 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
BranchSubcommand.class,
CommentSubcommand.class,
DistributionSubcommand.class,
ScreenshotSubcommand.class
ScreenshotSubcommand.class,
LabelSubcommand.class,
})
class RootCommand extends HelpCommand {
@Override
Expand Down
Loading