yats.git

ref: 0c76ad482d0f307584613068eff5c43ae46fe16d

client/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
/**
 * Yats - yats
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Paolo Lulli <kevwe.com>
 * @copyright Paolo Lulli 2024
 */
package main

import (
	"fmt"
	"os"
	"yats/config"

	flag "github.com/spf13/pflag"
)

type YatsClient struct {
	config config.ClientConfiguration
}

func main() {
	var configPath string

	if len(os.Args) == 1 {
		fmt.Printf("For usage: %s --help\n", os.Args[0])
		os.Exit(1)
	}

	configPath = os.Getenv("HOME") + "/.yats-client.json"

	cfg := config.GetClientConfig(configPath)
	var yatsClient = YatsClient{config: cfg}

	sourceOption := flag.StringP("source", "s", "", "Source Application")

	isMetricOption := flag.BoolP("metric", "m", false, "Metric Mode")
	isEventOption := flag.BoolP("event", "e", true, "Event Mode")
	isPositionOption := flag.BoolP("metric", "p", false, "Position Mode")

	writeModeOption := flag.BoolP("write", "w", false, "Write Mode")

	fromOption := flag.Int64("from", 0, "From tstamp")
	toOption := flag.Int64("to", 0, "To tstamp")
	metricNameOption := flag.StringP("metricName", "n", "", "Metric name")

	flag.Parse()

	if !*writeModeOption {
		if *isMetricOption {
			listResponse := yatsClient.MetricList(*sourceOption, *metricNameOption, *fromOption, *toOption)
			fmt.Printf("%s", listResponse)
		} else if *isPositionOption {
			listResponse := yatsClient.PositionList(*sourceOption, *fromOption, *toOption)
			fmt.Printf("%s", listResponse)
		} else {
			listEvents := yatsClient.EventList(*sourceOption, *fromOption, *toOption)
			fmt.Printf("%s", listEvents)
		}
	} else {
		// Write Mode
		if *isMetricOption {
			listResponse := yatsClient.MetricList(*sourceOption, *metricNameOption, *fromOption, *toOption)
			fmt.Printf("%s", listResponse)
		} else if *isPositionOption {
			listResponse := yatsClient.PositionList(*sourceOption, *fromOption, *toOption)
			fmt.Printf("%s", listResponse)
		} else {
			listEvents := yatsClient.EventList(*sourceOption, *fromOption, *toOption)
			fmt.Printf("%s", listEvents)
		}
	}
}