decentral1se
c764243f3a
All checks were successful
continuous-integration/drone/push Build is passing
144 lines
3.5 KiB
Go
144 lines
3.5 KiB
Go
package app
|
|
|
|
import (
|
|
"sort"
|
|
"strings"
|
|
|
|
abraFormatter "coopcloud.tech/abra/cli/formatter"
|
|
"coopcloud.tech/abra/pkg/catalogue"
|
|
"coopcloud.tech/abra/pkg/config"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
var status bool
|
|
var statusFlag = &cli.BoolFlag{
|
|
Name: "status",
|
|
Aliases: []string{"S"},
|
|
Value: false,
|
|
Usage: "Show app deployment status",
|
|
Destination: &status,
|
|
}
|
|
|
|
var appType string
|
|
var typeFlag = &cli.StringFlag{
|
|
Name: "type",
|
|
Aliases: []string{"t"},
|
|
Value: "",
|
|
Usage: "Show apps of a specific type",
|
|
Destination: &appType,
|
|
}
|
|
|
|
var listAppServer string
|
|
var listAppServerFlag = &cli.StringFlag{
|
|
Name: "server",
|
|
Aliases: []string{"s"},
|
|
Value: "",
|
|
Usage: "Show apps of a specific server",
|
|
Destination: &listAppServer,
|
|
}
|
|
|
|
var appListCommand = &cli.Command{
|
|
Name: "list",
|
|
Usage: "List all managed apps",
|
|
Description: `
|
|
This command looks at your local file system listing of apps and servers (e.g.
|
|
in ~/.abra/) to generate a report of all your apps.
|
|
|
|
By passing the "--status/-S" flag, you can query all your servers for the
|
|
actual live deployment status. Depending on how many servers you manage, this
|
|
can take some time.
|
|
`,
|
|
Aliases: []string{"ls"},
|
|
Flags: []cli.Flag{
|
|
statusFlag,
|
|
listAppServerFlag,
|
|
typeFlag,
|
|
},
|
|
Action: func(c *cli.Context) error {
|
|
appFiles, err := config.LoadAppFiles(listAppServer)
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
apps, err := config.GetApps(appFiles)
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
sort.Sort(config.ByServerAndType(apps))
|
|
|
|
statuses := make(map[string]map[string]string)
|
|
tableCol := []string{"Server", "Type", "Domain"}
|
|
if status {
|
|
tableCol = append(tableCol, "Status", "Version", "Updates")
|
|
statuses, err = config.GetAppStatuses(appFiles)
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
}
|
|
|
|
table := abraFormatter.CreateTable(tableCol)
|
|
table.SetAutoMergeCellsByColumnIndex([]int{0})
|
|
|
|
for _, app := range apps {
|
|
var tableRow []string
|
|
if app.Type == appType || appType == "" {
|
|
// If type flag is set, check for it, if not, Type == ""
|
|
tableRow = []string{app.Server, app.Type, app.Domain}
|
|
if status {
|
|
stackName := app.StackName()
|
|
if app.Env["STACK_NAME"] != "" {
|
|
stackName = app.Env["STACK_NAME"]
|
|
}
|
|
|
|
status := "unknown"
|
|
version := "unknown"
|
|
if statusMeta, ok := statuses[stackName]; ok {
|
|
if currentVersion, exists := statusMeta["version"]; exists {
|
|
version = currentVersion
|
|
}
|
|
if statusMeta["status"] != "" {
|
|
status = statusMeta["status"]
|
|
}
|
|
|
|
tableRow = append(tableRow, status, version)
|
|
} else {
|
|
tableRow = append(tableRow, status, version)
|
|
}
|
|
|
|
var newUpdates []string
|
|
updates, err := catalogue.GetRecipeCatalogueVersions(app.Type)
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
for _, update := range updates {
|
|
if update != version {
|
|
newUpdates = append(newUpdates, update)
|
|
}
|
|
}
|
|
|
|
if len(newUpdates) == 0 {
|
|
if version == "unknown" {
|
|
tableRow = append(tableRow, "unknown")
|
|
} else {
|
|
tableRow = append(tableRow, "none, on latest")
|
|
}
|
|
} else {
|
|
// FIXME: jeezus golang why do you not have a list reverse function
|
|
for i, j := 0, len(newUpdates)-1; i < j; i, j = i+1, j-1 {
|
|
newUpdates[i], newUpdates[j] = newUpdates[j], newUpdates[i]
|
|
}
|
|
tableRow = append(tableRow, strings.Join(newUpdates, "\n"))
|
|
}
|
|
}
|
|
}
|
|
table.Append(tableRow)
|
|
}
|
|
|
|
table.Render()
|
|
|
|
return nil
|
|
},
|
|
}
|