Skip to content

Commit

Permalink
Add option to list plugins
Browse files Browse the repository at this point in the history
This change creates a CLI option in the backend + desktop to list all
plugins, with a distinction between static/shipped plugins and
user-added ones.

Fixes: #2161

Signed-off-by: Evangelos Skopelitis <eskopelitis@microsoft.com>
  • Loading branch information
skoeva committed Sep 10, 2024
1 parent 2266251 commit dff3d5c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
26 changes: 26 additions & 0 deletions app/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ const args = yargs(hideBin(process.argv))
describe: 'Disable use of GPU. For people who may have buggy graphics drivers',
type: 'boolean',
},
'list-plugins': {
describe: 'List all static and user-added plugins.',
type: 'boolean',
},
})
.positional('kubeconfig', {
describe:
Expand All @@ -78,6 +82,28 @@ const args = yargs(hideBin(process.argv))
.help()
.parseSync();

const listPlugins = args['list-plugins'] === true;

// If the user wants to list plugins, we don't need to start the app.
if (listPlugins) {
const staticDir = path.join(process.resourcesPath, '.plugins');
const pluginsDir = path.join(app.getPath('userData'), 'plugins');

const staticPlugins = fs.existsSync(staticDir) ? fs.readdirSync(staticDir) : [];
const userPlugins = fs.existsSync(pluginsDir) ? fs.readdirSync(pluginsDir) : [];

console.log(' Static Plugins:');
staticPlugins.forEach(plugin => {
console.log(` - ${plugin}`);
});
console.log(' User Plugins:');
userPlugins.forEach(plugin => {
console.log(` - ${plugin}`);
});

process.exit(0);
}

const isHeadlessMode = args.headless === true;
let disableGPU = args['disable-gpu'] === true;
const defaultPort = 4466;
Expand Down
26 changes: 26 additions & 0 deletions backend/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/headlamp-k8s/headlamp/backend/pkg/logger"
"github.com/headlamp-k8s/headlamp/backend/pkg/plugins"
"github.com/knadh/koanf"
"github.com/knadh/koanf/providers/basicflag"
"github.com/knadh/koanf/providers/env"
Expand Down Expand Up @@ -86,6 +87,30 @@ func Parse(args []string) (*Config, error) {
return nil, fmt.Errorf("error parsing flags: %w", err)
}

// Handle list-plugins arg
if listPlugins := f.Lookup("list-plugins"); listPlugins != nil && listPlugins.Value.String() == "true" {
staticDir := f.Lookup("html-static-dir").Value.String()
pluginsDir := f.Lookup("plugins-dir").Value.String()

staticPlugins, userPlugins, err := plugins.GenerateSeparatePluginPaths(staticDir, pluginsDir)
if err != nil {
logger.Log(logger.LevelError, nil, err, "listing plugins")
return nil, fmt.Errorf("error listing plugins: %w", err)
}

fmt.Println(" Static Plugins:")
for _, plugin := range staticPlugins {

Check failure on line 102 in backend/pkg/config/config.go

View workflow job for this annotation

GitHub Actions / build

ranges should only be cuddled with assignments used in the iteration (wsl)
fmt.Println(" -", plugin)
}

fmt.Println(" User-added Plugins:")
for _, plugin := range userPlugins {

Check failure on line 107 in backend/pkg/config/config.go

View workflow job for this annotation

GitHub Actions / build

ranges should only be cuddled with assignments used in the iteration (wsl)
fmt.Println(" -", plugin)
}

os.Exit(0)
}

// Load config from env
if err := k.Load(env.Provider("HEADLAMP_CONFIG_", ".", func(s string) string {
return strings.ReplaceAll(strings.ToLower(strings.TrimPrefix(s, "HEADLAMP_CONFIG_")), "_", "-")
Expand Down Expand Up @@ -153,6 +178,7 @@ func flagset() *flag.FlagSet {
f.Bool("dev", false, "Allow connections from other origins")
f.Bool("insecure-ssl", false, "Accept/Ignore all server SSL certificates")
f.Bool("enable-dynamic-clusters", false, "Enable dynamic clusters, which stores stateless clusters in the frontend.")
f.Bool("list-plugins", false, "List all static and user-added plugins")

f.String("kubeconfig", "", "Absolute path to the kubeconfig file")
f.String("html-static-dir", "", "Static HTML directory to serve")
Expand Down
16 changes: 13 additions & 3 deletions backend/pkg/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,30 @@ func periodicallyWatchSubfolders(watcher *fsnotify.Watcher, path string, interva
}
}

// GeneratePluginPaths takes the staticPluginDir and pluginDir and returns a list of plugin paths.
func GeneratePluginPaths(staticPluginDir string, pluginDir string) ([]string, error) {
// GenerateSeparatePluginPaths takes the staticPluginDir and pluginDir and returns a list of plugin paths.
func GenerateSeparatePluginPaths(staticPluginDir string, pluginDir string) ([]string, []string, error) {
var pluginListURLStatic []string

if staticPluginDir != "" {
var err error

pluginListURLStatic, err = pluginBasePathListForDir(staticPluginDir, "static-plugins")
if err != nil {
return nil, err
return nil, nil, err
}
}

pluginListURL, err := pluginBasePathListForDir(pluginDir, "plugins")
if err != nil {
return nil, nil, err
}

return pluginListURLStatic, pluginListURL, nil
}

// GeneratePluginPaths generates a concatenated list of plugin paths from the staticPluginDir and pluginDir.
func GeneratePluginPaths(staticPluginDir string, pluginDir string) ([]string, error) {
pluginListURLStatic, pluginListURL, err := GenerateSeparatePluginPaths(staticPluginDir, pluginDir)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit dff3d5c

Please sign in to comment.