-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (103 loc) · 3 KB
/
main.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"encoding/xml"
"flag"
"fmt"
"io"
"log"
"net/http"
"strings"
"github.com/fatih/color"
"github.com/ktr0731/go-fuzzyfinder"
)
// Configuration was generated 2024-02-25 13:50:16 by https://xml-to-go.github.io/ in Ukraine.
type Configuration struct {
XMLName xml.Name `xml:"configuration"`
Property []struct {
XMLName xml.Name `xml:"property"`
Name string `xml:"name"`
Value string `xml:"value"`
Description string `xml:"description"`
} `xml:"property"`
}
var (
mode string
version string
fzf_desc bool
red = color.New(color.FgRed).SprintFunc()
yellow = color.New(color.FgYellow).SprintFunc()
)
func getUrls(version string) map[string]string {
baseUrl := "https://hadoop.apache.org/docs/" // note the r
modes := map[string]string{
"core": baseUrl + version + "/hadoop-project-dist/hadoop-common/core-default.xml",
"hdfs": baseUrl + version + "/hadoop-project-dist/hadoop-hdfs/hdfs-default.xml",
"yarn": baseUrl + version + "/hadoop-yarn/hadoop-yarn-common/yarn-default.xml",
}
return modes
}
func main() {
const (
defaultMode = "hdfs"
usageMode = `mode. decide which part of hdfs you want to configure and fuzzy find. Supported modes are:
- core [core-site.xml]
- yarn [yarn-site.xml]
- hdfs [hdfs-site.xml]`
defaultVersion = "current"
usageVersion = "The version of hadoop in format: rx.y.z (e.g. r3.3.6)"
defaultSwitch = false
usageSwitch = "Wether to search in configuration names or descriptions"
)
flag.StringVar(&mode, "mode", defaultMode, usageMode)
flag.StringVar(&mode, "m", defaultMode, usageMode+"(shorthand)")
flag.StringVar(&version, "version", defaultVersion, usageVersion)
flag.StringVar(&version, "v", defaultVersion, usageVersion+"(shorthand)")
flag.BoolVar(&fzf_desc, "switch", defaultSwitch, usageSwitch)
flag.BoolVar(&fzf_desc, "s", defaultSwitch, usageSwitch+"(shorthand)")
flag.Parse()
// fmt.Println(*mode)
modes := getUrls(version)
url, ok := modes[mode]
if !ok {
log.Fatal("wrong mode. type --help for usage")
}
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
var config Configuration
xml.Unmarshal(body, &config)
idx, err := fuzzyfinder.FindMulti(
config.Property,
func(i int) string {
if fzf_desc {
return config.Property[i].Description
}
return config.Property[i].Name
},
fuzzyfinder.WithPreviewWindow(func(i, w, h int) string {
if i == -1 {
return ""
}
return fmt.Sprintf("Name: %s \nDefault: %s \n\nDescription: %s",
red(config.Property[i].Name),
yellow(config.Property[i].Value),
yellow(config.Property[i].Description))
}))
if err != nil {
log.Fatal(err)
}
for _, element := range idx {
// print out the xml snippet for copy
out, err := xml.MarshalIndent(config.Property[element], " ", " ")
if err != nil {
log.Fatal(err)
}
output := string(out)
fmt.Println(red(strings.Replace(output, "
", "\n", -1)))
}
}