Skip to content

Commit d445e44

Browse files
authored
refactor(cli): extract options and output packages (#2440)
Signed-off-by: Miguel Martinez <[email protected]>
1 parent b346d9c commit d445e44

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+161
-107
lines changed

app/cli/cmd/attached_integration_add.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package cmd
1717

1818
import (
19+
"github.com/chainloop-dev/chainloop/app/cli/cmd/output"
1920
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
2021
"github.com/spf13/cobra"
2122
)
@@ -61,7 +62,7 @@ func newAttachedIntegrationAttachCmd() *cobra.Command {
6162
return err
6263
}
6364

64-
return encodeOutput([]*action.AttachedIntegrationItem{res}, attachedIntegrationListTableOutput)
65+
return output.EncodeOutput(flagOutputFormat, []*action.AttachedIntegrationItem{res}, attachedIntegrationListTableOutput)
6566
},
6667
}
6768

app/cli/cmd/attached_integration_list.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"strings"
2121
"time"
2222

23+
"github.com/chainloop-dev/chainloop/app/cli/cmd/output"
2324
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
2425
"github.com/jedib0t/go-pretty/v6/table"
2526
"github.com/spf13/cobra"
@@ -38,7 +39,7 @@ func newAttachedIntegrationListCmd() *cobra.Command {
3839
return err
3940
}
4041

41-
return encodeOutput(res, attachedIntegrationListTableOutput)
42+
return output.EncodeOutput(flagOutputFormat, res, attachedIntegrationListTableOutput)
4243
},
4344
}
4445

@@ -58,7 +59,7 @@ func attachedIntegrationListTableOutput(attachments []*action.AttachedIntegratio
5859
fmt.Println("Integrations attached to workflows")
5960
}
6061

61-
t := newTableWriter()
62+
t := output.NewTableWriter()
6263
t.AppendHeader(table.Row{"ID", "Kind", "Config", "Workflow", "Attached At"})
6364
for _, attachment := range attachments {
6465
wf := attachment.Workflow

app/cli/cmd/attestation_add.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/spf13/viper"
2727
"google.golang.org/grpc"
2828

29+
"github.com/chainloop-dev/chainloop/app/cli/cmd/output"
2930
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
3031
schemaapi "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1"
3132
"github.com/chainloop-dev/chainloop/pkg/resourceloader"
@@ -118,7 +119,7 @@ func newAttestationAddCmd() *cobra.Command {
118119
return err
119120
}
120121

121-
return encodeOutput(resp, func(s *action.AttestationStatusMaterial) error {
122+
return output.EncodeOutput(flagOutputFormat, resp, func(s *action.AttestationStatusMaterial) error {
122123
return displayMaterialInfo(s, policies[resp.Name])
123124
})
124125
},
@@ -168,7 +169,7 @@ func displayMaterialInfo(status *action.AttestationStatusMaterial, policyEvaluat
168169
return nil
169170
}
170171

171-
mt := newTableWriter()
172+
mt := output.NewTableWriter()
172173

173174
mt.AppendRow(table.Row{"Name", status.Material.Name})
174175
mt.AppendRow(table.Row{"Type", status.Material.Type})

app/cli/cmd/attestation_init.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"fmt"
2121

22+
"github.com/chainloop-dev/chainloop/app/cli/cmd/output"
2223
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
2324
"github.com/spf13/cobra"
2425
)
@@ -129,7 +130,7 @@ func newAttestationInitCmd() *cobra.Command {
129130
logger.Warn().Msg("DEPRECATION WARNING: --project not set, this will be required in the near future")
130131
}
131132

132-
return encodeOutput(res, fullStatusTable)
133+
return output.EncodeOutput(flagOutputFormat, res, fullStatusTable)
133134
}}
134135

135136
// This option is only useful for local-based attestation states

app/cli/cmd/attestation_push.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"google.golang.org/grpc/codes"
2525
"google.golang.org/grpc/status"
2626

