-
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathcommand_local_output.go
More file actions
94 lines (91 loc) 路 2.73 KB
/
Copy pathcommand_local_output.go
File metadata and controls
94 lines (91 loc) 路 2.73 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2022 Paul Greenberg greenpau@outlook.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package security
import (
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
"text/tabwriter"
"unicode"
)
func writeSecurityLocalResponse(w io.Writer, operation, format string, data []byte) error {
if format == "json" {
var output bytes.Buffer
if err := json.Indent(&output, data, "", " "); err != nil {
return fmt.Errorf("invalid response JSON")
}
output.WriteByte('\n')
_, err := w.Write(output.Bytes())
return err
}
var rows [][]string
switch operation {
case "realms":
var response struct {
Realms []struct{ Realm, Kind, Name string } `json:"realms"`
}
if err := json.Unmarshal(data, &response); err != nil {
return fmt.Errorf("invalid realms response")
}
rows = append(rows, []string{"realm", "kind", "name"})
for _, realm := range response.Realms {
rows = append(rows, []string{realm.Realm, realm.Kind, realm.Name})
}
case "users":
var response struct {
Users []struct {
Username, Name, Email string
Roles []string
Disabled bool
} `json:"users"`
}
if err := json.Unmarshal(data, &response); err != nil {
return fmt.Errorf("invalid users response")
}
rows = append(rows, []string{"username", "name", "email", "roles", "disabled"})
for _, user := range response.Users {
rows = append(rows, []string{user.Username, user.Name, user.Email, strings.Join(user.Roles, ";"), strconv.FormatBool(user.Disabled)})
}
default:
return fmt.Errorf("formatted output requires list users or list realms")
}
if format == "csv" {
writer := csv.NewWriter(w)
return writer.WriteAll(rows)
}
if format != "table" {
return fmt.Errorf("unsupported output format")
}
writer := tabwriter.NewWriter(w, 0, 4, 2, ' ', 0)
for _, row := range rows {
for i, value := range row {
// Names are server-controlled; do not let terminal escape sequences or
// tabs/newlines rewrite a human-readable table.
row[i] = strings.Map(func(c rune) rune {
if unicode.IsControl(c) {
return ' '
}
return c
}, value)
}
if _, err := fmt.Fprintln(writer, strings.Join(row, "\t")); err != nil {
return err
}
}
return writer.Flush()
}