Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap custom commands on shell #387

Merged

Conversation

gusandrioli
Copy link
Contributor

Context

This solves #354

Implementation

Ideally, we'd re-use ICmdObjBuilder from lazygit across both projects, but that'd require a complete refactor here. So i ported the string replacement for Windows OS and the wrapping of new shell.

@gusandrioli
Copy link
Contributor Author

Here's the result using the issue's example:

(base) ➜  lazydocker git:(master) ✗ go run -mod=vendor main.go
docker-compose config --quiet

+ bash -c docker run -it -v "$(pwd)":/data redis

1:C 15 Oct 2022 19:27:44.265 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 15 Oct 2022 19:27:44.265 # Redis version=7.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 15 Oct 2022 19:27:44.265 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 15 Oct 2022 19:27:44.265 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 7.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

1:M 15 Oct 2022 19:27:44.266 # Server initialized
1:M 15 Oct 2022 19:27:44.269 * Ready to accept connections
^C1:signal-handler (1665862067) Received SIGINT scheduling shutdown...
1:M 15 Oct 2022 19:27:47.999 # User requested shutdown...
1:M 15 Oct 2022 19:27:47.999 * Saving the final RDB snapshot before exiting.
1:M 15 Oct 2022 19:27:48.022 * DB saved on disk
1:M 15 Oct 2022 19:27:48.023 # Redis is now ready to exit, bye bye...


Press enter to return to lazydocker (this prompt can be disabled in your config by setting `gui.returnImmediately: true`)

pkg/commands/os.go Show resolved Hide resolved
pkg/commands/os_test.go Outdated Show resolved Hide resolved
@gusandrioli
Copy link
Contributor Author

@jesseduffield This is ready, let me know your thoughts!

Copy link
Owner

@jesseduffield jesseduffield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, one comment.

Also we should remove RunDirectCommand given that's not being used and seemed to do something similar

@jesseduffield
Copy link
Owner

Also one thing we'll want to test @gusandrioli is whether an existing custom command which already used bash -c explicitly now breaks with this new code

@gusandrioli
Copy link
Contributor Author

Also one thing we'll want to test @gusandrioli is whether an existing custom command which already used bash -c explicitly now breaks with this new code

Good point, i'll test that out.

@gusandrioli
Copy link
Contributor Author

gusandrioli commented Oct 15, 2022

@jesseduffield It does fail! Thanks for catching that:

+ bash -c bash -c docker run -it -v "$(pwd)":/data redis


Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

One alternative is to leave the command as is if it contains bash -c . What are your thoughts on this?

pkg/commands/os.go Outdated Show resolved Hide resolved
@gusandrioli gusandrioli marked this pull request as draft October 15, 2022 22:00
Copy link
Contributor Author

@gusandrioli gusandrioli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jesseduffield Updated the config to have a invokeShell bool (default: false). Tested on both cases and worked fine.

Also, for these type of PRs with a lot of commits, do you prefer if i squash them or leave as is?

pkg/config/app_config.go Outdated Show resolved Hide resolved
pkg/gui/custom_commands.go Outdated Show resolved Hide resolved
@gusandrioli gusandrioli marked this pull request as ready for review October 16, 2022 19:06
Copy link
Owner

@jesseduffield jesseduffield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couple things

pkg/config/app_config.go Outdated Show resolved Hide resolved
@@ -50,10 +52,13 @@ func (gui *Gui) createCommandMenu(customCommands []config.CustomCommand, command
return option.customCommand.InternalFunction()
}

if option.invokeShell {
option.command = gui.OSCommand.NewCommandStringWithShell(option.command)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than mutate option.command here I would have a separate resolvedCommand variable that we use. This means that if we want to reuse options from outside this callback down the line there's no chance of the callback's code interfering

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Okay, makes sense.

@gusandrioli gusandrioli force-pushed the wrap-custom-cmds-on-shell branch from 3e8b584 to e44d50d Compare October 17, 2022 00:59
@gusandrioli
Copy link
Contributor Author

@jesseduffield Fixed :) Thanks for the feedback.

@jesseduffield
Copy link
Owner

Nice work :) I'll give this a local test soon

@gusandrioli
Copy link
Contributor Author

Hey @jesseduffield! Hope you're doing well. Are we still going to use this PR? If so, i can solve the conflicts.

@jesseduffield
Copy link
Owner

@gusandrioli sorry this fell through the cracks. I'll take another look this afternoon

@gusandrioli
Copy link
Contributor Author

@jesseduffield No worries at all, let me know :)

@jesseduffield jesseduffield force-pushed the wrap-custom-cmds-on-shell branch from e44d50d to d77b895 Compare December 1, 2022 08:05
@jesseduffield
Copy link
Owner

Code looks good, I've just fixed some merge conflicts

@jesseduffield
Copy link
Owner

can you confirm this still works on your end after I resolved those merge conflicts @gusandrioli ?

@gusandrioli
Copy link
Contributor Author

Hey @jesseduffield, just tested and it's working. Thanks for solving the conflicts!

Testing Results

Using the following config

customCommands:
  images:
    - name: tryout
      attach: true
      shell: false
      command: bash -c "docker run -it -v "$(pwd)":/data redis"
      serviceNames: []
    - name: tryout2
      attach: true
      shell: true
      command: docker run -v "$(pwd)":/data redis
      serviceNames: []

For tryout:
Screen Shot 2022-12-01 at 1 17 19 PM

And for tryout2:
Screen Shot 2022-12-01 at 1 17 52 PM

@jesseduffield jesseduffield merged commit 6025912 into jesseduffield:master Dec 13, 2022
@jesseduffield
Copy link
Owner

Merged. Thanks @gusandrioli !

@mark2185
Copy link
Contributor

mark2185 commented Feb 24, 2023

This broke the custom commands like these:

customCommands:
  containers:
    - name: bash
      attach: true
      command: 'docker exec -it {{ .Container.ID }} bash'
      serviceNames: []

Results with the error:

+ bash -c docker exec -it {{ .Container.ID }} bash

Error response from daemon: No such container: {{

Setting shell: false changes nothing.

@VaultVulp
Copy link

This changeset broke lazydocker completely for me. I hope the issue will be resolved, ASAP.

For now I've downgrade to 0.20.0 via homebrew using the following snippet:

wget https://raw.githubusercontent.com/jesseduffield/homebrew-lazydocker/b6d29bcd59058d1b7ec371df12f75d7048b40789/lazydocker.rb
brew uninstall lazydocker
brew install -s lazydocker.rb
rm ./lazydocker.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants