suborbital / subo

The Suborbital CLI
Apache License 2.0
81 stars 25 forks source link

subo create runnable flag for directly exposing on Directive #174

Open flaki opened 2 years ago

flaki commented 2 years ago

Currently subo create runnable doesn't touch the Directive at all.

In certain situations it might be useful to append the newly created runnable to the Directive and expose it as a handler straight away. This is a functionality that is being added as #134 as a separate command -- but it might be that it would make sense also as a flag on create runnable.

This flag, say subo create runnable X --handler would create the runnable X and append it to the directive as a handler /X. Alternatively if specified as ... --handler foo, that would set the handler to /foo instead.

The equivalent currently via shell is

# create the runnable
subo create runnable X
tee -a Directive.yaml << EOF
  - type: request
    method: GET
    resource: /X
    steps:
      - fn: X
EOF
yashikajotwani12 commented 2 years ago

I want to work on this.

javorszky commented 2 years ago

The task is to amend the existing subo create runnable command, so it accepts a flag.

The flag is --handler, it should have an optional argument, and it should NOT have a shorthand.

The expectations are:

1. subo create runnable doSomething

This works as expected, creates a directory named doSomething. This is how the current command works. This should not change.

2. subo create runnable doSomething --handler

Creates the directory and necessary files in the doSomething directory. On top of that, it ALSO modifies the root level Directive.yaml file by adding a new entry to the handlers array for a type request, method GET, resource /doSomething, ie the same as the runnable name with a / prepended.

3. subo create runnable doSomthing --handler /endpoint

Creates the directory and necessary files in the doSomething directory.

On top that, it also modifies the root level Directive.yaml file by adding a new entry to the handlers array for type: request, method: GET, and resource: /endpoint; the same as the argument to the --handler flag.

Not part of this issue

Suggestions

One way this can be solved is to invoke the subo create handler command from within the running create runnable command and let that take care of manipulating the Directive.yaml file.

Another way to do it is to parse the existing yaml file into a struct, add the required handler to the struct, and then write that struct into a yaml file replacing the entire contents of it. There's code on how to do this in the create handler command.

javorszky commented 2 years ago

Cobra documentations:

https://cobra.dev/

https://github.com/spf13/cobra