@@ -14,7 +14,8 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- package utils
17+ // Package printer implements printing functionality for cli.
18+ package printer
1819
1920import (
2021 "encoding/json"
@@ -27,14 +28,35 @@ import (
2728 "k8s.io/cli-runtime/pkg/printers"
2829)
2930
30- // PrinterType is a type declaration for a printer type.
31- type PrinterType string
31+ // PType is a type declaration for a printer type.
32+ type PType string
3233
33- var (
34- // PrinterTypeTable is a table printer type.
35- PrinterTypeTable = PrinterType ("table" )
36- // PrinterTypeJSON is a json printer type.
37- PrinterTypeJSON = PrinterType ("json" )
34+ // String type casts to string.
35+ func (p * PType ) String () string {
36+ return string (* p )
37+ }
38+
39+ // Set sets value for var.
40+ func (p * PType ) Set (s string ) error {
41+ switch s {
42+ case string (PrinterTypeTable ), string (PrinterTypeJSON ):
43+ * p = PType (s )
44+ return nil
45+ default :
46+ return ErrUnknowPrinterType
47+ }
48+ }
49+
50+ // Type returns type in string format.
51+ func (p * PType ) Type () string {
52+ return "PType"
53+ }
54+
55+ const (
56+ // PrinterTypeTable is a table printer PType.
57+ PrinterTypeTable = PType ("table" )
58+ // PrinterTypeJSON is a json printer PType.
59+ PrinterTypeJSON = PType ("json" )
3860)
3961
4062var (
@@ -51,12 +73,12 @@ type Printer interface {
5173 Print (in interface {}) error
5274}
5375
54- // NewPrinter creates a new printer.
55- func NewPrinter (printerType string , writer io.Writer ) (Printer , error ) {
76+ // New creates a new printer.
77+ func New (printerType PType , writer io.Writer ) (Printer , error ) {
5678 switch printerType {
57- case string ( PrinterTypeTable ) :
79+ case PrinterTypeTable :
5880 return & tablePrinter {writer : writer }, nil
59- case string ( PrinterTypeJSON ) :
81+ case PrinterTypeJSON :
6082 return & jsonPrinter {writer : writer }, nil
6183 default :
6284 return nil , ErrUnknowPrinterType
0 commit comments