github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/lib/messaging/helpers.go (about)

     1  package messagingLib
     2  
     3  import (
     4  	"github.com/taubyte/go-project-schema/messaging"
     5  	"github.com/taubyte/go-project-schema/project"
     6  	structureSpec "github.com/taubyte/go-specs/structure"
     7  	applicationLib "github.com/taubyte/tau-cli/lib/application"
     8  	"github.com/taubyte/utils/id"
     9  )
    10  
    11  type getter struct {
    12  	project     project.Project
    13  	application string
    14  	messaging   messaging.Messaging
    15  }
    16  
    17  func get(name string) (info getter, err error) {
    18  	info.project, info.application, err = applicationLib.SelectedProjectAndApp()
    19  	if err != nil {
    20  		return
    21  	}
    22  
    23  	info.messaging, err = info.project.Messaging(name, info.application)
    24  	if err != nil {
    25  		return
    26  	}
    27  
    28  	return
    29  }
    30  
    31  func list() (project project.Project, application string, channels []string, err error) {
    32  	project, application, err = applicationLib.SelectedProjectAndApp()
    33  	if err != nil {
    34  		return
    35  	}
    36  
    37  	local, global := project.Get().Messaging(application)
    38  	if len(application) > 0 {
    39  		channels = local
    40  	} else {
    41  		channels = global
    42  	}
    43  
    44  	return
    45  }
    46  
    47  func set(messaging *structureSpec.Messaging, new bool) error {
    48  	info, err := get(messaging.Name)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	if new {
    54  		messaging.Id = id.Generate(info.project.Get().Id(), messaging.Name)
    55  	}
    56  
    57  	return info.messaging.SetWithStruct(true, messaging)
    58  }