-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
109 lines (100 loc) · 3.15 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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
)
type Config struct {
URL string `json:"url"`
User string `json:"user"`
Password string `json:"password"`
IndexName string `json:"index_name"`
Size int `json:"size"`
CSVFile string `json:"csv_file"`
TimestampField string `json:"timestamp_field"`
}
func main() {
// Define flags with both long and short forms
var (
url = flag.String("url", "", "OpenSearch URL (short form: -u)")
urlShort = flag.String("u", "", "OpenSearch URL (short form of -url)")
user = flag.String("user", "", "OpenSearch User (short form: -U)")
userShort = flag.String("U", "", "OpenSearch User (short form of -user)")
password = flag.String("password", "", "OpenSearch Password (short form: -p)")
passwordShort = flag.String("p", "", "OpenSearch Password (short form of -password)")
indexName = flag.String("index", "", "Index Name (short form: -i)")
indexShort = flag.String("i", "", "Index Name (short form of -index)")
size = flag.Int("size", 0, "Size limit for the number of documents to fetch (short form: -s)")
sizeShort = flag.Int("s", 10, "Size limit for the number of documents to fetch (short form: -s)")
configFile = flag.String("config", "", "Config file path (short form: -c)")
configShort = flag.String("c", "", "Config file path (short form of -config)")
csvFile = flag.String("csv", "", "CSV output file (short form: -o)")
csvShort = flag.String("o", "", "CSV output file (short form of -csv)")
timestampField = flag.String("timestamp", "@timestamp", "Timestamp field name (short form: -t)")
timestampShort = flag.String("t", "@timestamp", "Timestamp field name (short form of -timestamp)")
)
// Override the default flag.Usage
flag.Usage = Usage
flag.Parse()
// Use the short form values if the long form was not provided
if *url == "" {
url = urlShort
}
if *user == "" {
user = userShort
}
if *password == "" {
password = passwordShort
}
if *indexName == "" {
indexName = indexShort
}
if *size == 0 {
size = sizeShort
}
if *configFile == "" {
configFile = configShort
}
if *csvFile == "" {
csvFile = csvShort
}
if *timestampField == "@timestamp" {
timestampField = timestampShort
}
// Load config from file if provided
var config Config
if *configFile != "" {
file, err := os.ReadFile(*configFile)
if err != nil {
fmt.Println("Error reading config file:", err)
return
}
err = json.Unmarshal(file, &config)
if err != nil {
fmt.Println("Error parsing config file:", err)
return
}
} else {
config = Config{
URL: *url,
User: *user,
Password: *password,
IndexName: *indexName,
Size: *size,
CSVFile: *csvFile,
TimestampField: *timestampField,
}
}
// Validate required config
if config.URL == "" || config.IndexName == "" {
fmt.Println("\nURL and Index Name are required.")
return
}
if config.Size > 10000 {
fmt.Println("\nMaximum size is 10000 by OpenSearch API limitations.")
return
}
// Fetch data from OpenSearch
FetchData(config)
}