intake/cmd/info.go

90 lines
2.0 KiB
Go

package cmd
import (
"database/sql"
"fmt"
"os"
"runtime/debug"
"github.com/Jaculabilis/intake/core"
"github.com/spf13/cobra"
)
var infoCmd = &cobra.Command{
Use: "info [--build]",
Short: "Print intake config",
Long: `Print intake config as it would be used by other commands.
`,
Run: func(cmd *cobra.Command, args []string) {
info(boolArg(cmd, "build"))
},
DisableFlagsInUseLine: true,
}
func init() {
rootCmd.AddCommand(infoCmd)
infoCmd.Flags().Bool("build", false, "Include exact build settings")
}
func info(buildInfo bool) {
exe, err := os.Executable()
if err != nil {
fmt.Printf("intake: error: %v\n", err)
} else {
fmt.Printf("intake: %s\n", exe)
}
fmt.Printf("Revision: %s\n", core.GetRevInfo())
fmt.Printf("Data path: %s\n", dataPath)
fmt.Printf("Database: %s\n", core.DatabasePath(dataPath))
db := openDb()
row := db.QueryRow("select exists(select 1 from password)")
var hasPassword bool
if err := row.Scan(&hasPassword); err != nil && err != sql.ErrNoRows {
fmt.Printf("Password: error: %v\n", err)
} else {
if hasPassword {
fmt.Printf("Password: yes\n")
} else {
fmt.Printf("Password: no\n")
}
}
row = db.QueryRow("select count(*) from sources")
var sources int
if err := row.Scan(&sources); err != nil {
fmt.Printf("Sources: error: %v\n", err)
} else {
fmt.Printf("Sources: %d\n", sources)
}
row = db.QueryRow("select count(*) from channels")
var channels int
if err := row.Scan(&channels); err != nil {
fmt.Printf("Channels: error: %v\n", err)
} else {
fmt.Printf("Channels: %d\n", channels)
}
row = db.QueryRow("select count(*) from items")
var items int
if err := row.Scan(&items); err != nil {
fmt.Printf("Items: error: %v\n", err)
} else {
fmt.Printf("Items: %d\n", items)
}
if buildInfo {
fmt.Printf("Build settings:\n")
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
fmt.Printf(" %s=%s\n", setting.Key, setting.Value)
}
}
}
}