Reorganize CLI commands

This commit is contained in:
Tim Van Baak 2025-01-21 08:42:59 -08:00
parent fb0d4e9aee
commit cb161b4f91
23 changed files with 404 additions and 40 deletions

16
cmd/action.go Normal file
View File

@ -0,0 +1,16 @@
package cmd
import (
"github.com/spf13/cobra"
)
var actionCmd = &cobra.Command{
Use: "action",
Short: "Manage and run source actions",
Long: `
`,
}
func init() {
rootCmd.AddCommand(actionCmd)
}

21
cmd/actionAdd.go Normal file
View File

@ -0,0 +1,21 @@
package cmd
import (
"log"
"github.com/spf13/cobra"
)
var actionAddCmd = &cobra.Command{
Use: "add",
Short: "Add an action to a source",
Long: `
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
},
}
func init() {
actionCmd.AddCommand(actionAddCmd)
}

21
cmd/actionDelete.go Normal file
View File

@ -0,0 +1,21 @@
package cmd
import (
"log"
"github.com/spf13/cobra"
)
var actionDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete an action from a source",
Long: `
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
},
}
func init() {
actionCmd.AddCommand(actionDeleteCmd)
}

21
cmd/actionEdit.go Normal file
View File

@ -0,0 +1,21 @@
package cmd
import (
"log"
"github.com/spf13/cobra"
)
var actionEditCmd = &cobra.Command{
Use: "edit",
Short: "Edit an action",
Long: `
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
},
}
func init() {
actionCmd.AddCommand(actionEditCmd)
}

22
cmd/actionExecute.go Normal file
View File

@ -0,0 +1,22 @@
package cmd
import (
"log"
"github.com/spf13/cobra"
)
var actionExecuteCmd = &cobra.Command{
Use: "execute",
Aliases: []string{"exec"},
Short: "Run a source action for an item",
Long: `
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
},
}
func init() {
actionCmd.AddCommand(actionExecuteCmd)
}

16
cmd/channel.go Normal file
View File

@ -0,0 +1,16 @@
package cmd
import (
"github.com/spf13/cobra"
)
var channelCmd = &cobra.Command{
Use: "channel",
Short: "Manage channels",
Long: `
`,
}
func init() {
rootCmd.AddCommand(channelCmd)
}

21
cmd/channelAdd.go Normal file
View File

@ -0,0 +1,21 @@
package cmd
import (
"log"
"github.com/spf13/cobra"
)
var channelAddCmd = &cobra.Command{
Use: "add",
Short: "Create a channel",
Long: `
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
},
}
func init() {
channelCmd.AddCommand(channelAddCmd)
}

21
cmd/channelDelete.go Normal file
View File

@ -0,0 +1,21 @@
package cmd
import (
"log"
"github.com/spf13/cobra"
)
var channelDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete a channel",
Long: `
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
},
}
func init() {
channelCmd.AddCommand(channelDeleteCmd)
}

21
cmd/channelEdit.go Normal file
View File

