89 lines
1.8 KiB
Go
89 lines
1.8 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/Jaculabilis/intake/core"
|
|
"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)
|
|
}
|
|
}
|
|
|
|
var dbPath string
|
|
|
|
func init() {
|
|
// Disable the automatic help command
|
|
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
|
|
|
|
// All commands need to operate on a database
|
|
rootCmd.PersistentFlags().StringVarP(&dbPath, "db", "d", "", "Path to the intake sqlite database (default: INTAKE_DB)")
|
|
}
|
|
|
|
func getDbPath() string {
|
|
if dbPath != "" {
|
|
return dbPath
|
|
}
|
|
env := os.Getenv("INTAKE_DB")
|
|
if env != "" {
|
|
return env
|
|
}
|
|
fmt.Println("error: No database specified. Either --db or INTAKE_DB must be set.")
|
|
os.Exit(1)
|
|
return ""
|
|
}
|
|
|
|
// Attempt to open the specified database and exit with an error if it fails.
|
|
func openDb() *core.DB {
|
|
db, err := core.OpenDb(getDbPath())
|
|
if err != nil {
|
|
log.Fatalf("error: Failed to open %s", dbPath)
|
|
}
|
|
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 {
|
|
log.Fatalf("error: Failed to init database: %v", err)
|
|
}
|
|
if err := core.MigrateDatabase(db); err != nil {
|
|
log.Fatalf("error: Failed to migrate database: %v", err)
|
|
}
|
|
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)
|
|
}
|