Skip to content

Commit 01e461a

Browse files
committed
refactor: extract duplicate outputformat code to a shared package
1 parent 1f3564e commit 01e461a

File tree

3 files changed

+31
-62
lines changed

3 files changed

+31
-62
lines changed

pkg/commands/cluster/list.go

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cluster
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"io"
87

@@ -14,12 +13,11 @@ import (
1413
"github.com/intility/indev/internal/ux"
1514
"github.com/intility/indev/pkg/client"
1615
"github.com/intility/indev/pkg/clientset"
16+
"github.com/intility/indev/pkg/outputformat"
1717
)
1818

19-
var errInvalidOutputFormat = errors.New(`must be one of "wide", "json", "yaml"`)
20-
2119
func NewListCommand(set clientset.ClientSet) *cobra.Command {
22-
output := outputFormat("")
20+
output := outputformat.Format("")
2321
// clusterListCmd represents the list command.
2422
cmd := &cobra.Command{
2523
Use: "list",
@@ -55,7 +53,7 @@ func NewListCommand(set clientset.ClientSet) *cobra.Command {
5553
return cmd
5654
}
5755

58-
func printClusterList(writer io.Writer, format outputFormat, clusters client.ClusterList) error {
56+
func printClusterList(writer io.Writer, format outputformat.Format, clusters client.ClusterList) error {
5957
var err error
6058

6159
switch format {
@@ -126,32 +124,6 @@ func statusMessage(cluster client.Cluster) string {
126124
return cluster.Status.Ready.Message
127125
}
128126

129-
type OutputFormat interface {
130-
String() string
131-
Set(val string) error
132-
Type() string
133-
}
134-
135-
type outputFormat string
136-
137-
func (o *outputFormat) String() string {
138-
return string(*o)
139-
}
140-
141-
func (o *outputFormat) Set(value string) error {
142-
switch value {
143-
case "wide", "json", "yaml":
144-
*o = outputFormat(value)
145-
return nil
146-
default:
147-
return errInvalidOutputFormat
148-
}
149-
}
150-
151-
func (o *outputFormat) Type() string {
152-
return "outputFormat"
153-
}
154-
155127
func nodePoolSummary(cluster client.Cluster) string {
156128
if len(cluster.NodePools) == 0 {
157129
return "0"

pkg/commands/teams/list.go

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package teams
22

33
import (
44
"encoding/json"
5-
"errors"
65
"io"
76
"sort"
87
"strings"
@@ -15,12 +14,11 @@ import (
1514
"github.com/intility/indev/internal/ux"
1615
"github.com/intility/indev/pkg/client"
1716
"github.com/intility/indev/pkg/clientset"
17+
"github.com/intility/indev/pkg/outputformat"
1818
)
1919

20-
var errInvalidOutputFormat = errors.New(`must be one of "wide", "json", "yaml"`)
21-
2220
func NewListCommand(set clientset.ClientSet) *cobra.Command {
23-
output := outputFormat("")
21+
output := outputformat.Format("")
2422
cmd := &cobra.Command{
2523
Use: "list",
2624
Short: "List all teams",
@@ -55,7 +53,7 @@ func NewListCommand(set clientset.ClientSet) *cobra.Command {
5553
return cmd
5654
}
5755

58-
func printTeamsList(writer io.Writer, format outputFormat, teams []client.Team) error {
56+
func printTeamsList(writer io.Writer, format outputformat.Format, teams []client.Team) error {
5957
var err error
6058

6159
sort.Slice(teams, func(i, j int) bool {
@@ -102,29 +100,3 @@ func printTeamsList(writer io.Writer, format outputFormat, teams []client.Team)
102100

103101
return nil
104102
}
105-
106-
type OutputFormat interface {
107-
String() string
108-
Set(val string) error
109-
Type() string
110-
}
111-
112-
type outputFormat string
113-
114-
func (o *outputFormat) String() string {
115-
return string(*o)
116-
}
117-
118-
func (o *outputFormat) Set(value string) error {
119-
switch value {
120-
case "wide", "json", "yaml":
121-
*o = outputFormat(value)
122-
return nil
123-
default:
124-
return errInvalidOutputFormat
125-
}
126-
}
127-
128-
func (o *outputFormat) Type() string {
129-
return "outputFormat"
130-
}

pkg/outputformat/outputformat.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package outputformat
2+
3+
import "errors"
4+
5+
var ErrInvalidOutputFormat = errors.New(`must be one of "wide", "json", "yaml"`)
6+
7+
type Format string
8+
9+
func (o *Format) String() string {
10+
return string(*o)
11+
}
12+
13+
func (o *Format) Set(value string) error {
14+
switch value {
15+
case "wide", "json", "yaml":
16+
*o = Format(value)
17+
return nil
18+
default:
19+
return ErrInvalidOutputFormat
20+
}
21+
}
22+
23+
func (o *Format) Type() string {
24+
return "outputFormat"
25+
}

0 commit comments

Comments
 (0)