package cmd import ( "crypto/rand" "encoding/hex" "fmt" "log" "github.com/Jaculabilis/intake/core" _ "github.com/mattn/go-sqlite3" "github.com/spf13/cobra" ) var itemAddCmd = &cobra.Command{ Use: "add", Short: "Add an item", Long: `Create an ad-hoc item in a source. 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) { itemAdd() }, } var addItemSource string var addItemId string var addItemTitle string var addItemAuthor string var addItemBody string var addItemLink string var addItemTime int func init() { itemCmd.AddCommand(itemAddCmd) itemAddCmd.Flags().StringVarP(&addItemSource, "source", "s", "", "Source in which to create the item (default: default)") itemAddCmd.Flags().StringVarP(&addItemId, "id", "i", "", "Item id (default: random hex)") itemAddCmd.Flags().StringVarP(&addItemTitle, "title", "t", "", "Item title") itemAddCmd.Flags().StringVarP(&addItemAuthor, "author", "a", "", "Item author") itemAddCmd.Flags().StringVarP(&addItemBody, "body", "b", "", "Item body") itemAddCmd.Flags().StringVarP(&addItemLink, "link", "l", "", "Item link") itemAddCmd.Flags().IntVarP(&addItemTime, "time", "m", 0, "Item time as a Unix timestamp") } func itemAdd() { // Default to "default" source if addItemSource == "" { addItemSource = "default" } // Default id to random hex string if addItemId == "" { bytes := make([]byte, 16) if _, err := rand.Read(bytes); err != nil { log.Fatal("Failed to generate id") } addItemId = hex.EncodeToString(bytes) } db := openAndMigrateDb() if err := core.AddItems(db, []core.Item{{ Source: addItemSource, Id: addItemId, Title: addItemTitle, Author: addItemAuthor, Body: addItemBody, Link: addItemLink, Time: addItemTime, }}); err != nil { log.Fatalf("Failed to add item: %s", err) } fmt.Printf("Added %s/%s\n", addItemSource, addItemId) }