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

     1  package smartopsLib
     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(smartops *structureSpec.SmartOp, templateURL string) error {
    13  	_, err := set(smartops, true)
    14  	if err != nil {
    15  		return err
    16  	}
    17  
    18  	codePath, err := codefile.Path(smartops.Name, schemaCommon.SmartOpsFolder)
    19  	if err != nil {
    20  		return err
    21  	}
    22  
    23  	return codePath.Write(templateURL, smartops.Name)
    24  }
    25  
    26  func Set(smartops *structureSpec.SmartOp) (err error) {
    27  	_, err = set(smartops, 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.smartops.Delete()
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	codePath, err := codefile.Path(name, schemaCommon.SmartOpsFolder)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	return os.RemoveAll(codePath.String())
    48  }
    49  
    50  func List() ([]string, error) {
    51  	_, _, smartops, err := list()
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	return smartops, nil
    57  }
    58  
    59  func ListResources() ([]*structureSpec.SmartOp, error) {
    60  	project, application, relative, err := list()
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	smartops := make([]*structureSpec.SmartOp, len(relative))
    66  	for idx, name := range relative {
    67  		_smartops, err := project.SmartOps(name, application)
    68  		if err != nil {
    69  			return nil, err
    70  		}
    71  
    72  		smartops[idx], err = _smartops.Get().Struct()
    73  		if err != nil {
    74  			return nil, err
    75  		}
    76  	}
    77  
    78  	return smartops, nil
    79  }
    80  
    81  func ProjectSmartOpCount(project project.Project) (smartopsCount int) {
    82  	_, global := project.Get().SmartOps("")
    83  	smartopsCount += len(global)
    84  
    85  	for _, app := range project.Get().Applications() {
    86  		local, _ := project.Get().SmartOps(app)
    87  		smartopsCount += len(local)
    88  	}
    89  
    90  	return
    91  }