gomanager/cmd/list.go
2023-12-01 23:02:33 +01:00

82 lines
1.9 KiB
Go

package cmd
import (
"context"
"fmt"
"runtime"
"code.icod.de/dalu/gomanager/ent"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
_ "github.com/mattn/go-sqlite3"
)
var (
colTitleIndex = "ID"
colTitleRootPath = "Root Path"
colTitleBinaryPath = "Binary"
colTitleServiceName = "Service Name"
colTitleUser = "User"
colTitleGroup = "Group"
colTitleBinaryTargetPath = "Target"
rowHeader = table.Row{
colTitleIndex,
colTitleRootPath,
colTitleBinaryPath,
colTitleServiceName,
colTitleUser,
colTitleGroup,
colTitleBinaryTargetPath,
}
)
// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Short: "Lists all watched projects",
RunE: func(cmd *cobra.Command, args []string) error {
runtime.GOMAXPROCS(1)
client, err := ent.Open("sqlite3", fmt.Sprintf("file:%s?mode=rwc&cache=private&_fk=1", sqliteFilename))
if err != nil {
return fmt.Errorf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
ms, e := client.Project.Query().All(context.Background())
if e != nil {
return e
}
tw := table.NewWriter()
tw.AppendHeader(rowHeader)
for _, m := range ms {
tw.AppendRow(table.Row{
m.ID,
m.RootPath,
m.BinaryPath,
m.ServiceName,
m.User,
m.Group,
m.BinaryTargetPath,
})
}
tw.SetIndexColumn(1)
tw.SetTitle("Watched Projects")
fmt.Println(tw.Render())
return nil
},
}
func init() {
rootCmd.AddCommand(listCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}