Implementation
final Map<String, FigArg> sharedArgs = {
'resourcesArg': FigArg(
name: 'Resource Type',
generators: [
FigGenerator(
script: scriptsTypes,
cache: const FigCache(
strategy: 'stale-while-revalidate',
ttl: 1000 * 60 * 60,
),
postProcess: sharedPostProcess,
),
],
),
'runningPodsArg': FigArg(
name: 'Running Pods',
generators: [
FigGenerator(
script: const [
'kubectl',
'get',
'pods',
'--field-selector=status.phase=Running',
'-o',
'name',
],
postProcess: sharedPostProcess,
),
],
),
'resourceSuggestionsFromResourceType': FigArg(
name: 'Resource',
generators: [
FigGenerator(
script: (List<String> context) {
if (context.length < 2) return scriptsTypes;
final resourceType = context[context.length - 2];
return typeWithoutName(resourceType);
},
postProcess: sharedPostProcess,
cache: const FigCache(
strategy: 'stale-while-revalidate',
ttl: 1000 * 60 * 60,
),
),
],
isOptional: true,
),
'listKubeConfContexts': FigArg(
name: 'Context',
generators: [
FigGenerator(
script: (List<String> context) {
final index = context.indexOf('--kubeconfig');
if (index != -1 && index + 1 < context.length) {
return [
'kubectl',
'config',
'--kubeconfig=${context[index + 1]}',
'get-contexts',
'-o',
'name',
];
}
return ['kubectl', 'config', 'get-contexts', '-o', 'name'];
},
postProcess: sharedPostProcess,
),
],
),
'listDeployments': FigArg(
name: 'Deployments',
generators: [
FigGenerator(
script: typeWithoutName('deployments'),
postProcess: sharedPostProcess,
cache: const FigCache(
strategy: 'stale-while-revalidate',
ttl: 1000 * 60 * 60,
),
),
],
),
'listClusters': FigArg(
name: 'Cluster',
generators: [
FigGenerator(
script: (List<String> context) {
final index = context.indexOf('--kubeconfig');
if (index != -1 && index + 1 < context.length) {
return [
'kubectl',
'config',
'--kubeconfig=${context[index + 1]}',
'get-clusters',
];
}
return ['kubectl', 'config', 'get-clusters'];
},
postProcess: (String out, [List<String>? tokens]) {
if (sharedPostProcessCheckConnectedToCluster(out) ||
sharedPostProcessCheckGeneralError(out)) {
return [];
}
return out
.split('\n')
.where((line) => line != 'NAME')
.map((line) => FigSuggestion(
name: line,
icon: 'fig://icon?type=kubernetes',
))
.toList();
},
),
],
),
'typeOrTypeSlashName': FigArg(
name: 'TYPE | TYPE/NAME',
generators: [
FigGenerator(
script: (List<String> context) {
if (context.isEmpty) return scriptsTypes;
final lastInput = context[context.length - 1];
if (lastInput.contains('/')) {
return typeWithoutName(
lastInput.substring(0, lastInput.indexOf('/')),
);
}
return scriptsTypes;
},
postProcess: sharedPostProcess,
trigger: '/',
getQueryTerm: '/',
cache: const FigCache(
strategy: 'stale-while-revalidate',
ttl: 1000 * 60 * 60,
),
),
],
),
'listNodes': FigArg(
name: 'Node',
generators: [
FigGenerator(
script: typeWithoutName('nodes'),
postProcess: sharedPostProcess,
cache: const FigCache(
strategy: 'stale-while-revalidate',
ttl: 1000 * 60 * 60,
),
),
],
),
'listClusterRoles': FigArg(
name: 'Cluster Role',
generators: [
FigGenerator(
script: typeWithoutName('clusterroles'),
postProcess: sharedPostProcess,
cache: const FigCache(
strategy: 'stale-while-revalidate',
ttl: 1000 * 60 * 60,
),
),
],
),
'listContainersFromPod': FigArg(
name: 'Container',
generators: [
FigGenerator(
script: (List<String> context) {
final podIndex = context.indexWhere((i) =>
i == 'pods' ||
i.contains('pods/') ||
i == 'pod' ||
i.contains('pod/'));
if (podIndex < 0 || podIndex >= context.length) {
return ['kubectl', 'get', 'pods', '-o', 'json'];
}
final podSegment = context[podIndex];
final podName = podSegment.contains('/')
? podSegment
: (podIndex + 1 < context.length
? '${context[podIndex]}/${context[podIndex + 1]}'
: podSegment);
return ['kubectl', 'get', podName, '-o', 'json'];
},
postProcess: (String out, [List<String>? tokens]) {
if (sharedPostProcessCheckConnectedToCluster(out) ||
sharedPostProcessCheckGeneralError(out)) {
return [];
}
try {
final data = jsonDecode(out) as Map<String, dynamic>;
final spec = data['spec'] as Map<String, dynamic>?;
final containers = spec?['containers'] as List<dynamic>?;
if (containers == null) return [];
return containers
.map((item) {
final m = item as Map<String, dynamic>;
return FigSuggestion(
name: m['name']?.toString(),
description: m['image']?.toString(),
icon: 'fig://icon?type=kubernetes',
);
})
.where((s) => s.name != null)
.toList();
} catch (_) {
return [];
}
},
),
],
),
};