Files
abra/cli/app/list.go
decentral1se 7c5a83bce2
Some checks failed
continuous-integration/drone/push Build is failing
fix: ust list from bubbles
See #691
2025-10-15 18:54:45 +02:00

555 lines
13 KiB
Go

package app
import (
"encoding/json"
"fmt"
"sort"
"strings"
"time"
"coopcloud.tech/abra/cli/internal"
appPkg "coopcloud.tech/abra/pkg/app"
"coopcloud.tech/abra/pkg/autocomplete"
"coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/i18n"
"coopcloud.tech/abra/pkg/log"
"coopcloud.tech/abra/pkg/recipe"
"coopcloud.tech/tagcmp"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
)
type appStatus struct {
Server string `json:"server"`
Recipe string `json:"recipe"`
AppName string `json:"appName"`
Domain string `json:"domain"`
Status string `json:"status"`
Chaos string `json:"chaos"`
ChaosVersion string `json:"chaosVersion"`
AutoUpdate string `json:"autoUpdate"`
Version string `json:"version"`
Upgrade string `json:"upgrade"`
}
type serverStatus struct {
Apps []appStatus `json:"apps"`
AppCount int `json:"appCount"`
VersionCount int `json:"versionCount"`
UnversionedCount int `json:"unversionedCount"`
LatestCount int `json:"latestCount"`
UpgradeCount int `json:"upgradeCount"`
}
// translators: `abra app list` aliases. use a comma separated list of aliases with
// no spaces in between
var appListAliases = i18n.G("ls")
var AppListCommand = &cobra.Command{
// translators: `app list` command
Use: i18n.G("list [flags]"),
Aliases: strings.Split(appListAliases, ","),
// translators: Short description for `app list` command
Short: i18n.G("List all managed apps"),
Long: i18n.G(`Generate a report of all managed apps.
Use "status/S" flag to query all servers for the live deployment status.`),
Example: i18n.G(` # list apps of all servers without live status
abra app ls
# list apps of a specific server with live status
abra app ls s 1312.net S
# list apps of all servers which match a specific recipe
abra app ls r gitea`),
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
appFiles, err := appPkg.LoadAppFiles(listAppServer)
if err != nil {
log.Fatal(err)
}
apps, err := appPkg.GetApps(appFiles, recipeFilter)
if err != nil {
log.Fatal(err)
}
sort.Sort(appPkg.ByServerAndRecipe(apps))
statuses := make(map[string]map[string]string)
if status && internal.MachineReadable {
alreadySeen := make(map[string]bool)
for _, app := range apps {
if _, ok := alreadySeen[app.Server]; !ok {
alreadySeen[app.Server] = true
}
}
statuses, err = appPkg.GetAppStatuses(apps, internal.MachineReadable)
if err != nil {
log.Fatal(err)
}
}
var totalServersCount int
var totalAppsCount int
allStats := make(map[string]serverStatus)
for _, app := range apps {
var stats serverStatus
var ok bool
if stats, ok = allStats[app.Server]; !ok {
stats = serverStatus{}
if recipeFilter == "" {
// count server, no filtering
totalServersCount++
}
}
if app.Recipe.Name == recipeFilter || recipeFilter == "" {
if recipeFilter != "" {
// only count server if matches filter
totalServersCount++
}
appStats := appStatus{}
stats.AppCount++
totalAppsCount++
if status && internal.MachineReadable {
status := i18n.G("unknown")
version := i18n.G("unknown")
chaos := i18n.G("unknown")
chaosVersion := i18n.G("unknown")
autoUpdate := i18n.G("unknown")
if statusMeta, ok := statuses[app.StackName()]; ok {
if currentVersion, exists := statusMeta["version"]; exists {
if currentVersion != "" {
version = currentVersion
}
}
if chaosDeploy, exists := statusMeta["chaos"]; exists {
chaos = chaosDeploy
}
if chaosDeployVersion, exists := statusMeta["chaosVersion"]; exists {
chaosVersion = chaosDeployVersion
}
if autoUpdateState, exists := statusMeta["autoUpdate"]; exists {
autoUpdate = autoUpdateState
}
if statusMeta["status"] != "" {
status = statusMeta["status"]
}
stats.VersionCount++
} else {
stats.UnversionedCount++
}
appStats.Status = status
appStats.Chaos = chaos
appStats.ChaosVersion = chaosVersion
appStats.Version = version
appStats.AutoUpdate = autoUpdate
var newUpdates []string
if version != "unknown" && chaos == "false" {
if err := app.Recipe.EnsureExists(); err != nil {
log.Fatal(i18n.G("unable to clone %s: %s", app.Name, err))
}
updates, err := app.Recipe.Tags()
if err != nil {
log.Fatal(i18n.G("unable to retrieve tags for %s: %s", app.Name, err))
}
parsedVersion, err := tagcmp.Parse(version)
if err != nil {
log.Fatal(err)
}
for _, update := range updates {
if ok := tagcmp.IsParsable(update); !ok {
log.Debug(i18n.G("unable to parse %s, skipping as upgrade option", update))
continue
}
parsedUpdate, err := tagcmp.Parse(update)
if err != nil {
log.Fatal(err)
}
if update != version && parsedUpdate.IsGreaterThan(parsedVersion) {
newUpdates = append(newUpdates, update)
}
}
}
if len(newUpdates) == 0 {
if version == "unknown" {
appStats.Upgrade = i18n.G("unknown")
} else {
appStats.Upgrade = i18n.G("latest")
stats.LatestCount++
}
} else {
newUpdates = internal.SortVersionsDesc(newUpdates)
appStats.Upgrade = strings.Join(newUpdates, "\n")
stats.UpgradeCount++
}
}
appStats.Server = app.Server
appStats.Recipe = app.Recipe.Name
appStats.AppName = app.Name
appStats.Domain = app.Domain
stats.Apps = append(stats.Apps, appStats)
}
allStats[app.Server] = stats
}
if internal.MachineReadable {
jsonstring, err := json.Marshal(allStats)
if err != nil {
log.Fatal(err)
} else {
fmt.Println(string(jsonstring))
}
return
}
if err := runTui(apps); err != nil {
log.Fatal(err)
}
},
}
var (
status bool
recipeFilter string
listAppServer string
)
func init() {
AppListCommand.Flags().BoolVarP(
&status,
i18n.G("status"),
i18n.G("S"),
false,
i18n.G("show app deployment status"),
)
AppListCommand.Flags().StringVarP(
&recipeFilter,
i18n.G("recipe"),
i18n.G("r"),
"",
i18n.G("show apps of a specific recipe"),
)
AppListCommand.RegisterFlagCompletionFunc(
i18n.G("recipe"),
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return autocomplete.RecipeNameComplete()
},
)
AppListCommand.Flags().BoolVarP(
&internal.MachineReadable,
i18n.G("machine"),
i18n.G("m"),
false,
i18n.G("print machinereadable output"),
)
AppListCommand.Flags().StringVarP(
&listAppServer,
i18n.G("server"),
i18n.G("s"),
"",
i18n.G("show apps of a specific server"),
)
AppListCommand.RegisterFlagCompletionFunc(
i18n.G("server"),
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return autocomplete.ServerNameComplete()
},
)
}
var listRenderStyle = lipgloss.NewStyle().Margin(1, 2)
type appMeta struct {
status string
version string
updates string
chaos string
}
type listItem struct {
title string
app appPkg.App
appMetadata appMeta
}
func (li listItem) Title() string { return li.title }
func (li listItem) Description() string {
return fmt.Sprintf(
"server: %s • recipe: %s • status: %s • version: %s • updates: %s • chaos: %s",
li.app.Server,
li.app.Recipe.Name,
li.appMetadata.status,
li.appMetadata.version,
li.appMetadata.updates,
li.appMetadata.chaos,
)
}
func (li listItem) FilterValue() string {
return fmt.Sprintf(
"%s %s %s %s %s %s %s",
li.app.Domain,
li.app.Server,
li.app.Recipe.Name,
li.appMetadata.status,
li.appMetadata.version,
li.appMetadata.updates,
li.appMetadata.chaos,
)
}
type model struct {
apps []appPkg.App
list list.Model
initStatusGather bool
pollingStatus bool
err error
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "s":
if !m.pollingStatus {
m.pollingStatus = true
cmds = append(cmds, m.list.NewStatusMessage("Looking up app statuses 🧐"))
cmds = append(cmds, func() tea.Msg { return getAppsDeployStatus(m) })
} else {
cmds = append(cmds, m.list.NewStatusMessage("In progress, please hold ✋"))
}
case "q":
return m, tea.Quit
}
case appsDeployStatusMsg:
m.pollingStatus = false
cmds = append(cmds, m.list.SetItems(renderAppsDeployStatus(m, msg)))
case tea.WindowSizeMsg:
h, v := listRenderStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
}
m.list, cmd = m.list.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m model) View() string {
return listRenderStyle.Render(m.list.View())
}
type listKeyMap struct {
status key.Binding
}
func newListKeyMap() *listKeyMap {
return &listKeyMap{
status: key.NewBinding(
key.WithKeys("s"),
key.WithHelp("s", "status"),
),
}
}
type appsDeployStatusMsg map[string]map[string]string
func getAppsDeployStatus(m model) tea.Msg {
var apps []appPkg.App
for _, li := range m.list.VisibleItems() {
i := li.(listItem) // NOTE(d1): convert back to our custom item
apps = append(apps, i.app)
}
statuses, err := appPkg.GetAppStatuses(apps, true)
if err != nil {
errMsg := fmt.Sprintf("ERROR: %s", err.Error())
return func() tea.Msg { return m.list.NewStatusMessage(errMsg) }
}
catl, err := recipe.ReadRecipeCatalogue(true)
if err != nil {
errMsg := fmt.Sprintf("ERROR: %s", err.Error())
return func() tea.Msg { return m.list.NewStatusMessage(errMsg) }
}
for _, app := range apps {
var newUpdates []string
if status, ok := statuses[app.StackName()]; ok {
if version, ok := status["version"]; ok {
updates, err := recipe.GetRecipeCatalogueVersions(app.Recipe.Name, catl)
if err != nil {
errMsg := fmt.Sprintf("ERROR: %s", err.Error())
return func() tea.Msg { return m.list.NewStatusMessage(errMsg) }
}
parsedVersion, err := tagcmp.Parse(version)
if err != nil {
continue
}
for _, update := range updates {
parsedUpdate, err := tagcmp.Parse(update)
if err != nil {
continue
}
if update != version && parsedUpdate.IsGreaterThan(parsedVersion) {
newUpdates = append(newUpdates, update)
}
}
if len(newUpdates) != 0 {
statuses[app.StackName()]["updates"] = strings.Join(newUpdates, "\n")
}
}
}
}
return appsDeployStatusMsg(statuses)
}
func renderAppsDeployStatus(m model, appStatuses appsDeployStatusMsg) []list.Item {
itemsWithStatus := make(map[string]list.Item)
for _, li := range m.list.VisibleItems() {
i := li.(listItem) // NOTE(d1): convert back to our custom item
appStatus := appStatuses[i.app.StackName()]
var (
appVersion = appStatus["version"]
appUpdates = appStatus["updates"]
appDeploymentStatus = appStatus["status"]
appChaosVersion = appStatus["chaosVersion"]
)
if appVersion == "" {
appVersion = "-"
}
if appDeploymentStatus == "" || appDeploymentStatus == config.UNKNOWN_DEFAULT {
appDeploymentStatus = "-"
}
if appChaosVersion == "" {
appChaosVersion = "-"
}
if len(appUpdates) == 0 {
appUpdates = "-"
} else {
appUpdates = fmt.Sprintf("%d", len(appUpdates))
}
newMetadata := appMeta{
status: appDeploymentStatus,
version: appVersion,
updates: appUpdates,
chaos: appChaosVersion,
}
itemsWithStatus[i.title] = listItem{
title: i.title,
app: i.app,
appMetadata: newMetadata,
}
}
var items []list.Item
for _, li := range m.list.Items() {
i := li.(listItem) // NOTE(d1): convert back to our custom item
if is, ok := itemsWithStatus[i.title]; ok {
items = append(items, is)
continue
}
items = append(items, i)
}
return items
}
func runTui(apps []appPkg.App) error {
var items []list.Item
sort.Sort(appPkg.ByServerAndDomain(apps))
for _, app := range apps {
items = append(items, listItem{
title: app.Domain,
app: app,
appMetadata: appMeta{
status: "-",
version: "-",
updates: "-",
chaos: "-",
},
})
}
// NOTE(d1): disable standard styles on filter match since we
// hack the description to also become a target
delegate := list.NewDefaultDelegate()
delegateStyles := list.NewDefaultItemStyles()
delegateStyles.FilterMatch = delegateStyles.NormalTitle
delegate.Styles = delegateStyles
l := list.New(items, delegate, 0, 0)
l.Title = config.ABRA_DIR
l.StatusMessageLifetime = time.Second * 3
l.SetStatusBarItemName("app", "apps")
listKeys := newListKeyMap()
l.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
listKeys.status,
}
}
m := model{
apps: apps,
list: l,
initStatusGather: status,
}
p := tea.NewProgram(m, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
return fmt.Errorf("oops, app list tui exploded: %s", err)
}
return nil
}