|
| 1 | +/* |
| 2 | + * Copyright 2021-2023, Seqera. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package io.seqera.tower.cli.responses.datastudios; |
| 19 | + |
| 20 | +import java.io.PrintWriter; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import javax.annotation.Nullable; |
| 24 | + |
| 25 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
| 26 | +import io.seqera.tower.cli.responses.Response; |
| 27 | +import io.seqera.tower.cli.utils.PaginationInfo; |
| 28 | +import io.seqera.tower.cli.utils.TableList; |
| 29 | +import io.seqera.tower.model.DataStudioDto; |
| 30 | +import io.seqera.tower.model.DataStudioStatusInfo; |
| 31 | +import io.seqera.tower.model.StudioUser; |
| 32 | + |
| 33 | +import static io.seqera.tower.cli.utils.FormatHelper.formatDataStudioStatus; |
| 34 | + |
| 35 | +public class DataStudiosList extends Response { |
| 36 | + |
| 37 | + public final String workspaceRef; |
| 38 | + public final List<DataStudioDto> studios; |
| 39 | + |
| 40 | + @JsonIgnore |
| 41 | + @Nullable |
| 42 | + private final PaginationInfo paginationInfo; |
| 43 | + |
| 44 | + public DataStudiosList(String workspaceRef, List<DataStudioDto> studios, @Nullable PaginationInfo paginationInfo) { |
| 45 | + this.workspaceRef = workspaceRef; |
| 46 | + this.studios = studios; |
| 47 | + this.paginationInfo = paginationInfo; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void toString(PrintWriter out) { |
| 52 | + |
| 53 | + out.println(ansi(String.format("%n @|bold Data Studios at %s workspace:|@%n", workspaceRef))); |
| 54 | + |
| 55 | + if (studios.isEmpty()) { |
| 56 | + out.println(ansi(" @|yellow No data studios found|@")); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + List<String> descriptions = new ArrayList<>(List.of("ID", "Name", "Description", "User", "Status")); |
| 61 | + |
| 62 | + TableList table = new TableList(out, descriptions.size(), descriptions.toArray(new String[descriptions.size()])).sortBy(0); |
| 63 | + table.setPrefix(" "); |
| 64 | + |
| 65 | + studios.forEach(studio -> { |
| 66 | + |
| 67 | + DataStudioStatusInfo statusInfo = studio.getStatusInfo(); |
| 68 | + StudioUser user = studio.getUser(); |
| 69 | + List<String> rows = new ArrayList<>(List.of( |
| 70 | + studio.getSessionId() == null ? "" : studio.getSessionId(), |
| 71 | + studio.getName() == null ? "" : studio.getName(), |
| 72 | + studio.getDescription() == null ? "" : studio.getDescription(), |
| 73 | + user == null ? "" : user.getUserName(), |
| 74 | + formatDataStudioStatus(statusInfo == null ? null : statusInfo.getStatus()) |
| 75 | + )); |
| 76 | + |
| 77 | + table.addRow(rows.toArray(new String[rows.size()])); |
| 78 | + }); |
| 79 | + |
| 80 | + table.print(); |
| 81 | + |
| 82 | + PaginationInfo.addFooter(out, paginationInfo); |
| 83 | + |
| 84 | + out.println(""); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments