Skip to content

Fixed YAML warning & Modified the highlighting color #85

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 36 additions & 38 deletions kubeshell/kubeshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import subprocess
import yaml
import logging

logger = logging.getLogger(__name__)

inline_help = True
Expand All @@ -27,7 +28,6 @@


class KubeConfig(object):

clustername = user = ""
namespace = "default"
current_context_index = 0
Expand All @@ -39,22 +39,21 @@ def parse_kubeconfig():
return ("", "", "")

with open(os.path.expanduser(kubeconfig_filepath), "r") as fd:
docs = yaml.load_all(fd)
for doc in docs:
current_context = doc.get("current-context", "")
contexts = doc.get("contexts")
if contexts:
for index, context in enumerate(contexts):
if context['name'] == current_context:
KubeConfig.current_context_index = index
KubeConfig.current_context_name = context['name']
if 'cluster' in context['context']:
KubeConfig.clustername = context['context']['cluster']
if 'namespace' in context['context']:
KubeConfig.namespace = context['context']['namespace']
if 'user' in context['context']:
KubeConfig.user = context['context']['user']
return (KubeConfig.clustername, KubeConfig.user, KubeConfig.namespace)
docs = yaml.load(fd, Loader=yaml.FullLoader)
current_context = docs.get("current-context", "")
contexts = docs.get("contexts")
if contexts:
for index, context in enumerate(contexts):
if context['name'] == current_context:
KubeConfig.current_context_index = index
KubeConfig.current_context_name = context['name']
if 'cluster' in context['context']:
KubeConfig.clustername = context['context']['cluster']
if 'namespace' in context['context']:
KubeConfig.namespace = context['context']['namespace']
if 'user' in context['context']:
KubeConfig.user = context['context']['user']
return (KubeConfig.clustername, KubeConfig.user, KubeConfig.namespace)
return ("", "", "")

@staticmethod
Expand All @@ -63,15 +62,15 @@ def switch_to_next_cluster():
return

with open(os.path.expanduser(kubeconfig_filepath), "r") as fd:
docs = yaml.load_all(fd)
for doc in docs:
contexts = doc.get("contexts")
if contexts:
KubeConfig.current_context_index = (KubeConfig.current_context_index+1) % len(contexts)
cluster_name = contexts[KubeConfig.current_context_index]['name']
kubectl_config_use_context = "kubectl config use-context " + cluster_name
cmd_process = subprocess.Popen(kubectl_config_use_context, shell=True, stdout=subprocess.PIPE)
cmd_process.wait()
# docs = yaml.load_all(fd) # 已经弃用
docs = yaml.load(fd, Loader=yaml.FullLoader)
contexts = docs.get("contexts")
if contexts:
KubeConfig.current_context_index = (KubeConfig.current_context_index + 1) % len(contexts)
cluster_name = contexts[KubeConfig.current_context_index]['name']
kubectl_config_use_context = "kubectl config use-context " + cluster_name
cmd_process = subprocess.Popen(kubectl_config_use_context, shell=True, stdout=subprocess.PIPE)
cmd_process.wait()
return

@staticmethod
Expand All @@ -87,7 +86,6 @@ def switch_to_next_namespace(current_namespace):


class Kubeshell(object):

clustername = user = ""
namespace = "default"

Expand Down Expand Up @@ -144,7 +142,7 @@ def get_title():
logger.info("running kube-shell event loop")
if not os.path.exists(os.path.expanduser(kubeconfig_filepath)):
click.secho('Kube-shell uses {0} for server side completion. Could not find {0}. '
'Server side completion functionality may not work.'.format(kubeconfig_filepath),
'Server side completion functionality may not work.'.format(kubeconfig_filepath),
fg='red', blink=True, bold=True)
while True:
global inline_help
Expand All @@ -156,16 +154,16 @@ def get_title():

try:
user_input = prompt('kube-shell> ',
history=self.history,
auto_suggest=AutoSuggestFromHistory(),
style=StyleFactory("vim").style,
lexer=KubectlLexer,
get_title=get_title,
enable_history_search=False,
get_bottom_toolbar_tokens=self.toolbar.handler,
vi_mode=True,
key_bindings_registry=registry,
completer=completer)
history=self.history,
auto_suggest=AutoSuggestFromHistory(),
style=StyleFactory("vim").style,
lexer=KubectlLexer,
get_title=get_title,
enable_history_search=False,
get_bottom_toolbar_tokens=self.toolbar.handler,
vi_mode=True,
key_bindings_registry=registry,
completer=completer)
except (EOFError, KeyboardInterrupt):
sys.exit()

Expand Down
4 changes: 2 additions & 2 deletions kubeshell/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def style_factory(self, style_name):
t = Token
styles.update({
t.Menu.Completions.Completion.Current: 'bg:#00aaaa #000000',
t.Menu.Completions.Completion: 'bg:#008888 #ffffff',
t.Menu.Completions.Completion: 'bg:#008888 #D3D3D3',
t.Menu.Completions.Meta.Current: 'bg:#00aaaa #000000',
t.Menu.Completions.Meta: 'bg:#00aaaa #ffffff',
t.Menu.Completions.Meta: 'bg:#00aaaa #D3D3D3',
t.Scrollbar.Button: 'bg:#003333',
t.Scrollbar: 'bg:#00aaaa',
t.Toolbar: 'bg:#222222 #cccccc',
Expand Down