talos
72 строки · 1.5 Кб
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5package extensions
6
7import (
8"errors"
9"fmt"
10"os"
11"path/filepath"
12
13"gopkg.in/yaml.v3"
14)
15
16// Load extension from the filesystem.
17//
18// This performs initial validation of the extension file structure.
19func Load(path string) (*Extension, error) {
20extension := &Extension{
21directory: filepath.Base(path),
22}
23
24items, err := os.ReadDir(path)
25if err != nil {
26return nil, err
27}
28
29for _, item := range items {
30switch item.Name() {
31case "manifest.yaml":
32if err = extension.loadManifest(filepath.Join(path, item.Name())); err != nil {
33return nil, err
34}
35case "rootfs":
36extension.rootfsPath = filepath.Join(path, item.Name())
37default:
38return nil, fmt.Errorf("unexpected file %q", item.Name())
39}
40}
41
42var zeroManifest Manifest
43
44if extension.Manifest == zeroManifest {
45return nil, errors.New("extension manifest is missing")
46}
47
48if extension.rootfsPath == "" {
49return nil, errors.New("extension rootfs is missing")
50}
51
52return extension, nil
53}
54
55func (ext *Extension) loadManifest(path string) error {
56f, err := os.Open(path)
57if err != nil {
58return err
59}
60
61defer f.Close() //nolint:errcheck
62
63if err = yaml.NewDecoder(f).Decode(&ext.Manifest); err != nil {
64return err
65}
66
67if ext.Manifest.Version != "v1alpha1" {
68return fmt.Errorf("unsupported manifest version: %q", ext.Manifest.Version)
69}
70
71return nil
72}
73