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

     1  package functionPrompts
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  
     7  	"github.com/taubyte/go-project-schema/functions"
     8  	structureSpec "github.com/taubyte/go-specs/structure"
     9  	"github.com/taubyte/tau-cli/flags"
    10  	functionFlags "github.com/taubyte/tau-cli/flags/function"
    11  	"github.com/taubyte/tau-cli/prompts"
    12  	"github.com/taubyte/tau-cli/singletons/templates"
    13  	"github.com/urfave/cli/v2"
    14  )
    15  
    16  // Should only be used in new, will overwrite values.
    17  func checkTemplate(ctx *cli.Context, function *structureSpec.Function) (templateURL string, err error) {
    18  	if !ctx.IsSet(flags.Template.Name) && !prompts.GetUseACodeTemplate(ctx) {
    19  		return
    20  	}
    21  
    22  	language, err := prompts.GetOrSelectLanguage(ctx)
    23  	if err != nil {
    24  		return
    25  	}
    26  
    27  	codeTemplateMap, err := templates.Get().Function(language)
    28  	if err != nil {
    29  		return
    30  	}
    31  
    32  	templateURL, err = prompts.SelectATemplate(ctx, codeTemplateMap)
    33  	if err != nil {
    34  		return
    35  	}
    36  
    37  	file, err := os.ReadFile(path.Join(templateURL, "config.yaml"))
    38  	if err != nil {
    39  		return
    40  	}
    41  
    42  	getter, err := functions.Yaml(function.Name, "", file)
    43  	if err != nil {
    44  		return
    45  	}
    46  
    47  	_function, err := getter.Struct()
    48  	if err != nil {
    49  		return
    50  	}
    51  
    52  	// Overwrite new function
    53  	*function = *_function
    54  
    55  	return
    56  }
    57  
    58  func editHttp(ctx *cli.Context, function *structureSpec.Function) (err error) {
    59  	function.Domains, err = prompts.GetOrSelectDomainsWithFQDN(ctx, function.Domains...)
    60  	if err != nil {
    61  		return
    62  	}
    63  
    64  	function.Method, err = GetHttpMethod(ctx, function.Method)
    65  	if err != nil {
    66  		return
    67  	}
    68  
    69  	function.Paths = prompts.RequiredPaths(ctx, function.Paths...)
    70  	return
    71  }
    72  
    73  func editP2P(ctx *cli.Context, function *structureSpec.Function) (err error) {
    74  	function.Protocol, err = prompts.SelectAServiceWithProtocol(ctx, functionFlags.Protocol.Name, ProtocolPrompt, function.Protocol)
    75  	if err != nil {
    76  		return
    77  	}
    78  
    79  	function.Command = GetOrRequireACommand(ctx, function.Command)
    80  
    81  	function.Local = prompts.GetOrAskForLocal(ctx, function.Local)
    82  	return
    83  }
    84  
    85  func editPubSub(ctx *cli.Context, function *structureSpec.Function) (err error) {
    86  	function.Channel = GetOrRequireAChannel(ctx, function.Channel)
    87  	function.Local = prompts.GetOrAskForLocal(ctx, function.Local)
    88  	return
    89  }