Add more information to CLI help text
This commit is contained in:
parent
b7683f6805
commit
9a77beb582
@ -45,7 +45,7 @@ func actionAdd(argv []string) {
|
|||||||
|
|
||||||
err := core.AddAction(db, actionAddSource, actionAddAction, argv)
|
err := core.AddAction(db, actionAddSource, actionAddAction, argv)
|
||||||
if err != nil {
|
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)
|
log.Printf("Added action %s to source %s", actionAddAction, actionAddSource)
|
||||||
|
@ -43,7 +43,7 @@ func actionDelete() {
|
|||||||
|
|
||||||
err := core.DeleteAction(db, actionDeleteSource, actionDeleteAction)
|
err := core.DeleteAction(db, actionDeleteSource, actionDeleteAction)
|
||||||
if err != nil {
|
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)
|
log.Printf("Deleted action %s from source %s", actionDeleteAction, actionDeleteSource)
|
||||||
|
@ -45,7 +45,7 @@ func actionEdit(argv []string) {
|
|||||||
|
|
||||||
err := core.UpdateAction(db, actionEditSource, actionEditAction, argv)
|
err := core.UpdateAction(db, actionEditSource, actionEditAction, argv)
|
||||||
if err != nil {
|
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)
|
log.Printf("Updated action %s on source %s", actionEditAction, actionEditSource)
|
||||||
|
14
cmd/feed.go
14
cmd/feed.go
@ -11,14 +11,10 @@ import (
|
|||||||
var feedCmd = &cobra.Command{
|
var feedCmd = &cobra.Command{
|
||||||
Use: "feed",
|
Use: "feed",
|
||||||
Short: "Display the item 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".
|
The default format is "headlines".
|
||||||
|
|
||||||
Available formats:
|
%s`, makeFormatHelpText()),
|
||||||
headlines Only item titles
|
|
||||||
json Full item JSON
|
|
||||||
short Item source and id
|
|
||||||
`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
feed()
|
feed()
|
||||||
},
|
},
|
||||||
@ -40,14 +36,12 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func feed() {
|
func feed() {
|
||||||
formatter, err := core.FormatAs(feedFormat)
|
formatter := formatAs(feedFormat)
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
db := openAndMigrateDb()
|
db := openAndMigrateDb()
|
||||||
|
|
||||||
var items []core.Item
|
var items []core.Item
|
||||||
|
var err error
|
||||||
if feedSource != "" {
|
if feedSource != "" {
|
||||||
if feedShowInactive {
|
if feedShowInactive {
|
||||||
items, err = core.GetAllItemsForSource(db, feedSource)
|
items, err = core.GetAllItemsForSource(db, feedSource)
|
||||||
|
29
cmd/root.go
29
cmd/root.go
@ -33,6 +33,10 @@ func init() {
|
|||||||
rootCmd.PersistentFlags().StringVarP(&dbPath, "db", "d", "", "Path to the intake sqlite database (default: INTAKE_DB)")
|
rootCmd.PersistentFlags().StringVarP(&dbPath, "db", "d", "", "Path to the intake sqlite database (default: INTAKE_DB)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Common logic shared by multiple commands
|
||||||
|
//
|
||||||
|
|
||||||
func getDbPath() string {
|
func getDbPath() string {
|
||||||
if dbPath != "" {
|
if dbPath != "" {
|
||||||
return dbPath
|
return dbPath
|
||||||
@ -41,7 +45,8 @@ func getDbPath() string {
|
|||||||
if env != "" {
|
if env != "" {
|
||||||
return 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)
|
os.Exit(1)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -50,7 +55,7 @@ func getDbPath() string {
|
|||||||
func openDb() *core.DB {
|
func openDb() *core.DB {
|
||||||
db, err := core.OpenDb(getDbPath())
|
db, err := core.OpenDb(getDbPath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error: Failed to open %s", dbPath)
|
log.Fatalf("error: failed to open %s", dbPath)
|
||||||
}
|
}
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
@ -59,10 +64,10 @@ func openDb() *core.DB {
|
|||||||
func openAndMigrateDb() *core.DB {
|
func openAndMigrateDb() *core.DB {
|
||||||
db := openDb()
|
db := openDb()
|
||||||
if err := core.InitDatabase(db); err != nil {
|
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 {
|
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
|
return db
|
||||||
}
|
}
|
||||||
@ -86,3 +91,19 @@ func actionSort(a string, b string) int {
|
|||||||
}
|
}
|
||||||
return strings.Compare(a, b)
|
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
|
||||||
|
}
|
||||||
|
@ -34,7 +34,7 @@ func sourceAdd() {
|
|||||||
db := openAndMigrateDb()
|
db := openAndMigrateDb()
|
||||||
|
|
||||||
if err := core.AddSource(db, sourceAddSource); err != nil {
|
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)
|
log.Printf("Added source %s", sourceAddSource)
|
||||||
|
@ -34,7 +34,7 @@ func sourceDelete() {
|
|||||||
db := openAndMigrateDb()
|
db := openAndMigrateDb()
|
||||||
|
|
||||||
if err := core.DeleteSource(db, sourceDeleteSource); err != nil {
|
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)
|
log.Printf("Deleted source %s", sourceDeleteSource)
|
||||||
|
@ -12,13 +12,16 @@ import (
|
|||||||
var sourceFetchCmd = &cobra.Command{
|
var sourceFetchCmd = &cobra.Command{
|
||||||
Use: "fetch",
|
Use: "fetch",
|
||||||
Short: "Fetch items for a source and update the feed",
|
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.
|
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
|
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
|
and the exit code is 0. No changes will be made to the source if the fetch
|
||||||
does not succeed.
|
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) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
sourceFetch()
|
sourceFetch()
|
||||||
},
|
},
|
||||||
@ -39,10 +42,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sourceFetch() {
|
func sourceFetch() {
|
||||||
formatter, err := core.FormatAs(sourceFetchFormat)
|
formatter := formatAs(sourceFetchFormat)
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
db := openAndMigrateDb()
|
db := openAndMigrateDb()
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ func sourceFetch() {
|
|||||||
|
|
||||||
items, err := core.Execute(sourceFetchSource, argv, nil, "", time.Minute)
|
items, err := core.Execute(sourceFetchSource, argv, nil, "", time.Minute)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error: %v", err)
|
log.Fatalf("error: failed to execute fetch: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if sourceFetchDryRun {
|
if sourceFetchDryRun {
|
||||||
@ -66,7 +66,7 @@ func sourceFetch() {
|
|||||||
|
|
||||||
added, deleted, err := core.UpdateWithFetchedItems(db, sourceFetchSource, items)
|
added, deleted, err := core.UpdateWithFetchedItems(db, sourceFetchSource, items)
|
||||||
if err != nil {
|
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)
|
log.Printf("%s added %d items, updated %d items, and deleted %d items", sourceFetchSource, added, len(items)-added, deleted)
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ func sourceList() {
|
|||||||
|
|
||||||
names, err := core.GetSources(db)
|
names, err := core.GetSources(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error: %v", err)
|
log.Fatalf("error: failed to get sources: %v", err)
|
||||||
}
|
}
|
||||||
slices.Sort(names)
|
slices.Sort(names)
|
||||||
|
|
||||||
|
@ -12,9 +12,9 @@ import (
|
|||||||
var sourceTestCmd = &cobra.Command{
|
var sourceTestCmd = &cobra.Command{
|
||||||
Use: "test [flags] -- argv",
|
Use: "test [flags] -- argv",
|
||||||
Short: "Test a fetch action",
|
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) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
l := cmd.Flags().ArgsLenAtDash()
|
l := cmd.Flags().ArgsLenAtDash()
|
||||||
if l == -1 {
|
if l == -1 {
|
||||||
@ -36,10 +36,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sourceTest(cmd []string) {
|
func sourceTest(cmd []string) {
|
||||||
formatter, err := core.FormatAs(testFormat)
|
formatter := formatAs(testFormat)
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
items, err := core.Execute("", cmd, testEnv, "", time.Minute)
|
items, err := core.Execute("", cmd, testEnv, "", time.Minute)
|
||||||
log.Printf("Returned %d items", len(items))
|
log.Printf("Returned %d items", len(items))
|
||||||
|
@ -132,7 +132,7 @@ func Execute(
|
|||||||
log.Printf("Executing %v", argv)
|
log.Printf("Executing %v", argv)
|
||||||
|
|
||||||
if len(argv) == 0 {
|
if len(argv) == 0 {
|
||||||
return nil, errors.New("error: empty argv")
|
return nil, errors.New("empty argv")
|
||||||
}
|
}
|
||||||
|
|
||||||
env = append(env, "STATE_PATH=")
|
env = append(env, "STATE_PATH=")
|
||||||
|
@ -55,3 +55,9 @@ func FormatAs(format string) (func(item Item) string, error) {
|
|||||||
return nil, fmt.Errorf("invalid format '%s'", format)
|
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",
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user