@ -0,0 +1,21 @@
package cmd
import (
"log"
"github.com/spf13/cobra"
)
var channelEditCmd = &cobra.Command{
Use: "edit",
Short: "Edit a channel",
Long: `
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
},
}
func init() {
channelCmd.AddCommand(channelEditCmd)
}

View File

@ -10,7 +10,7 @@ import (
var feedCmd = &cobra.Command{
Use: "feed",
Short: "Print item feeds",
Short: "Display the item feed",
Long: `Display the intake item feed in various formats.
The default format is "headlines".

16
cmd/item.go Normal file
View File

@ -0,0 +1,16 @@
package cmd
import (
"github.com/spf13/cobra"
)
var itemCmd = &cobra.Command{
Use: "item",
Short: "Manage items",
Long: `Add, edit, or deactivate items.
`,
}
func init() {
rootCmd.AddCommand(itemCmd)
}

View File

@ -11,7 +11,7 @@ import (
"github.com/spf13/cobra"
)
var addCmd = &cobra.Command{
var itemAddCmd = &cobra.Command{
Use: "add",
Short: "Add an item",
Long: `Create an ad-hoc item in a source.
@ -19,7 +19,7 @@ var addCmd = &cobra.Command{
By default, the item is created in the "default" source, which is created
if it doesn't exist, with a random id.`,
Run: func(cmd *cobra.Command, args []string) {
add()
itemAdd()
},
}
@ -32,18 +32,18 @@ var addLink string
var addTime int
func init() {
rootCmd.AddCommand(addCmd)
itemCmd.AddCommand(itemAddCmd)
addCmd.Flags().StringVarP(&addSource, "source", "s", "", "Source in which to create the item (default: default)")
addCmd.Flags().StringVarP(&addId, "item", "i", "", "Item id (default: random hex)")
addCmd.Flags().StringVarP(&addTitle, "title", "t", "", "Item title")
addCmd.Flags().StringVarP(&addAuthor, "author", "a", "", "Item author")
addCmd.Flags().StringVarP(&addBody, "body", "b", "", "Item body")
addCmd.Flags().StringVarP(&addLink, "link", "l", "", "Item link")
addCmd.Flags().IntVarP(&addTime, "time", "m", 0, "Item time as a Unix timestamp")
itemAddCmd.Flags().StringVarP(&addSource, "source", "s", "", "Source in which to create the item (default: default)")
itemAddCmd.Flags().StringVarP(&addId, "item", "i", "", "Item id (default: random hex)")
itemAddCmd.Flags().StringVarP(&addTitle, "title", "t", "", "Item title")
itemAddCmd.Flags().StringVarP(&addAuthor, "author", "a", "", "Item author")
itemAddCmd.Flags().StringVarP(&addBody, "body", "b", "", "Item body")
itemAddCmd.Flags().StringVarP(&addLink, "link", "l", "", "Item link")
itemAddCmd.Flags().IntVarP(&addTime, "time", "m", 0, "Item time as a Unix timestamp")
}
func add() {
func itemAdd() {
// Default to "default" source
if addSource == "" {
addSource = "default"

View File

@ -9,15 +9,15 @@ import (
"github.com/spf13/cobra"
)
var deactivateCmd = &cobra.Command{
var itemDeactivateCmd = &cobra.Command{
Use: "deactivate",
Aliases: []string{"deac"},
Short: "Deactivate items",
Short: "Deactivate an item",
Long: `Deactivate items, hiding them from feeds and marking them for deletion.
Deactivation is idempotent.`,
Run: func(cmd *cobra.Command, args []string) {
deactivate()
itemDeactivate()
},
}
@ -25,15 +25,15 @@ var deacSource string
var deacItem string
func init() {
rootCmd.AddCommand(deactivateCmd)
itemCmd.AddCommand(itemDeactivateCmd)
deactivateCmd.Flags().StringVarP(&deacSource, "source", "s", "", "Source of the item")
deactivateCmd.MarkFlagRequired("source")
deactivateCmd.Flags().StringVarP(&deacItem, "item", "i", "", "Item id")
deactivateCmd.MarkFlagRequired("item")
itemDeactivateCmd.Flags().StringVarP(&deacSource, "source", "s", "", "Source of the item")
itemDeactivateCmd.MarkFlagRequired("source")
itemDeactivateCmd.Flags().StringVarP(&deacItem, "item", "i", "", "Item id")
itemDeactivateCmd.MarkFlagRequired("item")
}
func deactivate() {
func itemDeactivate() {
db, err := core.OpenDb(getDbPath())
if err != nil {
log.Fatalf("Failed to open %s", dbPath)

21
cmd/itemEdit.go Normal file
View File

@ -0,0 +1,21 @@
package cmd
import (
"log"
"github.com/spf13/cobra"
)
var itemEditCmd = &cobra.Command{
Use: "edit",
Short: "Edit an item",
Long: `
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
},
}
func init() {
itemCmd.AddCommand(itemEditCmd)
}

21
cmd/passwd.go Normal file
View File

@ -0,0 +1,21 @@
package cmd
import (
"log"
"github.com/spf13/cobra"
)
var passwdCmd = &cobra.Command{
Use: "passwd",
Short: "Set the password for the web interface",
Long: `
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
},
}
func init() {
rootCmd.AddCommand(passwdCmd)
}

21
cmd/serve.go Normal file
View File

@ -0,0 +1,21 @@
package cmd
import (
"log"
"github.com/spf13/cobra"
)
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Serve the web interface",
Long: `
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
},
}
func init() {
rootCmd.AddCommand(serveCmd)
}

19
cmd/source.go Normal file
View File

@ -0,0 +1,19 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"github.com/spf13/cobra"
)
var sourceCmd = &cobra.Command{
Use: "source",
Short: "Manage sources",
Long: `
`,
}
func init() {
rootCmd.AddCommand(sourceCmd)
}

21
cmd/sourceAdd.go Normal file
View File

@ -0,0 +1,21 @@
package cmd
import (
"log"
"github.com/spf13/cobra"
)
var sourceAddCmd = &cobra.Command{
Use: "add",
Short: "Create a source",
Long: `
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
},
}
func init() {
sourceCmd.AddCommand(sourceAddCmd)
}

21
cmd/sourceDeactivate.go Normal file
View File

@ -0,0 +1,21 @@
package cmd
import (
"log"
"github.com/spf13/cobra"
)
var sourceDeactivateCmd = &cobra.Command{
Use: "deactivate",
Short: "Deactivate all items in a source",
Long: `
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
},
}
func init() {
sourceCmd.AddCommand(sourceDeactivateCmd)
}

21
cmd/sourceDelete.go Normal file
View File

@ -0,0 +1,21 @@
package cmd
import (
"log"
"github.com/spf13/cobra"
)
var sourceDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete a source",
Long: `
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
},
}
func init() {
sourceCmd.AddCommand(sourceDeleteCmd)
}

21
cmd/sourceEdit.go Normal file
View File

@ -0,0 +1,21 @@
package cmd
import (
"log"
"github.com/spf13/cobra"
)
var sourceEditCmd = &cobra.Command{
Use: "edit",
Short: "Edit a source",
Long: `
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
},
}
func init() {
sourceCmd.AddCommand(sourceEditCmd)
}

View File

@ -9,14 +9,16 @@ import (
"github.com/spf13/cobra"
)
var updateCmd = &cobra.Command{
Use: "update",
Short: "Fetch items for a source and update it",
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.
Items returned by a successful fetch will be used to update the source.
No changes will be made to the source if the fetch does not succeed.`,
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.`,
Run: func(cmd *cobra.Command, args []string) {
update()
sourceFetch()
},
}
@ -24,15 +26,15 @@ var source string
var dryRun bool
func init() {
rootCmd.AddCommand(updateCmd)
sourceCmd.AddCommand(sourceFetchCmd)
updateCmd.Flags().StringVarP(&source, "source", "s", "", "Source name to fetch (required)")
updateCmd.MarkFlagRequired("source")
sourceFetchCmd.Flags().StringVarP(&source, "source", "s", "", "Source name to fetch (required)")
sourceFetchCmd.MarkFlagRequired("source")
updateCmd.Flags().BoolVar(&dryRun, "dry-run", false, "Instead of updating the source, print the fetched items")
sourceFetchCmd.Flags().BoolVar(&dryRun, "dry-run", false, "Instead of updating the source, print the fetched items")
}
func update() {
func sourceFetch() {
fmt.Printf("Hello %s\n", source)
if dryRun {

View File

@ -9,18 +9,18 @@ import (
"github.com/spf13/cobra"
)
var testCmd = &cobra.Command{
Use: "test [flags] -- command",
Short: "Test a feed source",
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.
The display format of the returned items is the same as "intake feed".`,
Run: func(cmd *cobra.Command, args []string) {
l := cmd.Flags().ArgsLenAtDash()
if l == -1 {
test(nil)
sourceTest(nil)
} else {
test(args[l:])
sourceTest(args[l:])
}
},
}
@ -29,13 +29,13 @@ var testEnv []string
var testFormat string
func init() {
rootCmd.AddCommand(testCmd)
sourceCmd.AddCommand(sourceTestCmd)
testCmd.Flags().StringArrayVarP(&testEnv, "env", "e", nil, "Environment variables to set, in the form KEY=VAL")
testCmd.Flags().StringVarP(&testFormat, "format", "f", "headlines", "Feed format for returned items.")
sourceTestCmd.Flags().StringArrayVarP(&testEnv, "env", "e", nil, "Environment variables to set, in the form KEY=VAL")
sourceTestCmd.Flags().StringVarP(&testFormat, "format", "f", "headlines", "Feed format for returned items.")
}
func test(cmd []string) {
func sourceTest(cmd []string) {
formatter, err := core.FormatAs(testFormat)
if err != nil {
log.Fatal(err)