1/*
2Copyright 2018 The Doctl Authors All rights reserved.
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6    http://www.apache.org/licenses/LICENSE-2.0
7Unless required by applicable law or agreed to in writing, software
8distributed under the License is distributed on an "AS IS" BASIS,
9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10See the License for the specific language governing permissions and
11limitations under the License.
12*/
13
14package displayers
15
16import (
17	"io"
18
19	"github.com/digitalocean/doctl/do"
20)
21
22// Key is used to display the SSH Key results from a `list` operation.
23type Key struct {
24	Keys do.SSHKeys
25}
26
27var _ Displayable = &Key{}
28
29func (ke *Key) JSON(out io.Writer) error {
30	return writeJSON(ke.Keys, out)
31}
32
33func (ke *Key) Cols() []string {
34	return []string{
35		"ID", "Name", "FingerPrint",
36	}
37}
38
39func (ke *Key) ColMap() map[string]string {
40	return map[string]string{
41		"ID": "ID", "Name": "Name", "FingerPrint": "FingerPrint",
42	}
43}
44
45func (ke *Key) KV() []map[string]interface{} {
46	out := make([]map[string]interface{}, 0, len(ke.Keys))
47
48	for _, k := range ke.Keys {
49		o := map[string]interface{}{
50			"ID": k.ID, "Name": k.Name, "FingerPrint": k.Fingerprint,
51		}
52
53		out = append(out, o)
54	}
55
56	return out
57}
58
59// KeyGet is used to display the SSH Key results from a `get` operation. This
60// separate displayer is required in order to include the public key in this
61// operation.
62type KeyGet struct {
63	Keys do.SSHKeys
64}
65
66var _ Displayable = &KeyGet{}
67
68func (ke *KeyGet) JSON(out io.Writer) error {
69	return writeJSON(ke.Keys, out)
70}
71
72func (ke *KeyGet) Cols() []string {
73	return []string{
74		"ID", "Name", "FingerPrint", "PublicKey",
75	}
76}
77
78func (ke *KeyGet) ColMap() map[string]string {
79	return map[string]string{
80		"ID": "ID", "Name": "Name", "FingerPrint": "FingerPrint", "PublicKey": "Public Key",
81	}
82}
83
84func (ke *KeyGet) KV() []map[string]interface{} {
85	out := make([]map[string]interface{}, 0, len(ke.Keys))
86
87	for _, k := range ke.Keys {
88		o := map[string]interface{}{
89			"ID": k.ID, "Name": k.Name, "FingerPrint": k.Fingerprint, "PublicKey": k.PublicKey,
90		}
91
92		out = append(out, o)
93	}
94
95	return out
96}
97