-
Notifications
You must be signed in to change notification settings - Fork 12
/
collections.go
119 lines (98 loc) · 2.56 KB
/
collections.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package cli
import (
"encoding/json"
"fmt"
"github.com/bitrise-io/go-utils/colorstring"
flog "github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/stepman/models"
"github.com/bitrise-io/stepman/stepman"
"github.com/urfave/cli"
)
// -------------
// Output Models
// OutputModel ...
type OutputModel struct {
Data *([]models.SteplibInfoModel) `json:"data,omitempty" yaml:"data,omitempty"`
Error string `json:"error,omitempty" yaml:"error,omitempty"`
}
// String ...
func (output OutputModel) String() string {
if output.Error != "" {
return fmt.Sprintf("%s: %s", colorstring.Red("Error"), output.Error)
}
if output.Data == nil {
return ""
}
str := ""
steplibInfos := *output.Data
for idx, steplibInfo := range steplibInfos {
str += colorstring.Bluef("%s\n", steplibInfo.URI)
str += fmt.Sprintf(" spec_path: %s\n", steplibInfo.SpecPath)
if idx != len(steplibInfos)-1 {
str += "\n"
}
}
return str
}
// JSON ...
func (output OutputModel) JSON() string {
bytes, err := json.Marshal(output)
if err != nil {
return fmt.Sprintf(`"Failed to marshal output (%#v), err: %s"`, output, err)
}
return string(bytes)
}
// NewOutput ...
func NewOutput(steplibInfos []models.SteplibInfoModel) OutputModel {
return OutputModel{
Data: &steplibInfos,
}
}
// NewErrorOutput ...
func NewErrorOutput(err error) OutputModel {
return OutputModel{
Error: err.Error(),
}
}
// -------------
func collections(c *cli.Context) error {
format := c.String(FormatKey)
if format == "" {
format = OutputFormatRaw
}
var log flog.Logger
if format == OutputFormatRaw {
log = flog.NewDefaultRawLogger()
} else if format == OutputFormatJSON {
log = flog.NewDefaultJSONLoger()
} else {
failf("invalid format: %s", format)
}
steplibInfos, err := Collections()
if err != nil {
out := NewErrorOutput(err)
if format == OutputFormatJSON {
failf(out.JSON())
}
failf(out.String())
}
log.Print(NewOutput(steplibInfos))
return nil
}
// Collections returns SteplibInfoModels about the locally configured step collections.
func Collections() ([]models.SteplibInfoModel, error) {
var steplibInfos []models.SteplibInfoModel
stepLibURIs := stepman.GetAllStepCollectionPath()
for _, steplibURI := range stepLibURIs {
route, found := stepman.ReadRoute(steplibURI)
if !found {
return nil, fmt.Errorf("no routing found for steplib: %s", steplibURI)
}
specPth := stepman.GetStepSpecPath(route)
steplibInfos = append(steplibInfos, models.SteplibInfoModel{
URI: steplibURI,
SpecPath: specPth,
})
}
return steplibInfos, nil
}