func DeleteObject(c *cli.Context) { objectId := helper.ValidId(c.Args().First()) object, err := data.GetObject(objectId) helper.ErrExit(err != nil, fmt.Sprintf("Invalid object ID %s!\n", objectId)) files := data.Files() for _, attachment := range object.Attachments { files.RemoveId(attachment.Id) } data.Objects().RemoveId(object.Id) }
func RemoveTag(c *cli.Context) { if len(c.Args()) < 2 { helper.ErrExit(true, "Not enough arguments provided") } objectId := helper.ValidId(c.Args().First()) value := strings.Join(c.Args().Tail(), " ") object, err := data.GetObject(objectId) helper.ErrExit(err != nil, fmt.Sprintf("Invalid object ID %s!\n", objectId)) object.RemoveTag(value) }
func GetObject(c *cli.Context) { colors.Allow(c) objectId := helper.ValidId(c.Args().First()) object, err := data.GetObject(objectId) helper.ErrExit(err != nil, fmt.Sprintf("Invalid object ID %s!\n", objectId)) attributes := make(map[string][]string) for _, attribute := range object.Attributes { attributes[attribute.Name] = append(attributes[attribute.Name], attribute.Value) } keys := make([]string, 0, len(attributes)) for k := range attributes { keys = append(keys, k) } sort.Strings(keys) fmt.Printf("%s\n\n", colors.Bold(object.Title)) fmt.Printf("%s\n\t%s\n", colors.Header("Created"), object.CreateDate.Format(time.RFC1123)) if _, exists := attributes["content"]; exists { fmt.Printf("\n%s\n\t%s\n", colors.Header("Content"), strings.Join(attributes["content"], ", ")) delete(attributes, "content") } if len(attributes) > 0 { fmt.Printf("\n%s\n", colors.Header("Attributes")) for k := range keys { sort.Strings(attributes[keys[k]]) fmt.Printf("\t%s: %s\n", colors.Bold(keys[k]), strings.Join(attributes[keys[k]], ", ")) } } if len(object.Attachments) > 0 { data.ClearCache() defer data.FlushCache() fmt.Printf("\n%s\n", colors.Header("Attachments")) for index, attachment := range object.Attachments { fmt.Printf("\t(%s) %s: %s (%s)\n", colors.ShortId(index+1), colors.ObjectId(attachment.Id.Hex()), attachment.Filename, attachment.UploadDate.Format(time.RFC1123)) data.SetCache(strconv.Itoa(index+1), attachment.Id.Hex()) } } }
func RemoveAttribute(c *cli.Context) { if len(c.Args()) < 3 { helper.ErrExit(true, "Not enough arguments provided") } objectId := helper.ValidId(c.Args().First()) name := c.Args().Get(1) value := strings.Join(c.Args()[2:], " ") object, err := data.GetObject(objectId) helper.ErrExit(err != nil, fmt.Sprintf("Invalid object ID %s!\n", objectId)) object.RemoveAttribute(name, value) }
func GetInfo(c *cli.Context) { objectId := helper.ValidId(c.Args().First()) _, err := data.GetObject(objectId) if err == nil { GetObject(c) return } _, err = data.GetAttachment(objectId) if err == nil { GetAttachmentInfo(c) return } helper.ErrExit(err != nil, fmt.Sprintf("Invalid object ID %s!\n", objectId)) }
func ListAttachments(c *cli.Context) { colors.Allow(c) objectId := helper.ValidId(c.Args().First()) object, err := data.GetObject(objectId) helper.ErrExit(err != nil, fmt.Sprintf("Invalid object ID %s!\n", objectId)) if len(object.Attachments) > 0 { data.ClearCache() defer data.FlushCache() for index, attachment := range object.Attachments { fmt.Printf("(%s) %s \"%s\"\n", colors.ShortId(index+1), colors.ObjectId(attachment.Id.Hex()), attachment.Filename) data.SetCache(strconv.Itoa(index+1), attachment.Id.Hex()) } } }
func AddAttachment(c *cli.Context) { objectId := helper.ValidId(c.Args().First()) attachmentPath := c.Args().Get(1) object, err := data.GetObject(objectId) helper.ErrExit(err != nil, fmt.Sprintf("Invalid object ID %s!\n", objectId)) dbFile, err := data.Files().Create("") helper.ErrPanic(err) file, err := os.Open(attachmentPath) helper.ErrPanic(err) defer file.Close() _, err = io.Copy(dbFile, file) helper.ErrPanic(err) err = dbFile.Close() helper.ErrPanic(err) var ( content = "" metadata = make(map[string]string) ) if strings.ToLower(filepath.Ext(file.Name())) == ".pdf" { if c.BoolT("extract-pdf-text") { content, metadata, err = helper.ConvertPDF(file) helper.ErrPanic(err) } } attachment := data.CreateAttachment(dbFile, path.Base(attachmentPath), content, metadata) object.Attachments = append(object.Attachments, *attachment) object.Update() fmt.Println(attachment.Id.Hex()) }