Skip to content

Commit 27eb417

Browse files
feat: printer interface and migration of all commands (#202)
1 parent b45e3c7 commit 27eb417

28 files changed

+317
-277
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- update oauth2 to v0.20.0
2020
- update sync to v0.7.0
2121
- update text to v0.15.0
22+
- introduced Printer interface with Table implementation
2223

2324
## [0.12.2] - 2024-03-07
2425

internal/clioptions/printer.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright Mia srl
2+
// SPDX-License-Identifier: Apache-2.0
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+
package clioptions
17+
18+
import (
19+
"os"
20+
21+
"github.com/mia-platform/miactl/internal/printer"
22+
)
23+
24+
type printerOptions struct {
25+
noWrapLines bool
26+
}
27+
type PrinterOption func(p *printerOptions)
28+
29+
func DisableWrapLines(noWrap bool) PrinterOption {
30+
return func(p *printerOptions) {
31+
p.noWrapLines = noWrap
32+
}
33+
}
34+
35+
func (o *CLIOptions) Printer(options ...PrinterOption) printer.IPrinter {
36+
opts := &printerOptions{}
37+
for _, option := range options {
38+
option(opts)
39+
}
40+
41+
return printer.NewTablePrinter(printer.TablePrinterOptions{
42+
WrapLinesDisabled: opts.noWrapLines,
43+
}, os.Stdout)
44+
}

internal/cmd/company/iam/list.go

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ package iam
1818
import (
1919
"context"
2020
"fmt"
21-
"os"
2221

2322
"github.com/mia-platform/miactl/internal/client"
2423
"github.com/mia-platform/miactl/internal/clioptions"
2524
"github.com/mia-platform/miactl/internal/iam"
25+
"github.com/mia-platform/miactl/internal/printer"
2626
"github.com/mia-platform/miactl/internal/util"
27-
"github.com/olekukonko/tablewriter"
2827
"github.com/spf13/cobra"
2928
)
3029

@@ -48,7 +47,7 @@ all of them noting the type and the current role associated with them`,
4847
iam.ServiceAccountsEntityName: options.ShowServiceAccounts,
4948
}
5049

51-
return listAllIAMEntities(cmd.Context(), client, restConfig.CompanyID, entityTypes)
50+
return listAllIAMEntities(cmd.Context(), client, restConfig.CompanyID, entityTypes, options.Printer())
5251
},
5352
}
5453

@@ -94,14 +93,14 @@ func listEntity(options *clioptions.CLIOptions, commandName, shortHelp, longHelp
9493
client, err := client.APIClientForConfig(restConfig)
9594
cobra.CheckErr(err)
9695

97-
return listSpecificEntities(cmd.Context(), client, restConfig.CompanyID, entityName)
96+
return listSpecificEntities(cmd.Context(), client, restConfig.CompanyID, entityName, options.Printer())
9897
},
9998
}
10099

101100
return cmd
102101
}
103102

104-
func listAllIAMEntities(ctx context.Context, client *client.APIClient, companyID string, entityTypes map[string]bool) error {
103+
func listAllIAMEntities(ctx context.Context, client *client.APIClient, companyID string, entityTypes map[string]bool, p printer.IPrinter) error {
105104
if len(companyID) == 0 {
106105
return fmt.Errorf("missing company id, please set one with the flag or context")
107106
}
@@ -120,19 +119,13 @@ func listAllIAMEntities(ctx context.Context, client *client.APIClient, companyID
120119
return err
121120
}
122121

123-
table := tablewriter.NewWriter(os.Stdout)
124-
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
125-
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
126-
table.SetCenterSeparator("")
127-
table.SetColumnSeparator("")
128-
table.SetRowSeparator("")
129-
table.SetHeader([]string{"ID", "Type", "Name", "Roles"})
130-
table.AppendBulk(rows)
131-
table.Render()
122+
p.Keys("ID", "Type", "Name", "Roles").
123+
BulkRecords(rows...).
124+
Print()
132125
return nil
133126
}
134127

135-
func listSpecificEntities(ctx context.Context, client *client.APIClient, companyID string, entityType string) error {
128+
func listSpecificEntities(ctx context.Context, client *client.APIClient, companyID string, entityType string, p printer.IPrinter) error {
136129
if len(companyID) == 0 {
137130
return fmt.Errorf("missing company id, please set one with the flag or context")
138131
}
@@ -164,15 +157,6 @@ func listSpecificEntities(ctx context.Context, client *client.APIClient, company
164157
return err
165158
}
166159

167-
table := tablewriter.NewWriter(os.Stdout)
168-
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
169-
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
170-
table.SetCenterSeparator("")
171-
table.SetColumnSeparator("")
172-
table.SetRowSeparator("")
173-
table.SetHeader(tableHeaders)
174-
table.SetAutoWrapText(false)
175-
table.AppendBulk(rows)
176-
table.Render()
160+
p.Keys(tableHeaders...).BulkRecords(rows...).Print()
177161
return nil
178162
}

internal/cmd/company/iam/list_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/mia-platform/miactl/internal/client"
2525
"github.com/mia-platform/miactl/internal/iam"
26+
"github.com/mia-platform/miactl/internal/printer"
2627
"github.com/stretchr/testify/assert"
2728
"github.com/stretchr/testify/require"
2829
)
@@ -65,7 +66,7 @@ func TestListAllIAMIdentities(t *testing.T) {
6566

6667
client, err := client.APIClientForConfig(clientConfig)
6768
require.NoError(t, err)
68-
err = listAllIAMEntities(context.TODO(), client, testCase.companyID, testCase.searchParams)
69+
err = listAllIAMEntities(context.TODO(), client, testCase.companyID, testCase.searchParams, &printer.NopPrinter{})
6970
if testCase.err {
7071
assert.Error(t, err)
7172
} else {
@@ -104,7 +105,7 @@ func TestListUsersIdentities(t *testing.T) {
104105
client, err := client.APIClientForConfig(clientConfig)
105106
require.NoError(t, err)
106107

107-
err = listSpecificEntities(context.TODO(), client, testCase.companyID, iam.UsersEntityName)
108+
err = listSpecificEntities(context.TODO(), client, testCase.companyID, iam.UsersEntityName, &printer.NopPrinter{})
108109
if testCase.err {
109110
assert.Error(t, err)
110111
} else {
@@ -146,7 +147,7 @@ func TestListGroupsIdentities(t *testing.T) {
146147
client, err := client.APIClientForConfig(testCase.clientConfig)
147148
require.NoError(t, err)
148149

149-
err = listSpecificEntities(context.TODO(), client, testCase.companyID, iam.GroupsEntityName)
150+
err = listSpecificEntities(context.TODO(), client, testCase.companyID, iam.GroupsEntityName, &printer.NopPrinter{})
150151
if testCase.err {
151152
assert.Error(t, err)
152153
} else {
@@ -188,7 +189,7 @@ func TestServiceAccountGroupsIdentities(t *testing.T) {
188189
client, err := client.APIClientForConfig(testCase.clientConfig)
189190
require.NoError(t, err)
190191

191-
err = listSpecificEntities(context.TODO(), client, testCase.companyID, iam.ServiceAccountsEntityName)
192+
err = listSpecificEntities(context.TODO(), client, testCase.companyID, iam.ServiceAccountsEntityName, &printer.NopPrinter{})
192193
if testCase.err {
193194
assert.Error(t, err)
194195
} else {

internal/cmd/company/list.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ package company
1818
import (
1919
"context"
2020
"fmt"
21-
"os"
2221

2322
"github.com/mia-platform/miactl/internal/client"
2423
"github.com/mia-platform/miactl/internal/clioptions"
24+
"github.com/mia-platform/miactl/internal/printer"
2525
"github.com/mia-platform/miactl/internal/resources"
26-
"github.com/olekukonko/tablewriter"
2726
"github.com/spf13/cobra"
2827
)
2928

@@ -44,13 +43,14 @@ Companies can be used to logically group projects by organizations or internal t
4443
cobra.CheckErr(err)
4544
client, err := client.APIClientForConfig(restConfig)
4645
cobra.CheckErr(err)
47-
return listCompanies(cmd.Context(), client)
46+
printer := options.Printer()
47+
return listCompanies(cmd.Context(), client, printer)
4848
},
4949
}
5050
}
5151

5252
// listCompanies retrieves the companies belonging to the current context
53-
func listCompanies(ctx context.Context, client *client.APIClient) error {
53+
func listCompanies(ctx context.Context, client *client.APIClient, p printer.IPrinter) error {
5454
// execute the request
5555
resp, err := client.Get().APIPath(listCompaniesEndpoint).Do(ctx)
5656
if err != nil {
@@ -66,21 +66,15 @@ func listCompanies(ctx context.Context, client *client.APIClient) error {
6666
return fmt.Errorf("error parsing response body: %w", err)
6767
}
6868

69-
table := tablewriter.NewWriter(os.Stdout)
70-
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
71-
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
72-
table.SetCenterSeparator("")
73-
table.SetColumnSeparator("")
74-
table.SetRowSeparator("")
75-
table.SetHeader([]string{"Name", "Company ID", "Git Provider", "Pipelines"})
69+
p.Keys("Name", "Company ID", "Git Provider", "Pipelines")
7670
for _, company := range companies {
7771
repositoryType := company.Repository.Type
7872
if repositoryType == "" {
7973
repositoryType = "gitlab"
8074
}
81-
table.Append([]string{company.Name, company.TenantID, repositoryType, company.Pipelines.Type})
75+
p.Record(company.Name, company.TenantID, repositoryType, company.Pipelines.Type)
8276
}
8377

84-
table.Render()
78+
p.Print()
8579
return nil
8680
}

internal/cmd/company/list_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/mia-platform/miactl/internal/client"
2525
"github.com/mia-platform/miactl/internal/clioptions"
26+
"github.com/mia-platform/miactl/internal/printer"
2627
"github.com/stretchr/testify/assert"
2728
"github.com/stretchr/testify/require"
2829
)
@@ -63,7 +64,7 @@ func TestListCompanies(t *testing.T) {
6364
testCase.clientConfig.Host = testCase.server.URL
6465
client, err := client.APIClientForConfig(testCase.clientConfig)
6566
require.NoError(t, err)
66-
err = listCompanies(context.TODO(), client)
67+
err = listCompanies(context.TODO(), client, &printer.NopPrinter{})
6768
if testCase.err {
6869
assert.Error(t, err)
6970
} else {

internal/cmd/environments/environments.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ package environments
1818
import (
1919
"context"
2020
"fmt"
21-
"os"
2221
"strconv"
2322

2423
"github.com/mia-platform/miactl/internal/client"
2524
"github.com/mia-platform/miactl/internal/clioptions"
25+
"github.com/mia-platform/miactl/internal/printer"
2626
"github.com/mia-platform/miactl/internal/resources"
27-
"github.com/olekukonko/tablewriter"
2827
"github.com/spf13/cobra"
2928
)
3029

@@ -62,14 +61,14 @@ func listEnvironmentsCmd(o *clioptions.CLIOptions) *cobra.Command {
6261
cobra.CheckErr(err)
6362
client, err := client.APIClientForConfig(restConfig)
6463
cobra.CheckErr(err)
65-
return printEnvironments(cmd.Context(), client, restConfig.CompanyID, restConfig.ProjectID)
64+
return printEnvironments(cmd.Context(), client, restConfig.CompanyID, restConfig.ProjectID, o.Printer())
6665
},
6766
}
6867

6968
return cmd
7069
}
7170

72-
func printEnvironments(ctx context.Context, client *client.APIClient, companyID, projectID string) error {
71+
func printEnvironments(ctx context.Context, client *client.APIClient, companyID, projectID string, p printer.IPrinter) error {
7372
switch {
7473
case len(companyID) == 0:
7574
return fmt.Errorf("missing company id, please set one with the flag or context")
@@ -104,13 +103,7 @@ func printEnvironments(ctx context.Context, client *client.APIClient, companyID,
104103
return nil
105104
}
106105

107-
table := tablewriter.NewWriter(os.Stdout)
108-
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
109-
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
110-
table.SetCenterSeparator("")
111-
table.SetColumnSeparator("")
112-
table.SetRowSeparator("")
113-
table.SetHeader([]string{"Name", "Environment ID", "Production", "Cluster", "Kubernetes Namespace"})
106+
p.Keys("Name", "Environment ID", "Production", "Cluster", "Kubernetes Namespace")
114107

115108
clustersCache := make(map[string]string, 0)
116109
for _, env := range environments {
@@ -125,16 +118,16 @@ func printEnvironments(ctx context.Context, client *client.APIClient, companyID,
125118
clusterName = name
126119
}
127120

128-
table.Append([]string{
121+
p.Record(
129122
env.DisplayName,
130123
env.EnvID,
131124
strconv.FormatBool(env.IsProduction),
132125
clusterName,
133-
env.Cluster.Namespace},
126+
env.Cluster.Namespace,
134127
)
135128
}
136129

137-
table.Render()
130+
p.Print()
138131
return nil
139132
}
140133

internal/cmd/environments/environments_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ import (
2424
"testing"
2525

2626
"github.com/mia-platform/miactl/internal/client"
27+
"github.com/mia-platform/miactl/internal/printer"
2728
"github.com/stretchr/testify/assert"
2829
"github.com/stretchr/testify/require"
2930
)
3031

3132
func TestPreconditions(t *testing.T) {
32-
err := printEnvironments(context.TODO(), nil, "company-id", "")
33+
err := printEnvironments(context.TODO(), nil, "company-id", "", &printer.NopPrinter{})
3334
assert.Error(t, err)
3435

35-
err = printEnvironments(context.TODO(), nil, "", "project-id")
36+
err = printEnvironments(context.TODO(), nil, "", "project-id", &printer.NopPrinter{})
3637
assert.Error(t, err)
3738

38-
err = printEnvironments(context.TODO(), nil, "", "")
39+
err = printEnvironments(context.TODO(), nil, "", "", &printer.NopPrinter{})
3940
assert.Error(t, err)
4041
}
4142

@@ -132,7 +133,7 @@ func TestListEnvironments(t *testing.T) {
132133
})
133134
require.NoError(t, err)
134135

135-
err = printEnvironments(context.TODO(), client, testCase.companyID, testCase.projectID)
136+
err = printEnvironments(context.TODO(), client, testCase.companyID, testCase.projectID, &printer.NopPrinter{})
136137
switch testCase.err {
137138
case false:
138139
require.NoError(t, err)

0 commit comments

Comments
 (0)