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

Show Docker Context #621

Open
mattcen opened this issue Sep 26, 2020 · 6 comments
Open

Show Docker Context #621

mattcen opened this issue Sep 26, 2020 · 6 comments
Labels
dev env Data sources relating to development environments enhancement Feature request

Comments

@mattcen
Copy link

mattcen commented Sep 26, 2020

Docker now features contexts which allow the user to use Docker setups on remote hosts etc. For people who use this, it's useful to list in the prompt what context is currently active, so they know which environment Docker commands will affect.

Description

The prompt should show, somewhere, the name of the current Docker context, if it's not the default context.

How will this be useful?

To show which Docker environment will be affected by Docker commands

Example prompt

I'm not sure where best to display it, but somewhere in the prompt, with some appropriate symbol, display the text name of the current context. In the past, I've displayed this as (🐳myContext), but not all terminals support emoji, so perhaps a new symbol makes sense.

I've written some sample code to determine the current Docker context. If we're in the default context, the functions returns the empty string. Note that jq is required to parse the Docker config file, hence _lp_require_tool.

_lp_require_tool DOCKER_CONTEXT jq
 
_lp_docker_context()
{
    (( LP_ENABLE_DOCKER_CONTEXT )) || return
     local context
     context=$(jq -r .currentContext ~/.docker/config.json | grep -v null)
     echo "$context"
}
@mattcen mattcen added the enhancement Feature request label Sep 26, 2020
@Rycieos
Copy link
Collaborator

Rycieos commented Sep 26, 2020

This is different than #569 right? Docker context is similar to Kubernetes context, not about which container you are in.

This is a valid request, and even better than you have included a suggested implementation. I don't know about using jq, but as we enter the post-json era, probably it will be needed for more parsing anyway, so I might accept that.

As with other features, this will have to wait until the rework is done, as Liquidprompt is currently too slow to accept any new features. Even disabled, each added feature will slow the prompt with how it works right now. But with an example implementation, this should be quick to implement.

@Rycieos Rycieos added the dev env Data sources relating to development environments label Sep 26, 2020
@Rycieos Rycieos added this to the v2.1 milestone Sep 26, 2020
@mattcen
Copy link
Author

mattcen commented Sep 27, 2020

Correct; different issue to #569, and yes, similar to Kubernetes context I believe. Understood re rework; just wanted to follow the issue while I remembered. Also understand re jq; I tried to think of other ways to parse the json, but they're all hacks. Could probably fall back to Python which may be more likely to be installed, but that'd involve some nasty inline Python code.

@Rycieos
Copy link
Collaborator

Rycieos commented Sep 27, 2020

Startup time of external commands will need to be compared. My guess is that jq will start up faster than most other tools.

@Rycieos
Copy link
Collaborator

Rycieos commented Apr 26, 2022

Sorry for the radio silence on this.

I do not want to use jq. Nothing else in Liquidprompt depends on it, and it would make more sense to use nothing other than docker itself.

Unfortunately, docker context does not have any subcommand to print the current context. But, it does have docker context inspect, which (while not documented) does allow for calling it without an argument, and then it returns the data for the current context.

So, using this:

local context="$(docker context inspect)"
context="${context#*'"Name": "'}"
context="${context%%'",'*}"

We can get the name of the context. It's hacky, but we don't need any external tools.

Thoughts?

@mattcen
Copy link
Author

mattcen commented Apr 26, 2022

I tend to try to avoid using hacky approaches like this, but I no longer use liquidprompt, so in this case I'm entirely indifferent.

@Rycieos
Copy link
Collaborator

Rycieos commented Apr 27, 2022

Thanks for the honest feedback.

In that case, I will leave this open to see if it gathers other interest, but will not be working on a solution myself until then.

@Rycieos Rycieos removed this from the v2.1 milestone Apr 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev env Data sources relating to development environments enhancement Feature request
Projects
None yet
Development

No branches or pull requests

2 participants