Skip to content

Commit af07bb0

Browse files
authored
Merge pull request #5 from dockersamples/add-local-catalog-support
Add local catalog support
2 parents 162e608 + a1db96e commit af07bb0

File tree

4 files changed

+59
-23
lines changed

4 files changed

+59
-23
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ LABEL org.opencontainers.image.title="Labspaces" \
2323
com.docker.extension.publisher-url="https://github.com/dockersamples/labspace-extension" \
2424
com.docker.extension.additional-urls="[{\"title\":\"Create your own Labspace\",\"url\":\"https://github.com/dockersamples/labspace-starter\"}]" \
2525
com.docker.extension.categories="cloud-development" \
26-
com.docker.extension.changelog="Fix bug that was showing \"Starting...\" label for all Labspaces instead of only the one being started"
26+
com.docker.extension.changelog="- Adding experimentation for locally defined catalogs and Labspace content"
2727
COPY beaker.svg /
2828
COPY metadata.json /
2929
COPY --from=build /usr/local/app/dist /ui

src/CatalogContext.jsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export function CatalogContextProvider({ children }) {
4646
}, [customLabspaces]);
4747

4848
const addCatalog = useCallback(
49-
(name, url) => {
50-
setCatalogs((catalog) => [...catalog, { name, url }]);
49+
({ name, url, content }) => {
50+
setCatalogs((catalog) => [...catalog, { name, url, content }]);
5151
},
5252
[setCatalogs],
5353
);
@@ -89,16 +89,22 @@ export function CatalogContextProvider({ children }) {
8989

9090
useEffect(() => {
9191
Promise.all(
92-
catalogs.map((catalog) =>
93-
fetch(catalog.url)
94-
.then((res) => res.text())
95-
.then((text) => parse(text))
96-
.then((data) => ({
97-
...catalog,
98-
tags: data.tags || [],
99-
labspaces: data.labspaces || [],
100-
})),
101-
),
92+
catalogs.map(async (catalog) => {
93+
let catalogContent;
94+
if (catalog.content) {
95+
console.log("Got some content", catalog.content);
96+
catalogContent = catalog.content;
97+
} else {
98+
catalogContent = await fetch(catalog.url).then((res) => res.text());
99+
}
100+
101+
const data = parse(catalogContent);
102+
return {
103+
...catalog,
104+
tags: data.tags || [],
105+
labspaces: data.labspaces || [],
106+
};
107+
}),
102108
).then((results) => setCatalogDetails(results));
103109
}, [catalogs]);
104110

src/DockerContext.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,13 @@ export function DockerContextProvider({ children }) {
9090
setLaunchLog("");
9191
setStartingLabspace(location);
9292

93+
const projectFileLocation = location.startsWith("/")
94+
? location
95+
: `oci://${location}`;
96+
9397
ddClient.docker.cli.exec(
9498
"compose",
95-
["-f", `oci://${location}`, "-p", "labspace", "up", "-d", "-y"],
99+
["-f", projectFileLocation, "-p", "labspace", "up", "-d", "-y"],
96100
{
97101
stream: {
98102
onOutput(data) {

src/components/ManageCatalogsModal.jsx

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ async function validateUrlResponse(url) {
2323
return response;
2424
}
2525

26-
async function validateCatalogContent(response) {
27-
const data = await response.text();
26+
async function validateCatalogContent(data) {
2827
let catalog;
2928
try {
3029
catalog = parse(data);
@@ -44,6 +43,7 @@ export function ManageCatalogsModal({ show, onClose }) {
4443
const { catalogs, addCatalog, removeCatalog } = useCatalogs();
4544
const [title, setTitle] = useState("");
4645
const [url, setUrl] = useState("");
46+
const [catalogContent, setCatalogContent] = useState("");
4747
const [error, setError] = useState(null);
4848
const [showCatalogAdded, setShowCatalogAdded] = useState(false);
4949

@@ -57,7 +57,8 @@ export function ManageCatalogsModal({ show, onClose }) {
5757
try {
5858
validateUrl(url);
5959
const response = await validateUrlResponse(url);
60-
await validateCatalogContent(response);
60+
const data = await response.text();
61+
await validateCatalogContent(data);
6162

6263
return true;
6364
} catch (e) {
@@ -66,12 +67,21 @@ export function ManageCatalogsModal({ show, onClose }) {
6667
}
6768
}, []);
6869

69-
function handleSubmit(e) {
70+
async function handleSubmit(e) {
7071
e.preventDefault();
71-
validate(url)
72-
.then(() => addCatalog(title, url))
73-
.then(() => setShowCatalogAdded(true))
74-
.then(() => reset());
72+
setError(null);
73+
74+
if (!url && !catalogContent) {
75+
setError("Please provide either a URL or catalog content");
76+
return;
77+
}
78+
79+
if (url) await validate(url);
80+
else await validateCatalogContent(catalogContent);
81+
82+
addCatalog({ name: title, url, content: catalogContent });
83+
setShowCatalogAdded(true);
84+
reset();
7585
}
7686

7787
function handleCancel() {
@@ -137,17 +147,33 @@ export function ManageCatalogsModal({ show, onClose }) {
137147
/>
138148
</Form.Group>
139149

150+
<Alert variant="info">
151+
You can add a catalog by either providing the URL or the content of
152+
the catalog definition file. If both are provided, the URL will be
153+
used.
154+
</Alert>
155+
140156
<Form.Group className="mb-3" controlId="labspaceComposeFile">
141157
<Form.Label>Catalog URL</Form.Label>
142158
<Form.Control
143159
type="text"
144160
value={url}
145161
onChange={(e) => setUrl(e.target.value)}
146-
required
147162
/>
148163
<Form.Text muted>Location of the catalog definition file</Form.Text>
149164
</Form.Group>
150165

166+
<Form.Group className="mb-3" controlId="labspaceComposeFile">
167+
<Form.Label>Catalog Content</Form.Label>
168+
<Form.Control
169+
as="textarea"
170+
rows={7}
171+
value={catalogContent}
172+
onChange={(e) => setCatalogContent(e.target.value)}
173+
/>
174+
<Form.Text muted>Content of the catalog definition file</Form.Text>
175+
</Form.Group>
176+
151177
{showCatalogAdded && (
152178
<Alert variant="success" onClose={() => setShowCatalogAdded(false)}>
153179
Catalog added successfully!

0 commit comments

Comments
 (0)