Skip to content

Commit

Permalink
Get current context from docker config
Browse files Browse the repository at this point in the history
Signed-off-by: Anca Iordache <anca.iordache@docker.com>
  • Loading branch information
aiordache committed Jan 22, 2020
1 parent 046da6c commit 8f99eb5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
22 changes: 14 additions & 8 deletions docker/context/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import os

from docker import errors
from docker.context.config import METADATA_DIR, METAFILE
from docker.context.config import (
METADATA_DIR, METAFILE, get_current_context_name
)
from docker.context import Context


Expand Down Expand Up @@ -93,6 +95,16 @@ def contexts(cls):
contexts.append(Context.load_context(name))
return contexts

@classmethod
def get_current_context(cls):
"""Context list.
Returns:
(Context): current context object.
"""
# get current context from docker config
name = get_current_context_name()
return ContextAPI.get_context(name)

@classmethod
def remove_context(cls, name):
"""Remove a context. Similar to the ``docker context rm`` command.
Expand Down Expand Up @@ -132,10 +144,6 @@ def get_context(cls, name="default"):
Args:
name (str): The name of the context
Raises:
:py:class:`docker.errors.MissingContextParameter`
If a context name is not provided.
Example:
>>> from docker.context import ContextAPI
Expand All @@ -154,9 +162,7 @@ def get_context(cls, name="default"):
}
}
"""
if not name:
raise errors.MissingContextParameter("name")
if name == "default":
if not name or name == "default":
return ContextAPI.DEFAULT_CONTEXT
# load the context to check if it already exists
ctx = Context.load_context(name)
Expand Down
8 changes: 8 additions & 0 deletions docker/context/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import json
import hashlib
from .. import utils

Expand All @@ -9,6 +10,13 @@
METAFILE = "meta.json"


def get_current_context_name():
docker_config = utils.find_docker_config_file()
if docker_config:
return json.load(docker_config).get("currentContext")
return None


def get_context_id(name):
return hashlib.sha256(name.encode('utf-8')).hexdigest()

Expand Down
6 changes: 6 additions & 0 deletions tests/integration/context_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
class ContextLifecycleTest(BaseAPIIntegrationTest):
def test_lifecycle(self):
# check there is no context 'test'
ctx = ContextAPI.get_context()
assert ctx is not None and ctx.Name is "default"

ctx = ContextAPI.get_context("test")
assert ctx is None

ctx = ContextAPI.get_current_context()
assert ctx is not None

success = False
dirpath = tempfile.mkdtemp()
ca = tempfile.NamedTemporaryFile(
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/context_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def test_default_in_context_list(self):
found = True
assert found is True

def test_get_current_context(self):
ctx = ContextAPI.get_current_context()
assert ctx is not None

def test_context_inspect_without_params(self):
ctx = ContextAPI.inspect_context()
assert ctx["Name"] == "default"
Expand Down

0 comments on commit 8f99eb5

Please sign in to comment.