/** * Yats - yats * * This file is licensed under the Affero General Public License version 3 or * later. See the COPYING file. * * @author Paolo Lulli * @copyright Paolo Lulli 2024 */ package main import ( "fmt" "os" "time" "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 CSR") 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, "timeStamp") writeEvent := flag.StringP("write-event", "E", "", "write event with name") writeMetric := flag.StringP("write-metric", "M", "", "write metric with 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") sinceSeconds := flag.BoolP("sec", "", false, "seconds ago") sinceMinutes := flag.BoolP("min", "", false, "minutes ago") sinceHours := flag.BoolP("hour", "", false, "hours ago") sinceDays := flag.BoolP("day", "", false, "days ago") sinceYears := flag.BoolP("year", "", false, "years ago") flag.Parse() var actualTimestamp int64 if 0 != *timestampOption { if *sinceSeconds { actualTimestamp = time.Now().Unix() - *timestampOption } if *sinceMinutes { actualTimestamp = time.Now().Unix() - *timestampOption*60 } if *sinceHours { actualTimestamp = time.Now().Unix() - *timestampOption*60*60 } if *sinceDays { actualTimestamp = time.Now().Unix() - *timestampOption*60*60*24 } if *sinceYears { actualTimestamp = time.Now().Unix() - *timestampOption*60*60*24*365 } } 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, actualTimestamp) if err == nil { fmt.Printf("%s", saveEventResponse) } else { fmt.Println(err) } os.Exit(0) } if "" != *writeMetric { listResponse, err := yatsClient.MetricSave(*writeMetric, *metricValue, actualTimestamp) if err == nil { fmt.Printf("%s", listResponse) } else { fmt.Println(err) } os.Exit(0) } if "" != *writePosition { savePositionResponse, err := yatsClient.PositionSave(*writePosition, *latitudeOption, *longitudeOption, actualTimestamp) 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) } }