package main import ( "context" "encoding/json" "flag" "fmt" "os" "edge-infra.dev/pkg/lib/gcp/pubsub" "edge-infra.dev/pkg/lib/logging" ) var ( projectID = "" topicID = "" message = ` { "hello": "world" }` attrs = map[string]string{ // cushion provider attributes - leaving for ease of use // todo - define these in data sync code somewhere // "tenant_id": "", // banner bsl ID, must be an org // "db_name": "", // name of database to use, will create if it doesn't not exist // "entity_id": "", // random uuid, must use same ID when trying to delete // "data_source": "x-publisher-testing", // any string // "deleted": "false", // string form of bool } ) func main() { // use flags if provided // todo - add ability to pass in message as string or file flag.StringVar(&projectID, "projectID", projectID, "GCP project ID that the PubSub topic lives in") flag.StringVar(&topicID, "topicID", topicID, "GCP PubSub topic ID") flag.Parse() l := logging.NewLogger().WithValues("cmd", "x-publisher").Logger // validate required config is set if projectID == "" || topicID == "" { l.Error(fmt.Errorf("invalid configuration"), "must set values for 'projectID' and 'topicID'", "projectID", projectID, "topicID", topicID) os.Exit(1) } // validate message JSON msgBytes := []byte(message) var js json.RawMessage if err := json.Unmarshal(msgBytes, &js); err != nil { l.Error(err, "'message' is not valid json") os.Exit(1) } ctx := context.Background() manager, err := pubsub.New(ctx, projectID) if err != nil { l.Error(err, "failed to connect to gcp pubsub", "project", projectID) os.Exit(1) } l.Info("connected to gcp pubsub", "project", projectID) l.Info("sending message to topic", "project", projectID, "topic", topicID) if err := manager.Send(ctx, topicID, msgBytes, attrs); err != nil { l.Error(err, "failed to publish message", "project", projectID, "topic", topicID, "attributes", attrs) os.Exit(1) } l.Info("published message") }