예제 #1
0
파일: search.go 프로젝트: matthistuff/shelf
func Search(c *cli.Context) {
	colors.Allow(c)

	page := c.Int("page")
	perPage := 10

	searchQuery := data.ParseQuery(c.Args())
	search := []bson.M{}
	if searchQuery.Text != "" {
		search = append(search, bson.M{
			"$text": bson.M{
				"$search": searchQuery.Text,
			},
		})
	}

	for _, attrQuery := range searchQuery.AttributeQuery {
		search = append(search, bson.M{
			"attributes": bson.M{
				"$elemMatch": bson.M{
					"name": attrQuery.Name,
					"value": bson.M{
						"$regex": attrQuery.Value,
					},
				},
			},
		})
	}

	query := data.Objects().Find(bson.M{
		"$and": search,
	}).Select(bson.M{
		"score": bson.M{
			"$meta": "textScore",
		},
	}).Sort("$textScore:score", "-_id")

	total, _ := query.Count()
	result := []data.SearchObject{}
	query.Skip((page - 1) * perPage).Limit(perPage).All(&result)

	if total > 0 {
		data.ClearCache()
		defer data.FlushCache()

		for index, object := range result {
			fmt.Printf("(%s) %s \"%s\" %s\n", colors.ShortId(index+1),
				colors.ObjectId(object.Id.Hex()),
				object.Title,
				colors.Faint(fmt.Sprintf("[%.2f]", object.Score)))
			data.SetCache(strconv.Itoa(index+1), object.Id.Hex())
		}
		fmt.Printf("Page %s of %s\n", colors.Bold(strconv.Itoa(page)), colors.Bold(strconv.Itoa(int(total/perPage)+1)))
	}
}
예제 #2
0
파일: object.go 프로젝트: matthistuff/shelf
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())
		}
	}
}
예제 #3
0
파일: object.go 프로젝트: matthistuff/shelf
func GetObjects(c *cli.Context) {
	colors.Allow(c)

	query := data.Objects().Find(nil).Sort("-_id")

	page := c.Int("page")
	perPage := 10
	total, _ := query.Count()
	result := []data.Object{}
	query.Skip((page - 1) * perPage).Limit(perPage).All(&result)

	if total > 0 {
		data.ClearCache()
		defer data.FlushCache()

		for index, object := range result {
			fmt.Printf("(%s) %s \"%s\"\n", colors.ShortId(index+1), colors.ObjectId(object.Id.Hex()), object.Title)
			data.SetCache(strconv.Itoa(index+1), object.Id.Hex())
		}
		fmt.Printf("Page %s of %s\n", colors.Bold(strconv.Itoa(page)), colors.Bold(strconv.Itoa(int(total/perPage)+1)))
	}
}
예제 #4
0
func GetAttachmentInfo(c *cli.Context) {
	objectId := helper.ValidId(c.Args().First())

	attachment, err := data.GetAttachment(objectId)
	helper.ErrPanic(err)

	metadata := make(map[string]string)
	for _, attribute := range attachment.MetaData {
		metadata[attribute.Name] = attribute.Value
	}

	keys := make([]string, 0, len(metadata))
	for k := range metadata {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	fmt.Printf("%s\n\n", colors.Bold(attachment.Filename))
	fmt.Printf("%s\n\t%s\n", colors.Header("Uploaded"), attachment.UploadDate.Format(time.RFC1123))

	for _, name := range keys {
		fmt.Printf("%s\n\t%v\n", colors.Header(name), metadata[name])
	}
}