/
commands.go
50 lines (46 loc) · 1.31 KB
/
commands.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
package formatter
import (
"strconv"
"strings"
"github.com/krok-o/krok/pkg/models"
)
// FormatCommand formats a command and displays it with the request
// format option.
func FormatCommand(command *models.Command, opt string) string {
var listOfRepoNames []string
for _, r := range command.Repositories {
listOfRepoNames = append(listOfRepoNames, r.Name)
}
var listOfPlatforms []string
for _, r := range command.Platforms {
listOfPlatforms = append(listOfPlatforms, r.Name)
}
d := []kv{
{"id", command.ID},
{"name", command.Name},
{"schedule", command.Schedule},
{"image", command.Image},
{"enabled", strconv.FormatBool(command.Enabled)},
{"repositories", strings.Join(listOfRepoNames, ",")},
{"platforms", strings.Join(listOfPlatforms, ",")},
}
formatter := NewFormatter(opt)
return formatter.FormatObject(d)
}
// FormatCommands formats a list of commands and displays it with the requested
// format option.
func FormatCommands(commands []*models.Command, opt string) string {
var d [][]kv
for _, command := range commands {
data := []kv{
{"id", command.ID},
{"name", command.Name},
{"schedule", command.Schedule},
{"image", command.Image},
{"enabled", strconv.FormatBool(command.Enabled)},
}
d = append(d, data)
}
formatter := NewFormatter(opt)
return formatter.FormatList(d)
}