Rework Gpg signing to work through interface.

This commit is contained in:
Andrey Smirnov
2013-12-25 19:14:15 +04:00
parent a33bd14386
commit c1c1776768

View File

@@ -1,20 +1,49 @@
package utils
import (
"fmt"
"os/exec"
"strings"
)
// GpgDetachedSign signs file with detached signature in ASCII format
func GpgDetachedSign(source string, destination string) error {
fmt.Printf("v = %#v\n", strings.Join([]string{"gpg", "-o", destination, "--armor", "--detach-sign", source}, " "))
cmd := exec.Command("gpg", "-o", destination, "--armor", "--yes", "--detach-sign", source)
// Signer interface describes facility implementing signing of files
type Signer interface {
SetKey(keyRef string)
DetachedSign(source string, destination string) error
ClearSign(source string, destination string) error
}
// Test interface
var (
_ Signer = &GpgSigner{}
)
// GpgSigner is implementation of Signer interface using gpg
type GpgSigner struct {
keyRef string
}
// SetKey sets key ID to use when signing files
func (g *GpgSigner) SetKey(keyRef string) {
g.keyRef = keyRef
}
// DetachedSign signs file with detached signature in ASCII format
func (g *GpgSigner) DetachedSign(source string, destination string) error {
args := []string{"-o", destination, "--armor", "--yes"}
if g.keyRef != "" {
args = append(args, "-u", g.keyRef)
}
args = append(args, "--detach-sign", source)
cmd := exec.Command("gpg", args...)
return cmd.Run()
}
// GpgClearSign clear-signs the file
func GpgClearSign(source string, destination string) error {
cmd := exec.Command("gpg", "-o", destination, "--yes", "--clearsign", source)
// ClearSign clear-signs the file
func (g *GpgSigner) ClearSign(source string, destination string) error {
args := []string{"-o", destination, "--yes"}
if g.keyRef != "" {
args = append(args, "-u", g.keyRef)
}
args = append(args, "--clearsign", source)
cmd := exec.Command("gpg", args...)
return cmd.Run()
}