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

     1  package applicationLib
     2  
     3  import (
     4  	"github.com/taubyte/go-project-schema/application"
     5  	"github.com/taubyte/go-project-schema/project"
     6  	structureSpec "github.com/taubyte/go-specs/structure"
     7  	"github.com/taubyte/tau-cli/env"
     8  	projectLib "github.com/taubyte/tau-cli/lib/project"
     9  	"github.com/taubyte/tau-cli/singletons/session"
    10  	"github.com/taubyte/utils/id"
    11  	"github.com/urfave/cli/v2"
    12  )
    13  
    14  func SelectedProjectAndApp() (project project.Project, selectedApp string, err error) {
    15  	project, err = projectLib.SelectedProjectInterface()
    16  	if err != nil {
    17  		return
    18  	}
    19  
    20  	// Returns a boolean for existence
    21  	selectedApp, _ = env.GetSelectedApplication()
    22  
    23  	return
    24  }
    25  
    26  func List() ([]string, error) {
    27  	project, err := projectLib.SelectedProjectInterface()
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	return project.Get().Applications(), nil
    33  }
    34  
    35  func ListResources() ([]*structureSpec.App, error) {
    36  	names, err := List()
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	if len(names) == 0 {
    41  		return nil, nil
    42  	}
    43  
    44  	resources := make([]*structureSpec.App, len(names))
    45  	for idx, name := range names {
    46  		resources[idx], err = Get(name)
    47  		if err != nil {
    48  			return nil, err
    49  		}
    50  	}
    51  
    52  	return resources, nil
    53  }
    54  
    55  func Get(name string) (*structureSpec.App, error) {
    56  	project, err := projectLib.SelectedProjectInterface()
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	app, err := project.Application(name)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	getter := app.Get()
    66  	return &structureSpec.App{
    67  		Id:          getter.Id(),
    68  		Name:        getter.Name(),
    69  		Description: getter.Description(),
    70  		Tags:        getter.Tags(),
    71  	}, nil
    72  }
    73  
    74  func Set(app *structureSpec.App) error {
    75  	project, err := projectLib.SelectedProjectInterface()
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	_app, err := project.Application(app.Name)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	return _app.Set(true,
    86  		application.Description(app.Description),
    87  		application.Tags(app.Tags),
    88  	)
    89  }
    90  
    91  func Select(ctx *cli.Context, name string) error {
    92  	return env.SetSelectedApplication(ctx, name)
    93  }
    94  
    95  func Deselect(ctx *cli.Context, name string) error {
    96  	return session.Unset().SelectedApplication()
    97  }
    98  
    99  func New(app *structureSpec.App) error {
   100  	project, err := projectLib.SelectedProjectInterface()
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	_app, err := project.Application(app.Name)
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	return _app.Set(true,
   111  		application.Id(id.Generate(project.Get().Id(), app.Name)),
   112  		application.Description(app.Description),
   113  		application.Tags(app.Tags),
   114  	)
   115  }
   116  
   117  func Delete(app *structureSpec.App) error {
   118  	project, err := projectLib.SelectedProjectInterface()
   119  	if err != nil {
   120  		return err
   121  	}
   122  
   123  	_app, err := project.Application(app.Name)
   124  	if err != nil {
   125  		return err
   126  	}
   127  
   128  	return _app.Delete()
   129  }