intake/cmd/root.go

110 lines
2.4 KiB
Go
Raw Normal View History

2025-01-16 15:55:38 +00:00
package cmd
import (
"fmt"
2025-01-23 16:36:25 +00:00
"log"
2025-01-16 15:55:38 +00:00
"os"
"strings"
2025-01-16 15:55:38 +00:00
2025-01-23 16:36:25 +00:00
"github.com/Jaculabilis/intake/core"
2025-01-16 15:55:38 +00:00
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "intake",
Short: "Universal and extensible feed aggregator",
Long: `intake, the universal and extensible feed aggregator`,
}
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
2025-01-28 05:54:46 +00:00
var dataPath string
2025-01-16 15:55:38 +00:00
func init() {
// Disable the automatic help command
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
// All commands need to operate on a database
2025-01-28 05:54:46 +00:00
rootCmd.PersistentFlags().StringVarP(&dataPath, "data-dir", "d", "", "Path to the intake data directory containing the database")
}
2025-01-23 21:22:38 +00:00
//
// Common logic shared by multiple commands
//
func getDbPath() string {
2025-01-28 05:54:46 +00:00
if dataPath != "" {
return core.DatabasePath(dataPath)
}
2025-01-28 05:54:46 +00:00
if dataDir := core.ResolveDataDir(); dataDir != "" {
return core.DatabasePath(dataDir)
}
2025-01-23 21:22:38 +00:00
fmt.Println("error: no database specified")
2025-01-28 05:54:46 +00:00
fmt.Println("One of --data-dir, INTAKE_DATA_DIR, XDG_DATA_HOME, or HOME must be defined.")
os.Exit(1)
return ""
2025-01-16 15:55:38 +00:00
}
2025-01-23 16:36:25 +00:00
// Attempt to open the specified database and exit with an error if it fails.
func openDb() *core.DB {
2025-01-28 05:54:46 +00:00
dbPath := getDbPath()
db, err := core.OpenDb(dbPath)
2025-01-23 16:36:25 +00:00
if err != nil {
2025-01-23 21:22:38 +00:00
log.Fatalf("error: failed to open %s", dbPath)
2025-01-23 16:36:25 +00:00
}
return db
}
// Attempt to open and migrate the specified database and exit with an error if it fails.
func openAndMigrateDb() *core.DB {
db := openDb()
if err := core.InitDatabase(db); err != nil {
2025-01-23 21:22:38 +00:00
log.Fatalf("error: failed to init database: %v", err)
2025-01-23 16:36:25 +00:00
}
if err := core.MigrateDatabase(db); err != nil {
2025-01-23 21:22:38 +00:00
log.Fatalf("error: failed to migrate database: %v", err)
2025-01-23 16:36:25 +00:00
}
return db
}
func getArgv(cmd *cobra.Command, args []string) []string {
lenAtDash := cmd.Flags().ArgsLenAtDash()
if lenAtDash == -1 {
return nil
} else {
return args[lenAtDash:]
}
}
// Sort "fetch" action ahead of other actions
func actionSort(a string, b string) int {
if a == "fetch" {
return -1
}
if b == "fetch" {
return 1
}
return strings.Compare(a, b)
}
2025-01-23 21:22:38 +00:00
func makeFormatHelpText() string {
text := "Available formats:\n"
for format, desc := range core.AvailableFormats {
text += fmt.Sprintf(" %-13s %s\n", format, desc)
}
return text
}
func formatAs(format string) func(item core.Item) string {
formatter, err := core.FormatAs(format)
if err != nil {
log.Fatalf("error: %v", err)
}
return formatter
}