Add more information to CLI help text

This commit is contained in:
Tim Van Baak 2025-01-23 13:22:38 -08:00
parent b7683f6805
commit 9a77beb582
12 changed files with 53 additions and 35 deletions

View File

@ -45,7 +45,7 @@ func actionAdd(argv []string) {
err := core.AddAction(db, actionAddSource, actionAddAction, argv)
if err != nil {
log.Fatalf("error: %v", err)
log.Fatalf("error: failed to add action: %v", err)
}
log.Printf("Added action %s to source %s", actionAddAction, actionAddSource)

View File

@ -43,7 +43,7 @@ func actionDelete() {
err := core.DeleteAction(db, actionDeleteSource, actionDeleteAction)
if err != nil {
log.Fatalf("error: %v", err)
log.Fatalf("error: failed to delete action: %v", err)
}
log.Printf("Deleted action %s from source %s", actionDeleteAction, actionDeleteSource)

View File

@ -45,7 +45,7 @@ func actionEdit(argv []string) {
err := core.UpdateAction(db, actionEditSource, actionEditAction, argv)
if err != nil {
log.Fatalf("error: %v", err)
log.Fatalf("error: failed to update action: %v", err)
}
log.Printf("Updated action %s on source %s", actionEditAction, actionEditSource)

View File

@ -11,14 +11,10 @@ import (
var feedCmd = &cobra.Command{
Use: "feed",
Short: "Display the item feed",
Long: `Display the intake item feed in various formats.
Long: fmt.Sprintf(`Display the intake item feed in various formats.
The default format is "headlines".
Available formats:
headlines Only item titles
json Full item JSON
short Item source and id
`,
%s`, makeFormatHelpText()),
Run: func(cmd *cobra.Command, args []string) {
feed()
},
@ -40,14 +36,12 @@ func init() {
}
func feed() {
formatter, err := core.FormatAs(feedFormat)
if err != nil {
log.Fatal(err)
}
formatter := formatAs(feedFormat)
db := openAndMigrateDb()
var items []core.Item
var err error
if feedSource != "" {
if feedShowInactive {
items, err = core.GetAllItemsForSource(db, feedSource)

View File

@ -33,6 +33,10 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&dbPath, "db", "d", "", "Path to the intake sqlite database (default: INTAKE_DB)")
}
//
// Common logic shared by multiple commands
//
func getDbPath() string {
if dbPath != "" {
return dbPath
@ -41,7 +45,8 @@ func getDbPath() string {
if env != "" {
return env
}
fmt.Println("error: No database specified. Either --db or INTAKE_DB must be set.")
fmt.Println("error: no database specified")
fmt.Println("Either --db or the environment variable INTAKE_DB must be set.")
os.Exit(1)
return ""
}
@ -50,7 +55,7 @@ func getDbPath() string {
func openDb() *core.DB {
db, err := core.OpenDb(getDbPath())
if err != nil {
log.Fatalf("error: Failed to open %s", dbPath)
log.Fatalf("error: failed to open %s", dbPath)
}
return db
}
@ -59,10 +64,10 @@ func openDb() *core.DB {
func openAndMigrateDb() *core.DB {
db := openDb()
if err := core.InitDatabase(db); err != nil {
log.Fatalf("error: Failed to init database: %v", err)
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)
log.Fatalf("error: failed to migrate database: %v", err)
}
return db
}
@ -86,3 +91,19 @@ func actionSort(a string, b string) int {
}
return strings.Compare(a, b)
}
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
}

View File

@ -34,7 +34,7 @@ func sourceAdd() {
db := openAndMigrateDb()
if err := core.AddSource(db, sourceAddSource); err != nil {
log.Fatalf("error: %v", err)
log.Fatalf("error: failed to add source: %v", err)
}
log.Printf("Added source %s", sourceAddSource)

View File

@ -34,7 +34,7 @@ func sourceDelete() {
db := openAndMigrateDb()
if err := core.DeleteSource(db, sourceDeleteSource); err != nil {
log.Fatalf("error: %v", err)
log.Fatalf("error: failed to delete source: %v", err)
}
log.Printf("Deleted source %s", sourceDeleteSource)

View File

@ -12,13 +12,16 @@ import (
var sourceFetchCmd = &cobra.Command{
Use: "fetch",
Short: "Fetch items for a source and update the feed",
Long: `Fetch items from a feed source using the configured "fetch" action.
Long: fmt.Sprintf(`Fetch items from a feed source using the configured "fetch" action.
Items returned by a successful fetch will be used to update the source.
A fetch is successful if all items output by the fetch are parsed successfully
and the exit code is 0. No changes will be made to the source if the fetch
does not succeed.
In a dry run, the items will be printed according to the chosen format.`,
In a dry run, the items will be printed according to the chosen format and
the source will not be updated with the fetch result.
%s`, makeFormatHelpText()),
Run: func(cmd *cobra.Command, args []string) {
sourceFetch()
},
@ -39,10 +42,7 @@ func init() {
}
func sourceFetch() {
formatter, err := core.FormatAs(sourceFetchFormat)
if err != nil {
log.Fatalf("error: %v", err)
}
formatter := formatAs(sourceFetchFormat)
db := openAndMigrateDb()
@ -53,7 +53,7 @@ func sourceFetch() {
items, err := core.Execute(sourceFetchSource, argv, nil, "", time.Minute)
if err != nil {
log.Fatalf("error: %v", err)
log.Fatalf("error: failed to execute fetch: %v", err)
}
if sourceFetchDryRun {
@ -66,7 +66,7 @@ func sourceFetch() {
added, deleted, err := core.UpdateWithFetchedItems(db, sourceFetchSource, items)
if err != nil {
log.Fatalf("error: %v", err)
log.Fatalf("error: failed to update: %v", err)
}
log.Printf("%s added %d items, updated %d items, and deleted %d items", sourceFetchSource, added, len(items)-added, deleted)
}

View File

@ -33,7 +33,7 @@ func sourceList() {
names, err := core.GetSources(db)
if err != nil {
log.Fatalf("error: %v", err)
log.Fatalf("error: failed to get sources: %v", err)
}
slices.Sort(names)

View File

@ -12,9 +12,9 @@ import (
var sourceTestCmd = &cobra.Command{
Use: "test [flags] -- argv",
Short: "Test a fetch action",
Long: `Execute a command as if it were a feed source's fetch action.
Long: fmt.Sprintf(`Execute a command as if it were a feed source's fetch action.
The display format of the returned items is the same as "intake feed".`,
%s`, makeFormatHelpText()),
Run: func(cmd *cobra.Command, args []string) {
l := cmd.Flags().ArgsLenAtDash()
if l == -1 {
@ -36,10 +36,7 @@ func init() {
}
func sourceTest(cmd []string) {
formatter, err := core.FormatAs(testFormat)
if err != nil {
log.Fatal(err)
}
formatter := formatAs(testFormat)
items, err := core.Execute("", cmd, testEnv, "", time.Minute)
log.Printf("Returned %d items", len(items))

View File

@ -132,7 +132,7 @@ func Execute(
log.Printf("Executing %v", argv)
if len(argv) == 0 {
return nil, errors.New("error: empty argv")
return nil, errors.New("empty argv")
}
env = append(env, "STATE_PATH=")

View File

@ -55,3 +55,9 @@ func FormatAs(format string) (func(item Item) string, error) {
return nil, fmt.Errorf("invalid format '%s'", format)
}
}
var AvailableFormats = map[string]string{
"headlines": "Only item titles",
"json": "Full item JSON",
"short": "Item source and id",
}