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

     1  package libraryLib
     2  
     3  import (
     4  	"github.com/taubyte/go-project-schema/libraries"
     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  	library     libraries.Library
    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.library, err = info.project.Library(name, info.application)
    24  	if err != nil {
    25  		return
    26  	}
    27  
    28  	return
    29  }
    30  
    31  func list() (project project.Project, application string, libraries []string, err error) {
    32  	project, application, err = applicationLib.SelectedProjectAndApp()
    33  	if err != nil {
    34  		return
    35  	}
    36  
    37  	local, global := project.Get().Libraries(application)
    38  	if len(application) > 0 {
    39  		libraries = local
    40  	} else {
    41  		libraries = global
    42  	}
    43  
    44  	return
    45  }
    46  
    47  func set(_library *structureSpec.Library, new bool) error {
    48  	info, err := get(_library.Name)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	if new {
    54  		_library.Id = id.Generate(info.project.Get().Id(), _library.Name)
    55  	}
    56  
    57  	err = info.library.SetWithStruct(false, _library)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	return info.library.Set(true,
    63  		libraries.Github(_library.RepoID, _library.RepoName),
    64  	)
    65  }