Skip to content

Commit

Permalink
Improve user-experience for local run
Browse files Browse the repository at this point in the history
When there is only one function within the YAML file, run that.

Print the address for the local web server.

Tested whilst building a Discord bot

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Feb 8, 2023
1 parent c78e5ce commit 02ce455
Showing 1 changed file with 44 additions and 25 deletions.
69 changes: 44 additions & 25 deletions commands/local_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const localSecretsDir = ".secrets"

func init() {
faasCmd.AddCommand(newLocalRunCmd())

}

type runOptions struct {
Expand All @@ -36,13 +35,13 @@ func newLocalRunCmd() *cobra.Command {
cmd := &cobra.Command{
Use: `local-run NAME --port PORT -f YAML_FILE`,
Short: "Start a function with docker for local testing (experimental feature)",
Long: `Providing faas-cli build has already been run, this command will use the
Long: `Providing faas-cli build has already been run, this command will use the
docker command to start a container on your local machine using its image.
The function will be bound to the port specified by the --port flag, or 8080
by default.
There is limited support for secrets, and the function cannot contact other
There is limited support for secrets, and the function cannot contact other
services deployed within your OpenFaaS cluster.`,
Example: `
# Run a function locally
Expand All @@ -60,10 +59,6 @@ services deployed within your OpenFaaS cluster.`,
return fmt.Errorf("this command is experimental, set OPENFAAS_EXPERIMENTAL=1 to use it")
}

if len(args) < 1 {
return fmt.Errorf("expected the name of the function")
}

if len(args) > 1 {
return fmt.Errorf("only one function name is allowed")
}
Expand All @@ -75,9 +70,14 @@ services deployed within your OpenFaaS cluster.`,
opts.output = cmd.OutOrStdout()
opts.err = cmd.ErrOrStderr()

return runFunction(ctx, args[0], opts)
name := ""
if len(args) > 0 {
name = args[0]
}

return runFunction(ctx, name, opts, args)
},
// TODO: unhide once we are happy with the DX.
// AE: show in commands list once we are happy with the developer experience.
Hidden: true,
}

Expand All @@ -89,26 +89,45 @@ services deployed within your OpenFaaS cluster.`,
return cmd
}

func runFunction(ctx context.Context, name string, opts runOptions) error {
services, err := stack.ParseYAMLFile(yamlFile, "", name, true)
if err != nil {
return err
}
func runFunction(ctx context.Context, name string, opts runOptions, args []string) error {
var services *stack.Services

if len(services.Functions) > 1 {
return fmt.Errorf("multiple functions matching %q in the stack file", name)
}
if len(name) == 0 {
s, err := stack.ParseYAMLFile(yamlFile, "", "", true)
if err != nil {
return err
}

err = updateGitignore()
if err != nil {
return err
}
services = s

if len(services.Functions) == 0 {
return fmt.Errorf("no functions found in the stack file")
}
if len(services.Functions) > 1 {
fnList := []string{}
for key := range services.Functions {
fnList = append(fnList, key)
}
return fmt.Errorf("give a function name to run: %v", fnList)
}

fnc := services.Functions[name]
// TODO: we should probably use a levelled logger here
// fmt.Fprintf(opts.output, "%#v\n\n", fnc)
for key := range services.Functions {
name = key
break
}
} else {
s, err := stack.ParseYAMLFile(yamlFile, "", name, true)
if err != nil {
return err
}
services = s

if len(services.Functions) == 0 {
return fmt.Errorf("no functions matching %q in the stack file", name)
}
}

cmd, err := buildDockerRun(ctx, fnc, opts)
cmd, err := buildDockerRun(ctx, services.Functions[name], opts)
if err != nil {
return err
}
Expand Down

0 comments on commit 02ce455

Please sign in to comment.