27+
"github.com/chainloop-dev/chainloop/app/cli/cmd/output"
2728
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
2829
)
2930

@@ -106,7 +107,7 @@ func newAttestationPushCmd() *cobra.Command {
106107
res.Status.Digest = res.Digest
107108

108109
// If we are returning the json format, we also want to render the attestation table as one property so it can also be consumed
109-
if flagOutputFormat == formatJSON {
110+
if flagOutputFormat == output.FormatJSON {
110111
// Render the attestation status to a string
111112
buf := &bytes.Buffer{}
112113
if err := fullStatusTableWithWriter(res.Status, buf); err != nil {
@@ -117,7 +118,7 @@ func newAttestationPushCmd() *cobra.Command {
117118
}
118119

119120
// In TABLE format, we render the attestation status
120-
if err := encodeOutput(res.Status, fullStatusTable); err != nil {
121+
if err := output.EncodeOutput(flagOutputFormat, res.Status, fullStatusTable); err != nil {
121122
return fmt.Errorf("failed to render output: %w", err)
122123
}
123124

app/cli/cmd/attestation_status.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/muesli/reflow/wrap"
2929
"github.com/spf13/cobra"
3030

31+
"github.com/chainloop-dev/chainloop/app/cli/cmd/output"
3132
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
3233
"github.com/chainloop-dev/chainloop/pkg/attestation/renderer/chainloop"
3334
)
@@ -58,12 +59,12 @@ func newAttestationStatusCmd() *cobra.Command {
5859
return err
5960
}
6061

61-
output := simpleStatusTable
62+
outputF := simpleStatusTable
6263
if full {
63-
output = fullStatusTable
64+
outputF = fullStatusTable
6465
}
6566

66-
return encodeOutput(res, output)
67+
return output.EncodeOutput(flagOutputFormat, res, outputF)
6768
},
6869
}
6970

@@ -87,7 +88,7 @@ func fullStatusTableWithWriter(status *action.AttestationStatusResult, w io.Writ
8788

8889
func attestationStatusTableOutput(status *action.AttestationStatusResult, w io.Writer, full bool) error {
8990
// General info table
90-
gt := newTableWriterWithWriter(w)
91+
gt := output.NewTableWriterWithWriter(w)
9192
gt.AppendRow(table.Row{"Initialized At", status.InitializedAt.Format(time.RFC822)})
9293
gt.AppendSeparator()
9394
meta := status.WorkflowMeta
@@ -154,7 +155,7 @@ func envVarsTable(status *action.AttestationStatusResult, w io.Writer, full bool
154155

155156
if len(status.EnvVars) > 0 {
156157
// Env Variables table
157-
evt := newTableWriterWithWriter(w)
158+
evt := output.NewTableWriterWithWriter(w)
158159
evt.SetTitle("Env Variables")
159160
for k, v := range status.EnvVars {
160161
if v == "" {
@@ -167,7 +168,7 @@ func envVarsTable(status *action.AttestationStatusResult, w io.Writer, full bool
167168

168169
runnerVars := status.RunnerContext.EnvVars
169170
if len(runnerVars) > 0 && full {
170-
evt := newTableWriterWithWriter(w)
171+
evt := output.NewTableWriterWithWriter(w)
171172
evt.SetTitle("Runner context")
172173
for k, v := range runnerVars {
173174
if v == "" {
@@ -190,7 +191,7 @@ func materialsTable(status *action.AttestationStatusResult, w io.Writer, full bo
190191
return strings.Compare(a.Name, b.Name)
191192
})
192193

193-
mt := newTableWriterWithWriter(w)
194+
mt := output.NewTableWriterWithWriter(w)
194195
mt.SetTitle("Materials")
195196

196197
for _, m := range status.Materials {

app/cli/cmd/available_integration_describe.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"sort"
2323

24+
"github.com/chainloop-dev/chainloop/app/cli/cmd/output"
2425
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
2526
"github.com/chainloop-dev/chainloop/app/controlplane/plugins/sdk/v1"
2627
"github.com/jedib0t/go-pretty/v6/table"
@@ -44,7 +45,7 @@ func newAvailableIntegrationDescribeCmd() *cobra.Command {
4445
return fmt.Errorf("integration %q not found", integrationName)
4546
}
4647

47-
return encodeOutput([]*action.AvailableIntegrationItem{item}, availableIntegrationDescribeTableOutput)
48+
return output.EncodeOutput(flagOutputFormat, []*action.AvailableIntegrationItem{item}, availableIntegrationDescribeTableOutput)
4849
},
4950
}
5051

@@ -97,7 +98,7 @@ func renderSchemaTable(tableTitle string, properties sdk.SchemaPropertiesMap) er
9798
return nil
9899
}
99100

100-
t := newTableWriter()
101+
t := output.NewTableWriter()
101102
t.SetTitle(tableTitle)
102103
t.AppendHeader(table.Row{"Field", "Type", "Required", "Description"})
103104

@@ -143,7 +144,7 @@ func renderSchemaRaw(tableTitle string, s string) error {
143144
return err
144145
}
145146

146-
rt := newTableWriter()
147+
rt := output.NewTableWriter()
147148
rt.SetTitle(tableTitle)
148149
rt.AppendRow(table.Row{prettyAttachmentJSON.String()})
149150
rt.Render()

app/cli/cmd/available_integration_list.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"strings"
2121

22+
"github.com/chainloop-dev/chainloop/app/cli/cmd/output"
2223
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
2324
"github.com/jedib0t/go-pretty/v6/table"
2425
"github.com/spf13/cobra"
@@ -35,7 +36,7 @@ func newAvailableIntegrationListCmd() *cobra.Command {
3536
return err
3637
}
3738

38-
return encodeOutput(res, availableIntegrationListTableOutput)
39+
return output.EncodeOutput(flagOutputFormat, res, availableIntegrationListTableOutput)
3940
},
4041
}
4142

@@ -48,7 +49,7 @@ func availableIntegrationListTableOutput(items []*action.AvailableIntegrationIte
4849
return nil
4950
}
5051

51-
t := newTableWriter()
52+
t := output.NewTableWriter()
5253
t.AppendHeader(table.Row{"Name", "Version", "Material Requirement", "Description"})
5354
for _, i := range items {
5455
t.AppendRow(table.Row{i.Name, i.Version, strings.Join(i.SubscribedInputs, ", "), i.Description})

app/cli/cmd/casbackend_add_azureblob.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cmd
1818
import (
1919
"fmt"
2020

21+
"github.com/chainloop-dev/chainloop/app/cli/cmd/output"
2122
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
2223
"github.com/chainloop-dev/chainloop/pkg/blobmanager/azureblob"
2324
"github.com/go-kratos/kratos/v2/log"
@@ -70,7 +71,7 @@ func newCASBackendAddAzureBlobStorageCmd() *cobra.Command {
7071
return nil
7172
}
7273

73-
return encodeOutput(res, casBackendItemTableOutput)
74+
return output.EncodeOutput(flagOutputFormat, res, casBackendItemTableOutput)
7475
},
7576
}
7677

app/cli/cmd/casbackend_add_oci.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package cmd
1717

1818
import (
19+
"github.com/chainloop-dev/chainloop/app/cli/cmd/output"
1920
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
2021
"github.com/go-kratos/kratos/v2/log"
2122
"github.com/spf13/cobra"
@@ -65,7 +66,7 @@ func newCASBackendAddOCICmd() *cobra.Command {
6566
return nil
6667
}
6768

68-
return encodeOutput(res, casBackendItemTableOutput)
69+
return output.EncodeOutput(flagOutputFormat, res, casBackendItemTableOutput)
6970
},
7071
}
7172

0 commit comments

Comments
 (0)