forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifest.go
112 lines (98 loc) · 3.16 KB
/
manifest.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2022 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package image
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"github.com/snapcore/snapd/snap"
)
// The seed.manifest generated by ubuntu-image contains entries in the following
// format:
// <snap-name> <snap-revision>
// The goal in a future iteration of this will be to move the generation of the
// seed.manifest to this package, out of ubuntu-image.
// TODO: Move generation of seed.manifest from ubuntu-image to here
// ReadSeedManifest reads a seed.manifest generated by ubuntu-image, and returns
// a map containing the snap names and their revisions.
func ReadSeedManifest(manifestFile string) (map[string]snap.Revision, error) {
f, err := os.Open(manifestFile)
if err != nil {
return nil, err
}
defer f.Close()
revisions := make(map[string]snap.Revision)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
if strings.HasPrefix(line, " ") {
return nil, fmt.Errorf("line cannot start with any spaces: %q", line)
}
tokens := strings.Fields(line)
// Expect exactly two tokens
if len(tokens) != 2 {
return nil, fmt.Errorf("line is illegally formatted: %q", line)
}
snapName := tokens[0]
revString := tokens[1]
if err := snap.ValidateName(snapName); err != nil {
return nil, err
}
rev, err := snap.ParseRevision(revString)
if err != nil {
return nil, err
}
// Values that are higher than 0 indicate the revision comes from the store, and values
// lower than 0 indicate the snap was sourced locally. We allow both in the seed.manifest as
// long as the user can provide us with the correct snaps. The only number we won't accept is
// 0.
if rev.Unset() {
return nil, fmt.Errorf("cannot use revision %d for snap %q: revision must not be 0", rev, snapName)
}
revisions[snapName] = rev
}
return revisions, nil
}
// WriteSeedManifest generates the seed.manifest contents from the provided map of
// snaps and their revisions, and stores them in the given file path
func WriteSeedManifest(filePath string, revisions map[string]snap.Revision) error {
if len(revisions) == 0 {
return nil
}
keys := make([]string, 0, len(revisions))
for k := range revisions {
keys = append(keys, k)
}
sort.Strings(keys)
buf := bytes.NewBuffer(nil)
for _, key := range keys {
rev := revisions[key]
if rev.Unset() {
return fmt.Errorf("revision must not be 0 for snap %q", key)
}
fmt.Fprintf(buf, "%s %s\n", key, rev)
}
return ioutil.WriteFile(filePath, buf.Bytes(), 0755)
}