Example #1
0
File: client.go Project: gaku/1pass
func editItem(vault *onepass.Vault, pattern string) {
	item, err := lookupSingleItem(vault, pattern)
	if err != nil {
		fatalErr(err, "Failed to find item")
	}

	logItemAction("Editing item", item)
	content, err := item.Content()
	if err != nil {
		fatalErr(err, "Unable to read item content")
	}

	formSectionId := len(content.Sections) + 1
	urlSectionId := len(content.Sections) + 2

	for i, section := range content.Sections {
		fmt.Printf("%d : %s\n", i+1, section.Title)
	}
	fmt.Printf("%d : Web Form fields\n", formSectionId)
	fmt.Printf("%d : URLs\n", urlSectionId)

	var section *onepass.ItemSection
	var field *onepass.ItemField

	sectionIdStr := readLinePrompt("Section (or title of new section)")
	sectionId, err := strconv.Atoi(sectionIdStr)
	if err != nil {
		// new section
		content.Sections = append(content.Sections, onepass.ItemSection{
			Name:   sectionIdStr,
			Title:  sectionIdStr,
			Fields: []onepass.ItemField{},
		})
		section = &content.Sections[len(content.Sections)-1]
	} else if sectionId > 0 && sectionId <= len(content.Sections) {
		section = &content.Sections[sectionId-1]
	} else if sectionId != formSectionId && sectionId != urlSectionId {
		fatalErr(nil, "Unknown section number")
	}

	if section != nil {
		for i, field := range section.Fields {
			fmt.Printf("%d : %s (%s)\n", i+1, field.Title, field.ValueString())
		}
		fieldIdStr := readLinePrompt("Field (or title of new field)")
		fieldId, err := strconv.Atoi(fieldIdStr)
		if err != nil {
			// new field
			section.Fields = append(section.Fields, onepass.ItemField{
				Name:  fieldIdStr,
				Kind:  "string",
				Title: fieldIdStr,
			})
			field = &section.Fields[len(section.Fields)-1]
		} else if fieldId > 0 && fieldId <= len(section.Fields) {
			field = &section.Fields[fieldId-1]
		} else {
			fatalErr(nil, "Unknown field number")
		}
		field.Value = readFieldValue(*field)

	} else if sectionId == formSectionId {
		for i, field := range content.FormFields {
			fmt.Printf("%d : %s (%s)\n", i+1, field.Name, field.Value)
		}
		fieldIdStr := readLinePrompt("Field")
		fieldId, err := strconv.Atoi(fieldIdStr)
		if err == nil && fieldId > 0 && fieldId <= len(content.FormFields) {
			content.FormFields[fieldId-1].Value = readFormFieldValue(content.FormFields[fieldId-1])
		} else {
			fatalErr(nil, "Unknown field number")
		}
	} else if sectionId == urlSectionId {
		for i, url := range content.Urls {
			fmt.Printf("%d : %s (%s)\n", i+1, url.Label, url.Url)
		}
		var url *onepass.ItemUrl
		urlIdStr := readLinePrompt("URL (or label of new URL)")
		urlId, err := strconv.Atoi(urlIdStr)
		if err != nil {
			// new URL
			content.Urls = append(content.Urls, onepass.ItemUrl{
				Label: urlIdStr,
			})
			url = &content.Urls[len(content.Urls)-1]
		} else if urlId > 0 && urlId <= len(content.Urls) {
			url = &content.Urls[urlId-1]
		} else {
			fatalErr(nil, "Unknown URL number")
		}

		url.Url = readLinePrompt("%s", url.Label)
	}

	err = item.SetContent(content)
	if err != nil {
		fatalErr(err, "Unable to save updated content")
	}
	err = item.Save()
	if err != nil {
		fatalErr(err, "Unable to save updated item")
	}
}