2022-07-20 14:08:56 +02:00
|
|
|
import { showNotification } from '@mantine/notifications';
|
2022-07-06 18:08:39 +02:00
|
|
|
import Dockerode from 'dockerode';
|
2022-07-20 14:08:56 +02:00
|
|
|
import { Config, MatchingImages, ServiceType, ServiceTypeList } from './types';
|
2022-07-06 18:08:39 +02:00
|
|
|
|
|
|
|
|
async function MatchIcon(name: string) {
|
|
|
|
|
const res = await fetch(
|
|
|
|
|
`https://cdn.jsdelivr.net/gh/walkxhub/dashboard-icons/png/${name
|
|
|
|
|
.replace(/\s+/g, '-')
|
|
|
|
|
.toLowerCase()}.png`
|
|
|
|
|
);
|
|
|
|
|
return res.ok ? res.url : '/favicon.svg';
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-20 14:08:56 +02:00
|
|
|
function tryMatchType(imageName: string): ServiceType {
|
|
|
|
|
// Try to find imageName inside MatchingImages
|
|
|
|
|
|
|
|
|
|
const match = MatchingImages.find(({ image }) => imageName.includes(image));
|
|
|
|
|
if (match) {
|
|
|
|
|
return match.type;
|
|
|
|
|
}
|
2022-07-06 18:08:39 +02:00
|
|
|
return 'Other';
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-20 14:08:56 +02:00
|
|
|
export function tryMatchService(container: Dockerode.ContainerInfo | undefined) {
|
|
|
|
|
if (container === undefined) return {};
|
|
|
|
|
const name = container.Names[0].substring(1);
|
|
|
|
|
return {
|
|
|
|
|
name,
|
|
|
|
|
id: container.Id,
|
|
|
|
|
type: tryMatchType(container.Image),
|
|
|
|
|
url: `${container.Ports.at(0)?.IP}:${container.Ports.at(0)?.PublicPort}`,
|
|
|
|
|
icon: `https://cdn.jsdelivr.net/gh/walkxhub/dashboard-icons/png/${name
|
|
|
|
|
.replace(/\s+/g, '-')
|
|
|
|
|
.toLowerCase()}.png`,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-06 18:08:39 +02:00
|
|
|
export default async function addToHomarr(
|
|
|
|
|
container: Dockerode.ContainerInfo,
|
|
|
|
|
config: Config,
|
|
|
|
|
setConfig: (newconfig: Config) => void
|
|
|
|
|
) {
|
|
|
|
|
setConfig({
|
|
|
|
|
...config,
|
|
|
|
|
services: [
|
|
|
|
|
...config.services,
|
|
|
|
|
{
|
|
|
|
|
name: container.Names[0].substring(1),
|
|
|
|
|
id: container.Id,
|
|
|
|
|
type: tryMatchType(container.Image),
|
2022-07-20 14:08:56 +02:00
|
|
|
url: `localhost:${container.Ports.at(0)?.PublicPort}`,
|
2022-07-06 18:08:39 +02:00
|
|
|
icon: await MatchIcon(container.Names[0].substring(1)),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
}
|