-
Notifications
You must be signed in to change notification settings - Fork 1
/
output.go
56 lines (44 loc) · 1.13 KB
/
output.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// SPDX-FileCopyrightText: 2023 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package rosenpass
import (
"errors"
"fmt"
"strconv"
"strings"
)
type KeyOutputReason string
const (
KeyOutputReasonExchanged KeyOutputReason = "exchanged"
KeyOutputReasonStale KeyOutputReason = "stale"
)
var errInvalidOutputFormat = errors.New("invalid output format")
// Output format:
// output-key peer {} key-file {of:?} {why}.
type KeyOutput struct {
Peer PeerID
KeyFile string
Why KeyOutputReason
}
func ParseKeyOutput(str string) (o KeyOutput, err error) {
tokens := strings.Split(str, " ")
if tokens[0] != "output-key" ||
tokens[1] != "peer" ||
tokens[3] != "key-file" {
return o, errInvalidOutputFormat
}
if o.Peer, err = ParsePeerID(tokens[2]); err != nil {
return o, fmt.Errorf("failed to parse peer id: %w", err)
}
o.KeyFile = strings.Trim(tokens[4], "\"")
o.Why = KeyOutputReason(tokens[5])
return o, nil
}
func (o KeyOutput) String() string {
return strings.Join([]string{
"output-key",
"peer", o.Peer.String(),
"key-file", strconv.Quote(o.KeyFile),
string(o.Why),
}, " ")
}