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
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package main
// This is a utility designed to create a list of all Elasticsearch http
// endpoints. The output of this script is checked in as 'endpoints.txt'.
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
)
type endpoint struct {
firstUnderscoreSegment string
filename string
method string
path string
}
func main() {
if len(os.Args) < 2 {
panic("provide path to github.com/elastic/elasticsearch/tree/7.5/rest-api-spec/src/main/resources/rest-api-spec/api/")
}
var files []string
err := filepath.Walk(os.Args[1], func(path string, info os.FileInfo, err error) error {
files = append(files, path)
return nil
})
if err != nil {
panic(err)
}
var endpoints []endpoint
for _, path := range files {
_, filename := filepath.Split(path)
// Check the "_" prefix to avoid parsing _common.
if filename == "" || strings.HasPrefix(filename, "_") {
continue
}
contents, err := ioutil.ReadFile(path)
if nil != err {
panic(err)
}
var fields map[string]struct {
Stability string `json:"stability"`
URL struct {
Paths []struct {
Path string `json:"path"`
Methods []string `json:"methods"`
} `json:"paths"`
} `json:"url"`
}
err = json.Unmarshal(contents, &fields)
if nil != err {
panic(err)
}
for _, v := range fields {
for _, p := range v.URL.Paths {
path := p.Path
segments := strings.Split(strings.TrimPrefix(path, "/"), "/")
var firstUnderscoreSegment string
for _, s := range segments {
if strings.HasPrefix(s, "_") {
firstUnderscoreSegment = s
break
}
}
for _, method := range p.Methods {
endpoints = append(endpoints, endpoint{
firstUnderscoreSegment: firstUnderscoreSegment,
filename: filename,
method: method,
path: path,
})
}
}
}
}
sort.Slice(endpoints, func(i int, j int) bool {
iseg := endpoints[i].firstUnderscoreSegment
jseg := endpoints[j].firstUnderscoreSegment
if iseg == jseg {
return endpoints[i].filename < endpoints[j].filename
}
return iseg < jseg
})
for _, e := range endpoints {
fmt.Printf("%-18v %-35v %-10v %v\n",
e.firstUnderscoreSegment,
e.filename,
e.method,
e.path)
}
}
|