age/cmd/age/parse.go
2019-10-06 23:16:20 -04:00

52 lines
1.2 KiB
Go

// Copyright 2019 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/FiloSottile/age/internal/age"
)
func parseRecipient(arg string) (age.Recipient, error) {
if strings.HasPrefix(arg, "pubkey:") {
return age.ParseX25519Recipient(arg)
}
return nil, fmt.Errorf("unknown recipient type: %s", arg)
}
func parseIdentitiesFile(name string) ([]age.Identity, error) {
f, err := os.Open(name)
if err != nil {
return nil, fmt.Errorf("failed to open file: %v", err)
}
var ids []age.Identity
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") || line == "" {
continue
}
i, err := age.ParseX25519Identity(line)
if err != nil {
return nil, fmt.Errorf("malformed secret keys file %q: %v", name, err)
}
ids = append(ids, i)
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("failed to read %q: %v", name, err)
}
if len(ids) == 0 {
return nil, fmt.Errorf("no secret keys found in %q", name)
}
return ids, nil
}