package server import ( "strings" "coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/context" "coopcloud.tech/abra/pkg/formatter" "github.com/docker/cli/cli/connhelper/ssh" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) var problemsFilter bool var problemsFilterFlag = &cli.BoolFlag{ Name: "problems, p", Usage: "Show only servers with potential connection problems", Destination: &problemsFilter, } var serverListCommand = cli.Command{ Name: "list", Aliases: []string{"ls"}, Usage: "List managed servers", Flags: []cli.Flag{ problemsFilterFlag, internal.DebugFlag, internal.MachineReadableFlag, internal.OfflineFlag, }, Before: internal.SubCommandBefore, Action: func(c *cli.Context) error { dockerContextStore := context.NewDefaultDockerContextStore() contexts, err := dockerContextStore.Store.List() if err != nil { logrus.Fatal(err) } tableColumns := []string{"name", "host", "user", "port"} table := formatter.CreateTable(tableColumns) serverNames, err := config.ReadServerNames() if err != nil { logrus.Fatal(err) } for _, serverName := range serverNames { var row []string for _, ctx := range contexts { endpoint, err := context.GetContextEndpoint(ctx) if err != nil && strings.Contains(err.Error(), "does not exist") { // No local context found, we can continue safely continue } if ctx.Name == serverName { sp, err := ssh.ParseURL(endpoint) if err != nil { logrus.Fatal(err) } if sp.Host == "" { sp.Host = "unknown" } if sp.User == "" { sp.User = "unknown" } if sp.Port == "" { sp.Port = "unknown" } row = []string{serverName, sp.Host, sp.User, sp.Port} } } if len(row) == 0 { if serverName == "default" { row = []string{serverName, "local", "n/a", "n/a"} } else { row = []string{serverName, "unknown", "unknown", "unknown"} } } if problemsFilter { for _, val := range row { if val == "unknown" { table.Append(row) break } } } else { table.Append(row) } } if internal.MachineReadable { table.JSONRender() } else { if problemsFilter && table.NumLines() == 0 { logrus.Info("all servers wired up correctly 👏") } else { table.Render() } } return nil }, }