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

     1  package functionLib
     2  
     3  import (
     4  	"os"
     5  
     6  	schemaCommon "github.com/taubyte/go-project-schema/common"
     7  	"github.com/taubyte/go-project-schema/project"
     8  	structureSpec "github.com/taubyte/go-specs/structure"
     9  	"github.com/taubyte/tau-cli/lib/codefile"
    10  )
    11  
    12  func New(function *structureSpec.Function, templateURL string) error {
    13  	_, err := set(function, true)
    14  	if err != nil {
    15  		return err
    16  	}
    17  
    18  	codePath, err := codefile.Path(function.Name, schemaCommon.FunctionFolder)
    19  	if err != nil {
    20  		return err
    21  	}
    22  
    23  	return codePath.Write(templateURL, function.Name)
    24  }
    25  
    26  func Set(function *structureSpec.Function) (err error) {
    27  	_, err = set(function, false)
    28  	return
    29  }
    30  
    31  func Delete(name string) error {
    32  	info, err := get(name)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	err = info.function.Delete()
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	codePath, err := codefile.Path(name, schemaCommon.FunctionFolder)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	return os.RemoveAll(codePath.String())
    48  }
    49  
    50  func List() ([]string, error) {
    51  	_, _, functions, err := list()
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	return functions, nil
    57  }
    58  
    59  func ListResources() ([]*structureSpec.Function, error) {
    60  	project, application, relative, err := list()
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	functions := make([]*structureSpec.Function, len(relative))
    66  	for idx, name := range relative {
    67  		function, err := project.Function(name, application)
    68  		if err != nil {
    69  			return nil, err
    70  		}
    71  
    72  		functions[idx], err = function.Get().Struct()
    73  		if err != nil {
    74  			return nil, err
    75  		}
    76  	}
    77  
    78  	return functions, nil
    79  }
    80  
    81  func ProjectFunctionCount(project project.Project) (functionCount int) {
    82  	_, global := project.Get().Functions("")
    83  	functionCount += len(global)
    84  
    85  	for _, app := range project.Get().Applications() {
    86  		local, _ := project.Get().Functions(app)
    87  		functionCount += len(local)
    88  	}
    89  
    90  	return
    91  }