func importItems(vault *onepass.Vault, path string) { items, err := onepass.ImportItems(path) if err != nil { fatalErr(err, "Unable to import items") } for _, importedItem := range items { item, err := vault.AddItem(importedItem.Title, importedItem.TypeName, importedItem.SecureContents) if err != nil { fatalErr(err, fmt.Sprintf("Unable to import item '%s'", importedItem.Title)) } logItemAction("Imported item", item) } }
func listTag(vault *onepass.Vault, tag string) { items, err := vault.ListItems() if err != nil { fatalErr(err, "Unable to list vault items") } itemsWithTag := []onepass.Item{} for _, item := range items { hasTag := rangeutil.Contains(0, len(item.OpenContents.Tags), func(i int) bool { return item.OpenContents.Tags[i] == tag }) if hasTag { itemsWithTag = append(itemsWithTag, item) } } listItems(vault, itemsWithTag) }
func listMatchingItems(vault *onepass.Vault, pattern string) { var items []onepass.Item var err error if len(pattern) > 0 { items, err = lookupItems(vault, pattern) } else { items, err = vault.ListItems() } if err != nil { fmt.Fprintf(os.Stderr, "Unable to list vault items: %v\n", err) os.Exit(1) } listItems(vault, items) }
func setPassword(vault *onepass.Vault, currentPwd string) { // TODO - Prompt for hint and save that to the .password.hint file fmt.Printf("New master password: "******"\nRe-enter new master password: "******"Passwords do not match") } err = vault.SetMasterPassword(currentPwd, string(newPwd)) if err != nil { fatalErr(err, "Failed to change master password") } fmt.Printf("The master password has been updated.\n\n") fmt.Printf(setPasswordSyncNote) }
func listFolder(vault *onepass.Vault, pattern string) { pattern = "folder:" + pattern folder, err := lookupSingleItem(vault, pattern) if err != nil { fatalErr(err, "Failed to find folder") } items, err := vault.ListItems() if err != nil { fatalErr(err, "Failed to list items") } itemsInFolder := []onepass.Item{} for _, item := range items { if item.FolderUuid == folder.Uuid { itemsInFolder = append(itemsInFolder, item) } } listItems(vault, itemsInFolder) }
func listTags(vault *onepass.Vault) { uniqTags := map[string]bool{} items, err := vault.ListItems() if err != nil { fatalErr(err, "Unable to list vault items") } for _, item := range items { for _, tag := range item.OpenContents.Tags { uniqTags[tag] = true } } tags := []string{} for tag, _ := range uniqTags { tags = append(tags, tag) } sort.Strings(tags) for _, tag := range tags { fmt.Printf("%s\n", tag) } }
func showItem(vault *onepass.Vault, item onepass.Item) { typeName := item.TypeName itemType, ok := onepass.ItemTypes[item.TypeName] if ok { typeName = itemType.Name } fmt.Printf("%s (%s)\n", item.Title, typeName) fmt.Printf("Info:\n") fmt.Printf(" ID: %s\n", item.Uuid) updateTime := int64(item.UpdatedAt) if updateTime == 0 { updateTime = int64(item.CreatedAt) } fmt.Printf(" Updated: %s\n", time.Unix(updateTime, 0).Format("15:04 02/01/06")) if len(item.FolderUuid) > 0 { folder, err := vault.LoadItem(item.FolderUuid) if err != nil { fmt.Fprintf(os.Stderr, "Item folder '%s' not found", item.FolderUuid) // continue } fmt.Printf(" Folder: %s\n", folder.Title) } if len(item.OpenContents.Tags) > 0 { fmt.Printf(" Tags: %s\n", strings.Join(item.OpenContents.Tags, ", ")) } fmt.Println() content, err := item.Content() if err != nil { fmt.Fprintf(os.Stderr, "Failed to decrypt item: %s: %v", item.Title, err) return } fmt.Printf(content.String()) }
func lookupItems(vault *onepass.Vault, pattern string) ([]onepass.Item, error) { typeName := typeFromAlias(pattern) if typeName != "" { pattern = "" } if strings.Contains(pattern, ":") { parts := strings.SplitN(pattern, ":", 2) typeName = typeFromAlias(parts[0]) pattern = parts[1] if typeName == "" { fatalErr(nil, fmt.Sprintf("Unknown type name '%s'", parts[0])) } } items, err := vault.ListItems() if err != nil { return items, err } patternLower := strings.ToLower(pattern) matches := []onepass.Item{} for _, item := range items { patternMatch := pattern == "" typeMatch := typeName == "" || item.TypeName == typeName if strings.Contains(strings.ToLower(item.Title), patternLower) || strings.HasPrefix(strings.ToLower(item.Uuid), patternLower) { patternMatch = true } if patternMatch && typeMatch { matches = append(matches, item) } } return matches, nil }
// create a set of item templates based on existing // items in a vault func exportItemTemplates(vault *onepass.Vault, pattern string) { items, err := vault.ListItems() if err != nil { fatalErr(err, "Unable to list vault items") } typeTemplates := map[string]onepass.ItemContent{} for _, item := range items { typeTemplate := onepass.ItemContent{ Sections: []onepass.ItemSection{}, FormFields: []onepass.WebFormField{}, } if !strings.HasPrefix(strings.ToLower(item.Title), pattern) { continue } content, err := item.Content() if err != nil { fmt.Fprintf(os.Stderr, "Failed to decrypt item: %v\n", err) } // section templates for _, section := range content.Sections { sectionTemplate := onepass.ItemSection{ Name: section.Name, Title: section.Title, Fields: []onepass.ItemField{}, } for _, field := range section.Fields { fieldTemplate := onepass.ItemField{ Name: field.Name, Title: field.Title, Kind: field.Kind, } sectionTemplate.Fields = append(sectionTemplate.Fields, fieldTemplate) } typeTemplate.Sections = append(typeTemplate.Sections, sectionTemplate) } // web form field templates for _, formField := range content.FormFields { formTemplate := onepass.WebFormField{ Name: formField.Name, Id: formField.Id, Type: formField.Type, Designation: formField.Designation, } typeTemplate.FormFields = append(typeTemplate.FormFields, formTemplate) } // URL templates for _, url := range content.Urls { urlTemplate := onepass.ItemUrl{Label: url.Label} typeTemplate.Urls = append(typeTemplate.Urls, urlTemplate) } typeTemplates[item.TypeName] = typeTemplate } data, err := json.Marshal(typeTemplates) if err != nil { fatalErr(err, "Unable to export item templates") } _, _ = os.Stdout.Write(prettyJson(data)) }
func addItem(vault *onepass.Vault, title string, shortTypeName string) { itemContent := onepass.ItemContent{} var typeName string for typeKey, itemType := range onepass.ItemTypes { if itemType.ShortAlias == shortTypeName { itemContent = onepass.ItemContent{} typeName = typeKey } } if len(typeName) == 0 { fatalErr(fmt.Errorf("Unknown item type '%s'", shortTypeName), "") } template, ok := onepass.StandardTemplate(typeName) if !ok { fatalErr(fmt.Errorf("No template for item type '%s'", shortTypeName), "") } // read sections for _, sectionTemplate := range template.Sections { section := onepass.ItemSection{ Name: sectionTemplate.Name, Title: sectionTemplate.Title, Fields: []onepass.ItemField{}, } for _, fieldTemplate := range sectionTemplate.Fields { field := onepass.ItemField{ Name: fieldTemplate.Name, Title: fieldTemplate.Title, Kind: fieldTemplate.Kind, } field.Value = readFieldValue(field) section.Fields = append(section.Fields, field) } itemContent.Sections = append(itemContent.Sections, section) } // read form fields for _, formFieldTemplate := range template.FormFields { field := onepass.WebFormField{ Name: formFieldTemplate.Name, Id: formFieldTemplate.Id, Type: formFieldTemplate.Type, Designation: formFieldTemplate.Designation, } field.Value = readFormFieldValue(field) itemContent.FormFields = append(itemContent.FormFields, field) } // read URLs for _, urlTemplate := range template.Urls { url := onepass.ItemUrl{ Label: urlTemplate.Label, } url.Url = readLinePrompt("%s (URL)", url.Label) itemContent.Urls = append(itemContent.Urls, url) } // save item to vault item, err := vault.AddItem(title, typeName, itemContent) if err != nil { fatalErr(err, "Unable to add item") } logItemAction("Added new item", item) }