yats.git

ref: afde882c4708403ba71acf27b1791be2fdc52fda

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
 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
121
122
123
124
125
126
127
/**
 * 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}

	generatePki := flag.BoolP("csr", "c", false, "Create Certificate Signing Request")

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

	isShowPermsOption := flag.BoolP("show-permissions", "S", false, "Show User Permissions")

	fromOption := flag.Int64("from", 0, "From tstamp")
	toOption := flag.Int64("to", 0, "To tstamp")

	timestampOption := flag.Int64("timestamp", 0, "Metric/Event TimeStamp")

	writeEvent := flag.StringP("write-event", "E", "", "Write Event Name")
	writeMetric := flag.StringP("write-metric", "M", "", "Write Metric Name")
	metricValue := flag.StringP("metric-value", "v", "", "Metric Value")
	writePosition := flag.StringP("write-position", "P", "", "Write Position Name")

	listEvents := flag.BoolP("list-events", "e", false, "List Events")
	listMetrics := flag.StringP("list-metrics", "m", "", "List Metrics with Name")
	listPositions := flag.StringP("list-positions", "p", "", "List Positions with Name")

	longitudeOption := flag.Float64P("longitude", "o", 0, "Longitude")
	latitudeOption := flag.Float64P("latitude", "a", 0, "Latitude")

	flag.Parse()

	if *isShowPermsOption {
		listResponse, err := yatsClient.PermissionsList()
		if err == nil {
			fmt.Printf("%s", listResponse)
		} else {
			fmt.Println(err)
		}
		os.Exit(0)
	}

	if *generatePki {
		yatsClient.CreateCsr()
		os.Exit(0)
	}

	if "" != *writeEvent {
		saveEventResponse, err := yatsClient.EventSave(*writeEvent, *timestampOption)
		if err == nil {
			fmt.Printf("%s", saveEventResponse)
		} else {
			fmt.Println(err)
		}
		os.Exit(0)
	}

	if "" != *writeMetric {
		listResponse, err := yatsClient.MetricSave(*writeMetric, *metricValue, *timestampOption)
		if err == nil {
			fmt.Printf("%s", listResponse)
		} else {
			fmt.Println(err)
		}
		os.Exit(0)
	}
	if "" != *writePosition {
		savePositionResponse, err := yatsClient.PositionSave(*writePosition, *latitudeOption, *longitudeOption, *timestampOption)
		if err == nil {
			fmt.Printf("%s", savePositionResponse)
		} else {
			fmt.Println(err)
		}
		os.Exit(0)
	}

	if *listEvents {
		listEvents := yatsClient.EventList(*sourceOption, *fromOption, *toOption)
		fmt.Printf("%s", listEvents)
		os.Exit(0)
	}

	if "" != *listMetrics {
		listResponse, err := yatsClient.MetricList(*sourceOption, *listMetrics, *fromOption, *toOption)
		if err == nil {
			fmt.Printf("%s", listResponse)
		} else {
			fmt.Println(err)
		}
		os.Exit(0)
	}

	if "" != *listPositions {
		listResponse := yatsClient.PositionList(*sourceOption, *fromOption, *toOption)
		fmt.Printf("%s", listResponse)
		os.Exit(0)
	}
}