generated from Vanilla-OS/vib-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
82 lines (69 loc) · 1.98 KB
/
plugin.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
// Copyright 2023 - 2023, axtlos <[email protected]>
// Copyright 2023 - present, K.B.Dharun Krishna <[email protected]>
// SPDX-License-Identifier: GPL-3.0-ONLY
package main
import (
"C"
"bufio"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/vanilla-os/vib/api"
)
type DnfModule struct {
Name string `json:"name"`
Type string `json:"type"`
Options DnfOptions `json:"options"`
Source api.Source `json:"source"`
}
type DnfOptions struct {
ExtraFlags []string `json:"extra_flags"`
}
//export BuildModule
func BuildModule(moduleInterface *C.char, recipeInterface *C.char) *C.char {
var module *DnfModule
var recipe *api.Recipe
err := json.Unmarshal([]byte(C.GoString(moduleInterface)), &module)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
err = json.Unmarshal([]byte(C.GoString(recipeInterface)), &recipe)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
extraFlags := strings.Join(module.Options.ExtraFlags, " ")
if len(module.Source.Packages) > 0 {
packages := strings.Join(module.Source.Packages, " ")
return C.CString(fmt.Sprintf("dnf install -y %s %s && dnf clean all", extraFlags, packages))
}
if len(module.Source.Paths) > 0 {
cmd := ""
for i, path := range module.Source.Paths {
instPath := filepath.Join(recipe.ParentPath, path+".inst")
packages := ""
file, err := os.Open(instPath)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
packages += scanner.Text() + " "
}
if err := scanner.Err(); err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
cmd += fmt.Sprintf("dnf install -y %s %s", extraFlags, packages)
if i != len(module.Source.Paths)-1 {
cmd += "&& "
} else {
cmd += "&& dnf clean all"
}
}
return C.CString(cmd)
}
return C.CString("ERROR: no packages or paths specified")
}
func main() {}