예제 #1
0
파일: main.go 프로젝트: fd/1pwd
func doSearch(vault *opvault.Vault, query, typeFilter, extract string, jsonFormat bool) {
	if typeFilter == "any" {
		typeFilter = ""
	}
	cat := opvault.FromTypeString(typeFilter)

	results := vault.All()

	var bufIn bytes.Buffer
	var bufOut bytes.Buffer

	tabw := tabwriter.NewWriter(&bufIn, 8, 8, 2, ' ', tabwriter.StripEscape)
	for _, result := range results {
		if result.Trashed {
			continue
		}
		if typeFilter != "" && result.Category != cat {
			continue
		}

		fmt.Fprintf(tabw,
			field("%s", "")+
				field("%s", "2")+
				field("%s", "")+
				field("%s", "blue")+
				field("%s", "yellow")+
				"\n",
			result.UUID,
			result.UUID[:8],
			result.Category.String(),
			trunc(result.Data.Domain, 32),
			result.Data.Title,
		)
	}
	tabw.Flush()

	cmd := exec.Command("fzf", "--ansi", "--with-nth=2..", "--nth=4..,3,1", "--query="+query)
	cmd.Env = os.Environ()
	cmd.Stdin = &bufIn
	cmd.Stdout = &bufOut
	cmd.Stderr = os.Stderr
	err := cmd.Run()
	assert(err)

	var id string
	fmt.Fscan(&bufOut, &id)
	if id != "" {
		doGet(vault, id, extract, jsonFormat)
	}
}
예제 #2
0
파일: main.go 프로젝트: fd/1pwd
func doGet(vault *opvault.Vault, id, extract string, jsonFormat bool) {

	item, err := vault.Get(id)
	assert(err)

	err = item.Decrypt(vault)
	assert(err)

	var (
		v interface{} = item.Data
		f             = true
	)

	if extract != "" {
		v, f = item.Extract(extract)
		if !f {
			abortf("field %q not found", extract)
		}
	}

	if jsonFormat {
		err = json.NewEncoder(os.Stdout).Encode(v)
		assert(err)
	} else {
		if extract != "" {
			fmt.Printf("%s\n", v)
		} else {
			fmt.Printf("id:       %s\n", item.UUID)
			if v, f := item.Extract("url"); f {
				fmt.Printf("url:      %s\n", v)
			}
			if v, f := item.Extract("username"); f {
				fmt.Printf("username: %s\n", v)
			}
			if v, f := item.Extract("password"); f {
				fmt.Printf("password: %s\n", v)
			}
		}
	}
}