Implement source {add,list,delete}

This commit is contained in:
Tim Van Baak 2025-01-23 07:55:11 -08:00
parent 4355a79ec0
commit 14df3cac03
6 changed files with 113 additions and 16 deletions

View File

@ -10,7 +10,11 @@ import (
var sourceCmd = &cobra.Command{
Use: "source",
Short: "Manage sources",
Long: `
Long: `Manage sources.
A source represents a single content feed that generates discrete feed items.
The command defined in the "fetch" action is used to check for new items to
update the feed.
`,
}

View File

@ -3,19 +3,39 @@ package cmd
import (
"log"
"github.com/Jaculabilis/intake/core"
"github.com/spf13/cobra"
)
var sourceAddCmd = &cobra.Command{
Use: "add",
Short: "Create a source",
Long: `
Long: `Create a source.
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
sourceAdd()
},
}
var sourceAddSource string
func init() {
sourceCmd.AddCommand(sourceAddCmd)
sourceAddCmd.Flags().StringVarP(&sourceAddSource, "source", "s", "", "Source name")
sourceAddCmd.MarkFlagRequired("source")
}
func sourceAdd() {
if sourceAddSource == "" {
log.Fatal("error: --source is empty")
}
db := openAndMigrateDb()
if err := core.AddSource(db, sourceAddSource); err != nil {
log.Fatalf("error: %v", err)
}
log.Printf("Added source %s", sourceAddSource)
}

View File

@ -3,19 +3,39 @@ package cmd
import (
"log"
"github.com/Jaculabilis/intake/core"
"github.com/spf13/cobra"
)
var sourceDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete a source",
Long: `
Use: "delete",
Aliases: []string{"rm"},
Short: "Delete a source",
Long: `Delete a source.
`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("not implemented")
sourceDelete()
},
}
var sourceDeleteSource string
func init() {
sourceCmd.AddCommand(sourceDeleteCmd)
sourceDeleteCmd.Flags().StringVarP(&sourceDeleteSource, "source", "s", "", "Source to delete")
}
func sourceDelete() {
if sourceDeleteSource == "" {
log.Fatal("error: --source is empty")
}
db := openAndMigrateDb()
if err := core.DeleteSource(db, sourceDeleteSource); err != nil {
log.Fatalf("error: %v", err)
}
log.Printf("Deleted source %s", sourceDeleteSource)
}

40
cmd/sourceList.go Normal file
View File

@ -0,0 +1,40 @@
package cmd
import (
"fmt"
"log"
"slices"
"github.com/Jaculabilis/intake/core"
"github.com/spf13/cobra"
)
var sourceListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List sources",
Long: `Print the list of sources.
`,
Run: func(cmd *cobra.Command, args []string) {
sourceList()
},
}
func init() {
sourceCmd.AddCommand(sourceListCmd)
}
func sourceList() {
db := openAndMigrateDb()
names, err := core.GetSources(db)
if err != nil {
log.Fatalf("error: %v", err)
}
slices.Sort(names)
for _, name := range names {
fmt.Println(name)
}
}

View File

@ -17,10 +17,29 @@ func AddSource(db *DB, name string) error {
return err
}
func GetSources(db *DB) ([]string, error) {
rows, err := db.Query(`
select name
from sources
`)
if err != nil {
return nil, err
}
var names []string
for rows.Next() {
var name string
if err = rows.Scan(&name); err != nil {
return nil, err
}
names = append(names, name)
}
return names, nil
}
func DeleteSource(db *DB, name string) error {
_, err := db.Exec(`
delete from sources
where name = ?
delete from sources
where name = ?
`, name)
return err

View File

@ -24,17 +24,11 @@ func TestCreateSource(t *testing.T) {
t.Fatal(err)
}
rows, err := db.Query("select name from sources")
names, err := GetSources(db)
if err != nil {
t.Fatal(err)
}
var names []string
expected := []string{"one", "three"}
for rows.Next() {
var name string
rows.Scan(&name)
names = append(names, name)
}
for i := 0; i < len(expected); i += 1 {
if !slices.Contains(names, expected[i]) {
t.Fatalf("missing %s, have: %v", expected[i], names)