I am trying to build a GNOME extension for my personal use on my Ubuntu system

38 Views Asked by At

I am trying to build a GNOME extension for my personal use on my Ubuntu system. The purpose of this extension is to switch between LAMP versions.

enter image description here

enter image description here

const Indicator = GObject.registerClass(
class Indicator extends PanelMenu.Button {
    _init() {
        super._init(0.0, _('PHP Switcher'));

        this.add_child(new St.Icon({
            icon_name: 'face-smile-symbolic',
            style_class: 'system-status-icon',
        }));

    
        let item = new PopupMenu.PopupMenuItem(_('Switch to PHP 7.4'));
        item.connect('activate', () => {
            GLib.spawn_command_line_sync('sudo /opt/lampp/lampp stop && sudo rm /opt/lampp && sudo ln -s 7.4 /opt/lampp && sudo /opt/lampp/lampp start');
        });

        let item2 = new PopupMenu.PopupMenuItem(_('Switch to PHP 8.1'));
        item2.connect('activate', () => {
            GLib.spawn_command_line_sync('sudo /opt/lampp/lampp stop && sudo rm /opt/lampp && sudo ln -s 8.1 /opt/lampp && sudo /opt/lampp/lampp start');
        });

        this.menu.addMenuItem(item);
        this.menu.addMenuItem(item2);
    }
});

when i try click switch to php 7.4 and 8.1 its not work

Does anyone know why this isn't working?

1

There are 1 best solutions below

3
Anya Shenanigans On

Based on an image to text analysis of the input, we see the following commands being issued:

sudo /opt/lampp/lampp stop && sudo rm /opt/lampp && sudo ln -s 7.4 /opt/lampp && sudo /opt/lampp/lampp start

Now this is being run in a shell extension. These don't have a terminal associated with them, and thus won't have a place to type in a password.

In general for things that are in this situation, you should use something other than sudo, such as pkexec, and make the entire set of commands into one pkexec command line like:

pkexec bash -c '/opt/lampp/lampp stop && rm /opt/lampp && ln -s 7.4 /opt/lampp && /opt/lampp/lampp start'

This will cause the prompt for the sudo password, and then run the set of commands.

So, for the full command line, as asked about in the subsequent comment:

GLib.spawn_command_line_sync("pkexec bash -c '/opt/lampp/lampp stop && rm /opt/lampp && ln -s 8.1 /opt/lampp && /opt/lampp/lampp start'");

Please note: testing this was really tedious as I could not get gnome to reload my extension without restarting my entire session.