-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.go
95 lines (78 loc) · 2.94 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
// Copyright (c) 2022 Exograd SAS.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
)
// FreeBSD pkg manifests use the UCL format
// (https://github.com/vstakhov/libucl). Fortunately, UCL is officially fully
// compatible with JSON so we can just generate JSON data and be done with it.
// See https://github.com/freebsd/pkg/blob/master/libpkg/pkg_manifest.c
type Manifest struct {
Name string `json:"name"`
Version string `json:"version"`
Comment string `json:"comment"`
Desc string `json:"desc"`
Origin string `json:"origin"`
WWW string `json:"www,omitempty"`
Maintainer string `json:"maintainer,omitempty"`
Arch string `json:"arch"`
Deps ManifestDeps `json:"deps,omitempty"`
Users []string `json:"users,omitempty"`
Groups []string `json:"groups,omitempty"`
Prefix string `json:"prefix,omitempty"`
Files ManifestFiles `json:"files,omitempty"`
Directories ManifestDirectories `json:"directories,omitempty"`
Scripts map[string]string `json:"scripts"`
}
type ManifestDep struct {
Origin string `json:"origin"`
Version string `json:"version"`
}
type ManifestDeps map[string]ManifestDep
type ManifestFile struct {
Uname string `json:"uname"`
Gname string `json:"gname"`
Perm string `json:"perm"`
Sum string `json:"sum"`
}
type ManifestFiles map[string]ManifestFile
type ManifestDirectory struct {
Uname string `json:"uname"`
Gname string `json:"gname"`
Perm string `json:"perm"`
}
type ManifestDirectories map[string]ManifestDirectory
func NewManifest() *Manifest {
return &Manifest{
Files: make(ManifestFiles),
Directories: make(ManifestDirectories),
Scripts: make(map[string]string),
}
}
func (m *Manifest) PackageFilename() string {
return m.Name + "-" + m.Version + ".pkg"
}
func (m *Manifest) WriteFile(filePath string) error {
var buf bytes.Buffer
encoder := json.NewEncoder(&buf)
encoder.SetIndent("", " ")
if err := encoder.Encode(m); err != nil {
return fmt.Errorf("cannot encode manifest: %w", err)
}
return os.WriteFile(filePath, buf.Bytes(), 0644)
}