Mateusz Zalega | db75e21 | 2022-08-04 17:31:34 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "log" |
| 7 | "os" |
| 8 | |
| 9 | "source.monogon.dev/metropolis/node/core/identity" |
| 10 | apb "source.monogon.dev/metropolis/proto/api" |
| 11 | ) |
| 12 | |
| 13 | type encoder struct { |
| 14 | out io.WriteCloser |
| 15 | } |
| 16 | |
| 17 | func (e *encoder) writeNodeID(n *apb.Node) error { |
| 18 | id := identity.NodeID(n.Pubkey) |
| 19 | _, err := fmt.Fprintf(e.out, "%s\n", id) |
| 20 | return err |
| 21 | } |
| 22 | |
| 23 | func (e *encoder) close() error { |
| 24 | if e.out != os.Stdout { |
| 25 | return e.out.Close() |
| 26 | } |
| 27 | return nil |
| 28 | } |
| 29 | |
| 30 | func newOutputEncoder() *encoder { |
| 31 | var o io.WriteCloser |
| 32 | o = os.Stdout |
| 33 | |
| 34 | // Redirect output to the file at flags.output, if the flag was provided. |
| 35 | if flags.output != "" { |
| 36 | of, err := os.Create(flags.output) |
| 37 | if err != nil { |
| 38 | log.Fatalf("Couldn't create the output file at %s: %v", flags.output, err) |
| 39 | } |
| 40 | o = of |
| 41 | } |
| 42 | |
| 43 | if flags.format != "plaintext" { |
| 44 | log.Fatalf("Currently only the plaintext output format is supported.") |
| 45 | } |
| 46 | return &encoder{ |
| 47 | out: o, |
| 48 | } |
| 49 | } |