talos

Форк
0
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

5
package extensions
6

7
import (
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.
19
func Load(path string) (*Extension, error) {
20
	extension := &Extension{
21
		directory: filepath.Base(path),
22
	}
23

24
	items, err := os.ReadDir(path)
25
	if err != nil {
26
		return nil, err
27
	}
28

29
	for _, item := range items {
30
		switch item.Name() {
31
		case "manifest.yaml":
32
			if err = extension.loadManifest(filepath.Join(path, item.Name())); err != nil {
33
				return nil, err
34
			}
35
		case "rootfs":
36
			extension.rootfsPath = filepath.Join(path, item.Name())
37
		default:
38
			return nil, fmt.Errorf("unexpected file %q", item.Name())
39
		}
40
	}
41

42
	var zeroManifest Manifest
43

44
	if extension.Manifest == zeroManifest {
45
		return nil, errors.New("extension manifest is missing")
46
	}
47

48
	if extension.rootfsPath == "" {
49
		return nil, errors.New("extension rootfs is missing")
50
	}
51

52
	return extension, nil
53
}
54

55
func (ext *Extension) loadManifest(path string) error {
56
	f, err := os.Open(path)
57
	if err != nil {
58
		return err
59
	}
60

61
	defer f.Close() //nolint:errcheck
62

63
	if err = yaml.NewDecoder(f).Decode(&ext.Manifest); err != nil {
64
		return err
65
	}
66

67
	if ext.Manifest.Version != "v1alpha1" {
68
		return fmt.Errorf("unsupported manifest version: %q", ext.Manifest.Version)
69
	}
70

71
	return nil
72
}
73

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.