-
Notifications
You must be signed in to change notification settings - Fork 1
/
output.go
57 lines (47 loc) · 1.19 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
57
// Copyright 2022 Adam Chalkley
//
// https://github.com/atc0005/safelinks
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
package safelinks
import (
"fmt"
"io"
"net/url"
"sort"
)
// emitOutput emits a given URL to the specified output sink. If specified,
// verbose output is used.
func emitOutput(u *url.URL, w io.Writer, verbose bool) {
switch {
case verbose:
verboseOutput(u, w)
default:
simpleOutput(u, w)
}
}
// simpleOutput handles generating reduced or "simple" output when verbose
// mode is not invoked.
func simpleOutput(u *url.URL, w io.Writer) {
urlValues := u.Query()
maskedURL := urlValues.Get("url")
fmt.Fprintln(w, maskedURL)
}
// verboseOutput handles generating extended or "verbose" output when
// requested.
func verboseOutput(u *url.URL, w io.Writer) {
urlValues := u.Query()
urlValues.Add("host", u.Host)
keys := make([]string, 0, len(urlValues))
for k := range urlValues {
keys = append(keys, k)
}
sort.Strings(keys)
fmt.Fprintf(w, "\nExpanded values from the given link:\n\n")
for _, key := range keys {
if len(urlValues[key]) > 0 {
fmt.Fprintf(w, " %-10s: %s\n", key, urlValues[key][0])
}
}
}