WIP: app list command sorting
This commit is contained in:
parent
337d3e9ae1
commit
01cbee824a
2
TODO.md
2
TODO.md
@ -12,7 +12,7 @@ Disclaimer!: List is WIP
|
|||||||
- [ ] `init`
|
- [ ] `init`
|
||||||
- [ ] `apps`
|
- [ ] `apps`
|
||||||
- [ ] `abra app`
|
- [ ] `abra app`
|
||||||
- [ ] `ls`
|
- [ ] `ls` (XXX: in progress (decentral1se))
|
||||||
- [ ] `new`
|
- [ ] `new`
|
||||||
- [ ] `backup`
|
- [ ] `backup`
|
||||||
- [ ] `deploy`
|
- [ ] `deploy`
|
||||||
|
21
cli/app.go
21
cli/app.go
@ -3,6 +3,7 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"coopcloud.tech/abra/client"
|
"coopcloud.tech/abra/client"
|
||||||
@ -51,29 +52,25 @@ var appRestoreCommand = &cli.Command{
|
|||||||
var appListCommand = &cli.Command{
|
var appListCommand = &cli.Command{
|
||||||
Name: "list",
|
Name: "list",
|
||||||
Aliases: []string{"ls"},
|
Aliases: []string{"ls"},
|
||||||
Flags: []cli.Flag{StatusFlag, ServerFlag, TypeFlag},
|
Flags: []cli.Flag{StatusFlag, ServerFlag, TypeFlag}, // FIXME: implement flags
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
// FIXME: Needs status flag implementing
|
appFiles, err := config.LoadAppFiles(Server)
|
||||||
// TODO: Sorting of output to make servers in alphabetical
|
|
||||||
// Looks like sorting a 2d slice of strings might be messy though
|
|
||||||
apps, err := config.LoadAppFiles(Server)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
tableCol := []string{"Name", "Type", "Server"}
|
apps, err := config.GetApps(appFiles)
|
||||||
|
sort.Sort(config.ByServer(apps))
|
||||||
|
tableCol := []string{"Server", "Type", "Name"}
|
||||||
table := createTable(tableCol)
|
table := createTable(tableCol)
|
||||||
for name, appFile := range apps {
|
for _, app := range apps {
|
||||||
app, err := config.GetApp(apps, name)
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err.Error())
|
|
||||||
}
|
|
||||||
if app.Type == Type || Type == "" {
|
if app.Type == Type || Type == "" {
|
||||||
// If type flag is set, check for it, if not, Type == ""
|
// If type flag is set, check for it, if not, Type == ""
|
||||||
tableRow := []string{name, app.Type, appFile.Server}
|
tableRow := []string{app.File.Server, app.Type, app.Name}
|
||||||
table.Append(tableRow)
|
table.Append(tableRow)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
table.SetAutoMergeCells(true)
|
||||||
table.Render()
|
table.Render()
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
@ -29,7 +29,33 @@ type App struct {
|
|||||||
Type string
|
Type string
|
||||||
Domain string
|
Domain string
|
||||||
Env AppEnv
|
Env AppEnv
|
||||||
|
File AppFile
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ByServer []App
|
||||||
|
|
||||||
|
func (a ByServer) Len() int { return len(a) }
|
||||||
|
func (a ByServer) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
func (a ByServer) Less(i, j int) bool {
|
||||||
|
return strings.ToLower(a[i].File.Server) < strings.ToLower(a[j].File.Server)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ByType []App
|
||||||
|
|
||||||
|
func (a ByType) Len() int { return len(a) }
|
||||||
|
func (a ByType) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
func (a ByType) Less(i, j int) bool {
|
||||||
|
return strings.ToLower(a[i].Type) < strings.ToLower(a[j].Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ByName []App
|
||||||
|
|
||||||
|
func (a ByName) Len() int { return len(a) }
|
||||||
|
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
func (a ByName) Less(i, j int) bool {
|
||||||
|
return strings.ToLower(a[i].Name) < strings.ToLower(a[j].Name)
|
||||||
|
}
|
||||||
|
|
||||||
type AppFile struct {
|
type AppFile struct {
|
||||||
Path string
|
Path string
|
||||||
Server string
|
Server string
|
||||||
@ -81,6 +107,18 @@ func GetApp(apps AppFiles, name AppName) (App, error) {
|
|||||||
return app, nil
|
return app, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetApps(appFiles AppFiles) ([]App, error) {
|
||||||
|
var apps []App
|
||||||
|
for name := range appFiles {
|
||||||
|
app, err := GetApp(appFiles, name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
apps = append(apps, app)
|
||||||
|
}
|
||||||
|
return apps, nil
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: maybe better names than read and get
|
// TODO: maybe better names than read and get
|
||||||
|
|
||||||
func readAppFile(appFile AppFile, name AppName) (App, error) {
|
func readAppFile(appFile AppFile, name AppName) (App, error) {
|
||||||
@ -88,7 +126,7 @@ func readAppFile(appFile AppFile, name AppName) (App, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return App{}, fmt.Errorf("env file for '%s' couldn't be read: %s", name, err.Error())
|
return App{}, fmt.Errorf("env file for '%s' couldn't be read: %s", name, err.Error())
|
||||||
}
|
}
|
||||||
app, err := makeApp(env, name)
|
app, err := makeApp(env, name, appFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return App{}, fmt.Errorf("env file for '%s' has issues: %s", name, err.Error())
|
return App{}, fmt.Errorf("env file for '%s' has issues: %s", name, err.Error())
|
||||||
}
|
}
|
||||||
@ -104,7 +142,7 @@ func readEnv(filePath string) (AppEnv, error) {
|
|||||||
return envFile, nil
|
return envFile, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeApp(env AppEnv, name string) (App, error) {
|
func makeApp(env AppEnv, name string, appFile AppFile) (App, error) {
|
||||||
// Checking for type as it is required - apps wont work without it
|
// Checking for type as it is required - apps wont work without it
|
||||||
domain := env["DOMAIN"]
|
domain := env["DOMAIN"]
|
||||||
apptype, ok := env["TYPE"]
|
apptype, ok := env["TYPE"]
|
||||||
@ -116,6 +154,7 @@ func makeApp(env AppEnv, name string) (App, error) {
|
|||||||
Domain: domain,
|
Domain: domain,
|
||||||
Type: apptype,
|
Type: apptype,
|
||||||
Env: env,
|
Env: env,
|
||||||
|
File: appFile,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user