From 37fee157dd103a47e268b35b7f91bb37f6c7a4ed Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Wed, 23 Aug 2023 18:21:12 +0800 Subject: [PATCH] add `message-bus-codegen` CLI tool for generating a markdown of all event types (#1346) --- cmd/message-bus-docgen/main.go | 28 ++++++++++++++++++++++++++++ common/message.go | 23 ++++++----------------- go.mod | 4 ++-- go.sum | 4 ++-- main.go | 8 ++------ 5 files changed, 40 insertions(+), 27 deletions(-) create mode 100644 cmd/message-bus-docgen/main.go diff --git a/cmd/message-bus-docgen/main.go b/cmd/message-bus-docgen/main.go new file mode 100644 index 0000000..a0649ea --- /dev/null +++ b/cmd/message-bus-docgen/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "github.com/IceWhaleTech/CasaOS-Common/external" + "github.com/IceWhaleTech/CasaOS/codegen/message_bus" + "github.com/IceWhaleTech/CasaOS/common" + "github.com/samber/lo" +) + +func main() { + eventTypes := lo.Map(common.EventTypes, func(item message_bus.EventType, index int) external.EventType { + return external.EventType{ + Name: item.Name, + SourceID: item.SourceID, + PropertyTypeList: lo.Map( + item.PropertyTypeList, func(item message_bus.PropertyType, index int) external.PropertyType { + return external.PropertyType{ + Name: item.Name, + Description: item.Description, + Example: item.Example, + } + }, + ), + } + }) + + external.PrintEventTypesAsMarkdown(common.SERVICENAME, common.VERSION, eventTypes) +} diff --git a/common/message.go b/common/message.go index 5e41a25..6aa6204 100644 --- a/common/message.go +++ b/common/message.go @@ -1,23 +1,12 @@ package common import ( - "fmt" - "github.com/IceWhaleTech/CasaOS/codegen/message_bus" ) -var ( - // devtype -> action -> event - EventTypes map[string]map[string]message_bus.EventType - - PropertyNameLookupMaps = map[string]map[string]string{ - "system": { - fmt.Sprintf("%s:%s", SERVICENAME, "utilization"): "ID_BUS", - }, - } - - ActionPastTense = map[string]string{ - "add": "added", - "remove": "removed", - } -) +// devtype -> action -> event +var EventTypes = []message_bus.EventType{ + {Name: "casaos:system:utilization", SourceID: SERVICENAME, PropertyTypeList: []message_bus.PropertyType{}}, + {Name: "casaos:file:recover", SourceID: SERVICENAME, PropertyTypeList: []message_bus.PropertyType{}}, + {Name: "casaos:file:operate", SourceID: SERVICENAME, PropertyTypeList: []message_bus.PropertyType{}}, +} diff --git a/go.mod b/go.mod index 6b1a91a..0ce12f4 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d - github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha8 + github.com/IceWhaleTech/CasaOS-Common v0.4.7-alpha4 github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf github.com/deckarep/golang-set/v2 v2.3.0 github.com/deepmap/oapi-codegen v1.12.4 @@ -33,6 +33,7 @@ require ( github.com/patrickmn/go-cache v2.1.0+incompatible github.com/pkg/errors v0.9.1 github.com/robfig/cron/v3 v3.0.1 + github.com/samber/lo v1.38.1 github.com/satori/go.uuid v1.2.0 github.com/shirou/gopsutil/v3 v3.23.2 github.com/sirupsen/logrus v1.9.0 @@ -106,7 +107,6 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect - github.com/samber/lo v1.38.1 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect github.com/tklauser/go-sysconf v0.3.11 // indirect diff --git a/go.sum b/go.sum index 2055a06..f8bc3e9 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d h1:62lEBImTxZ83pgzywgDNIrPPuQ+j4ep9QjqrWBn1hrU= github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d/go.mod h1:lW9x+yEjqKdPbE3+cf2fGPJXCw/hChX3Omi9QHTLFsQ= -github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha8 h1:UhCg3d9Cxhx7KVmqh8oUrUl1qFmFdcHee3Zkk4+P2JA= -github.com/IceWhaleTech/CasaOS-Common v0.4.4-alpha8/go.mod h1:2IuYyy5qW1BE6jqC6M+tOU+WtUec1K565rLATBJ9p/0= +github.com/IceWhaleTech/CasaOS-Common v0.4.7-alpha4 h1:bsVQ+kdVkmPsqX6lxxETaShiMb9I8OgiR4xrEQ/vyss= +github.com/IceWhaleTech/CasaOS-Common v0.4.7-alpha4/go.mod h1:2IuYyy5qW1BE6jqC6M+tOU+WtUec1K565rLATBJ9p/0= github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= diff --git a/main.go b/main.go index 8e3cca1..cb527f4 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,6 @@ import ( util_http "github.com/IceWhaleTech/CasaOS-Common/utils/http" - "github.com/IceWhaleTech/CasaOS/codegen/message_bus" "github.com/IceWhaleTech/CasaOS/common" "github.com/IceWhaleTech/CasaOS/pkg/cache" "github.com/IceWhaleTech/CasaOS/pkg/config" @@ -161,13 +160,10 @@ func main() { panic(err) } } - var events []message_bus.EventType - events = append(events, message_bus.EventType{Name: "casaos:system:utilization", SourceID: common.SERVICENAME, PropertyTypeList: []message_bus.PropertyType{}}) - events = append(events, message_bus.EventType{Name: "casaos:file:recover", SourceID: common.SERVICENAME, PropertyTypeList: []message_bus.PropertyType{}}) - events = append(events, message_bus.EventType{Name: "casaos:file:operate", SourceID: common.SERVICENAME, PropertyTypeList: []message_bus.PropertyType{}}) + // register at message bus for i := 0; i < 10; i++ { - response, err := service.MyService.MessageBus().RegisterEventTypesWithResponse(context.Background(), events) + response, err := service.MyService.MessageBus().RegisterEventTypesWithResponse(context.Background(), common.EventTypes) if err != nil { logger.Error("error when trying to register one or more event types - some event type will not be discoverable", zap.Error(err)) }