forked from kata-containers/tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.go
83 lines (64 loc) · 1.75 KB
/
display.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"os"
"sort"
)
var outputFile = os.Stdout
// displayHandler is an interface that all output display handlers
// (formatters) must implement.
type DisplayHandler interface {
DisplayLabels(lf *LabelsFile) error
DisplayCategories(lf *LabelsFile, showLabels bool) error
}
// DisplayHandlers encapsulates the list of available display handlers.
type DisplayHandlers struct {
handlers map[string]DisplayHandler
}
// handlers is a map of the available output format display handling
// implementations.
var handlers map[string]DisplayHandler
// NewDisplayHandlers create a new DisplayHandler.
func NewDisplayHandlers() *DisplayHandlers {
if handlers == nil {
handlers = make(map[string]DisplayHandler)
handlers["md"] = NewDisplayMD(outputFile)
handlers[textFormat] = NewDisplayText(outputFile)
handlers["tsv"] = NewDisplayTSV(outputFile)
}
h := &DisplayHandlers{
handlers: handlers,
}
return h
}
// find looks for a display handler corresponding to the specified format
func (d *DisplayHandlers) find(format string) DisplayHandler {
for f, handler := range d.handlers {
if f == format {
return handler
}
}
return nil
}
// Get returns a list of the available formatters (display handler names).
func (d *DisplayHandlers) Get() []string {
var formats []string
for f := range d.handlers {
formats = append(formats, f)
}
sort.Strings(formats)
return formats
}
func show(inputFilename string, handler DisplayHandler, what DataToShow, withLabels bool) error {
lf, err := readYAML(inputFilename)
if err != nil {
return err
}
if what == showLabels {
return handler.DisplayLabels(lf)
}
return handler.DisplayCategories(lf, withLabels)
}