Implement source {add,list,delete}
This commit is contained in:
parent
4355a79ec0
commit
14df3cac03
@ -10,7 +10,11 @@ import (
|
|||||||
var sourceCmd = &cobra.Command{
|
var sourceCmd = &cobra.Command{
|
||||||
Use: "source",
|
Use: "source",
|
||||||
Short: "Manage sources",
|
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.
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,19 +3,39 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/Jaculabilis/intake/core"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var sourceAddCmd = &cobra.Command{
|
var sourceAddCmd = &cobra.Command{
|
||||||
Use: "add",
|
Use: "add",
|
||||||
Short: "Create a source",
|
Short: "Create a source",
|
||||||
Long: `
|
Long: `Create a source.
|
||||||
`,
|
`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
log.Fatal("not implemented")
|
sourceAdd()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var sourceAddSource string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
sourceCmd.AddCommand(sourceAddCmd)
|
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)
|
||||||
}
|
}
|
||||||
|
@ -3,19 +3,39 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/Jaculabilis/intake/core"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var sourceDeleteCmd = &cobra.Command{
|
var sourceDeleteCmd = &cobra.Command{
|
||||||
Use: "delete",
|
Use: "delete",
|
||||||
Short: "Delete a source",
|
Aliases: []string{"rm"},
|
||||||
Long: `
|
Short: "Delete a source",
|
||||||
|
Long: `Delete a source.
|
||||||
`,
|
`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
log.Fatal("not implemented")
|
sourceDelete()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var sourceDeleteSource string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
sourceCmd.AddCommand(sourceDeleteCmd)
|
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
40
cmd/sourceList.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
@ -17,10 +17,29 @@ func AddSource(db *DB, name string) error {
|
|||||||
return err
|
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 {
|
func DeleteSource(db *DB, name string) error {
|
||||||
_, err := db.Exec(`
|
_, err := db.Exec(`
|
||||||
delete from sources
|
delete from sources
|
||||||
where name = ?
|
where name = ?
|
||||||
`, name)
|
`, name)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
@ -24,17 +24,11 @@ func TestCreateSource(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := db.Query("select name from sources")
|
names, err := GetSources(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
var names []string
|
|
||||||
expected := []string{"one", "three"}
|
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 {
|
for i := 0; i < len(expected); i += 1 {
|
||||||
if !slices.Contains(names, expected[i]) {
|
if !slices.Contains(names, expected[i]) {
|
||||||
t.Fatalf("missing %s, have: %v", expected[i], names)
|
t.Fatalf("missing %s, have: %v", expected[i], names)
|
||||||
|
Loading…
Reference in New Issue
Block a user