tilt-dev / tilt-extensions

Extensions for Tilt
https://tilt.dev/
Apache License 2.0
201 stars 159 forks source link

Being able to call a function defined in the Tiltfile #535

Closed Vanderscycle closed 10 months ago

Vanderscycle commented 10 months ago

Hey,

I love tilt, and I was hoping to use the ui to make small changes to the state of tilt without having to go back to the file. One example is to switch between localhost/infrastructure where one is dev and the other is running using all of the k8s manifets.

# Custom UI
load('ext://uibutton', 'cmd_button', 'location', 'text_input')

# Define the available modes and an initial selection
modes = ['localhost', 'infrastructure']
selection = modes[0]

def toggle_modes(button, session, status):
    if selection == modes[0]:
        selection = modes[1]
    else:
        selection = modes[0]

cmd_button(
    name='toggle-button',
    argv=['python', '-c', toggle_modes],
    text='Toggle Bool',
    location=location.NAV
)

Its possible that I missed how to do it in the docs.

nicks commented 10 months ago

You can't call tiltfile functions from a shell script. Here's a good doc on tilt's execution model: https://docs.tilt.dev/controlloop

You can do this with the tiltfile API. For example for example, i have this in my tiltfile:

   cmd_button(name='frontend-dev',
              text='Frontend Dev',
              icon_name='bolt',
              location=location.NAV,
              argv=['tilt', 'patch', 'tiltfile', '(Tiltfile)', '--patch', '{"spec": {"args": ["--frontend=dev"]}}'])
   cmd_button(name='frontend-prod',
              text='Frontend Prod',
              icon_name='local_shipping',
              location=location.NAV,
              argv=['tilt', 'patch', 'tiltfile', '(Tiltfile)', '--patch', '{"spec": {"args": ["--frontend=prod"]}}'])
   local_resource('choose-a-mode', ['echo', """Must specify 'tilt up -- --frontend=dev' or 'tilt up -- --frontend=prod'
 Click the bolt for --frontend=dev.
 Click the truck for --frontend=prod.
 """])

which toggles where it runs the frontend in a local_resource or remotely