Skip to content

Commit

Permalink
Show current context instead of icon
Browse files Browse the repository at this point in the history
Add option to switch between icon and label
Add convinience lib
Add preferencies
  • Loading branch information
vvbogdanov87 committed Jun 12, 2018
1 parent 85aef63 commit 32b8b1a
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 7 deletions.
58 changes: 51 additions & 7 deletions kubeIndicator.js
Expand Up @@ -5,36 +5,38 @@ const PopupMenu = imports.ui.popupMenu;
const Clutter = imports.gi.Clutter;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Shell = imports.gi.Shell;

const Me = imports.misc.extensionUtils.getCurrentExtension();
const KubePopupMenuItem = Me.imports.kubePopupMenuItem;
const Convenience = Me.imports.lib.convenience;

const KubeIndicator = new Lang.Class({
Name: "Kube",
Extends: PanelMenu.Button,

_init: function(metadata, params) {
this.parent(null, "Kube");
this._settings = Convenience.getSettings();
this.kcPath = GLib.get_home_dir() + "/.kube/config";

this.icon = new St.Icon({
icon_name: 'logo',
style_class: 'system-status-icon'
});
this.actor.add_actor(this.icon);

this.setMenu(new PopupMenu.PopupMenu(this.actor, 0.25, St.Side.TOP));
this._update();

this._setView()

let kcFile = Gio.File.new_for_path(this.kcPath);
this._monitor = kcFile.monitor(Gio.FileMonitorFlags.NONE, null);
this._monitor.connect('changed', Lang.bind(this, this._update));

this._bindSettingsChanges();
},

_onChange: function(m, f, of, eventType) {
if (eventType == Gio.FileMonitorEvent.CHANGED){
this._update()
}
},

_update: function() {
this.menu.removeAll()
try {
Expand All @@ -44,6 +46,9 @@ const KubeIndicator = new Lang.Class({
let currentContext = '';
if (match != null){
currentContext = match[1];
if ( this._settings.get_boolean('show-current-context') == true ){
this.label.text = currentContext;
}
}

re = new RegExp('-\\scontext:\\n.*\\n.*\\n.*name:\\s(.*)','gm');
Expand All @@ -56,8 +61,47 @@ const KubeIndicator = new Lang.Class({
this.menu.addMenuItem(new KubePopupMenuItem.KubePopupMenuItem(match[1],curr));
match = re.exec(contents);
}

// add seperator to popup menu
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());

// add link to settings dialog
this._menu_settings = new PopupMenu.PopupMenuItem(_("Settings"));
this._menu_settings.connect("activate", function(){
// call gnome settings tool for this extension
let app = Shell.AppSystem.get_default().lookup_app("gnome-shell-extension-prefs.desktop");
if( app!=null ) {
let info = app.get_app_info();
let timestamp = global.display.get_current_time_roundtrip();
info.launch_uris([Me.uuid], global.create_app_launch_context(timestamp, -1));
}
});
this.menu.addMenuItem(this._menu_settings);
} catch (e) {
log('gnome-shell-extension-kubeconfig',e);
}
},

_setView: function() {
this.actor.remove_all_children();
log('gnome-shell-extension-kubeconfig',"_setView");
if ( this._settings.get_boolean('show-current-context') == false ){
this.icon = new St.Icon({
icon_name: 'logo',
style_class: 'system-status-icon'
});
this.actor.add_actor(this.icon);
} else {
this.label = new St.Label({ text: _("kubectl"),
y_align: Clutter.ActorAlign.CENTER });
this.actor.add_actor(this.label);
}
this._update();
},

_bindSettingsChanges: function() {
this._settings.connect('changed::show-current-context',Lang.bind(this, function() {
this._setView();
}));
}
});
92 changes: 92 additions & 0 deletions lib/convenience.js
@@ -0,0 +1,92 @@
/* -*- mode: js; js-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
Copyright (c) 2011-2012, Giovanni Campagna <scampa.giovanni@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the GNOME nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

const Gettext = imports.gettext;
const Gio = imports.gi.Gio;

const Config = imports.misc.config;
const ExtensionUtils = imports.misc.extensionUtils;

/**
* initTranslations:
* @domain: (optional): the gettext domain to use
*
* Initialize Gettext to load translations from extensionsdir/locale.
* If @domain is not provided, it will be taken from metadata['gettext-domain']
*/
function initTranslations(domain) {
let extension = ExtensionUtils.getCurrentExtension();

domain = domain || extension.metadata['gettext-domain'];

// check if this extension was built with "make zip-file", and thus
// has the locale files in a subfolder
// otherwise assume that extension has been installed in the
// same prefix as gnome-shell
let localeDir = extension.dir.get_child('locale');
if (localeDir.query_exists(null))
Gettext.bindtextdomain(domain, localeDir.get_path());
else
Gettext.bindtextdomain(domain, Config.LOCALEDIR);
}

/**
* getSettings:
* @schema: (optional): the GSettings schema id
*
* Builds and return a GSettings schema for @schema, using schema files
* in extensionsdir/schemas. If @schema is not provided, it is taken from
* metadata['settings-schema'].
*/
function getSettings(schema) {
let extension = ExtensionUtils.getCurrentExtension();

schema = schema || extension.metadata['settings-schema'];

const GioSSS = Gio.SettingsSchemaSource;

// check if this extension was built with "make zip-file", and thus
// has the schema files in a subfolder
// otherwise assume that extension has been installed in the
// same prefix as gnome-shell (and therefore schemas are available
// in the standard folders)
let schemaDir = extension.dir.get_child('schemas');
let schemaSource;
if (schemaDir.query_exists(null))
schemaSource = GioSSS.new_from_directory(schemaDir.get_path(),
GioSSS.get_default(),
false);
else
schemaSource = GioSSS.get_default();

let schemaObj = schemaSource.lookup(schema, true);
if (!schemaObj)
throw new Error('Schema ' + schema + ' could not be found for extension '
+ extension.metadata.uuid + '. Please check your installation.');

return new Gio.Settings({ settings_schema: schemaObj });
}
1 change: 1 addition & 0 deletions metadata.json
Expand Up @@ -2,6 +2,7 @@
"name": "Kube Config",
"description": "Switches kube config context",
"uuid": "kube_config@vvbogdanov87.gmail.com",
"settings-schema": "org.gnome.shell.extensions.kube-config",
"shell-version": [
"3.28.2"
]
Expand Down
36 changes: 36 additions & 0 deletions prefs.js
@@ -0,0 +1,36 @@
const Gtk = imports.gi.Gtk;
const Gio = imports.gi.Gio;

const Me = imports.misc.extensionUtils.getCurrentExtension();
const Convenience = Me.imports.lib.convenience;

let settings;

function init() {
settings = Convenience.getSettings();
}

function option() {
let hBox = new Gtk.Box({orientation: Gtk.Orientation.HORIZONTAL});
let hbLabel = new Gtk.Label({label: "show current context", xalign: 0});
let hbSwitch = new Gtk.Switch();

settings.bind('show-current-context',
hbSwitch,
'active',
Gio.SettingsBindFlags.DEFAULT);

hBox.pack_start(hbLabel,false, false, 0);
hBox.pack_end(hbSwitch,false, false, 0);

return hBox;
}

function buildPrefsWidget() {
let window = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL, border_width: 10 });
window.add(option())

window.show_all();

return window;
}
Binary file added schemas/gschemas.compiled
Binary file not shown.
10 changes: 10 additions & 0 deletions schemas/org.gnome.shell.extensions.kube_config.gschema.xml
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<schemalist gettext-domain="gnome-shell-extensions">
<schema path="/org/gnome/shell/extensions/kube-config/" id="org.gnome.shell.extensions.kube-config">
<key type="b" name="show-current-context">
<default>false</default>
<summary>Show current contest</summary>
<description></description>
</key>
</schema>
</schemalist>

0 comments on commit 32b8b1a

Please sign in to comment.