File: anise.go

package info (click to toggle)
distrobuilder 3.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,436 kB
  • sloc: sh: 214; makefile: 76
file content (90 lines) | stat: -rw-r--r-- 1,931 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package managers

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	incus "github.com/lxc/incus/v6/shared/util"

	"github.com/lxc/distrobuilder/shared"
)

type anise struct {
	common
}

func (m *anise) load() error {
	m.commands = managerCommands{
		clean:   "anise",
		install: "anise",
		refresh: "anise",
		remove:  "anise",
		update:  "anise",
	}

	m.flags = managerFlags{
		global: []string{},
		clean: []string{
			"cleanup", "--purge-repos",
		},
		install: []string{
			// Forcing always override of the protected
			// files. Not needed on image creation.
			"install", "--skip-config-protect",
		},
		refresh: []string{
			"repo", "update", "--force",
		},
		remove: []string{
			"uninstall", "--skip-config-protect",
		},
		update: []string{
			"upgrade", "--sync-repos", "--skip-config-protect",
		},
	}

	return nil
}

func (m *anise) manageRepository(repoAction shared.DefinitionPackagesRepository) error {
	var targetFile string

	if repoAction.Name == "" {
		return errors.New("Invalid repository name")
	}

	if repoAction.URL == "" {
		return errors.New("Invalid repository url")
	}

	if strings.HasSuffix(repoAction.Name, ".yml") {
		targetFile = filepath.Join("/etc/anise/repos.conf.d", repoAction.Name)
	} else {
		targetFile = filepath.Join("/etc/anise/repos.conf.d", repoAction.Name+".yml")
	}

	if !incus.PathExists(filepath.Dir(targetFile)) {
		err := os.MkdirAll(filepath.Dir(targetFile), 0o755)
		if err != nil {
			return fmt.Errorf("Failed to create directory %q: %w", filepath.Dir(targetFile), err)
		}
	}

	f, err := os.OpenFile(targetFile, os.O_CREATE|os.O_WRONLY, 0o644)
	if err != nil {
		return fmt.Errorf("Failed to open file %q: %w", targetFile, err)
	}

	defer f.Close()

	// NOTE: repo.URL is not an URL but the content of the file.
	_, err = f.WriteString(repoAction.URL)
	if err != nil {
		return fmt.Errorf("Failed to write string to %q: %w", targetFile, err)
	}

	return nil
}