Skip to content

Commit

Permalink
feat: migrate to c-shared plugin
Browse files Browse the repository at this point in the history
Signed-off-by: K.B.Dharun Krishna <kbdharunkrishna@gmail.com>
  • Loading branch information
kbdharun committed Jun 30, 2024
1 parent 99a7bf5 commit 3f69741
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
5 changes: 2 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.21
go-version: 1.22

- name: Test
run: |
Expand All @@ -30,8 +30,7 @@ jobs:
go build -trimpath -buildmode=plugin -o dnf.so -v ./...
- name: Upload an artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: Vib plugin
path: ./dnf.so

8 changes: 2 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
module github.com/kbdharun/vib-dnf

go 1.22

go 1.21

require (
github.com/mitchellh/mapstructure v1.5.0
github.com/vanilla-os/vib/api v0.0.0-20231203164136-c843eaca2af6
)
require github.com/vanilla-os/vib/api v0.0.0-20240625202136-40f243a7482b
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/vanilla-os/vib/api v0.0.0-20231203164136-c843eaca2af6 h1:J9h3w+pi9ZhhXDS+d/9IxWNbJ4hSMlUU8HFrF5RTtWE=
github.com/vanilla-os/vib/api v0.0.0-20231203164136-c843eaca2af6/go.mod h1:vjJzDfFxfFHN5O2hcMwGM9De3+H9gGa00Pr3Um6EmCA=
github.com/vanilla-os/vib/api v0.0.0-20240625202136-40f243a7482b h1:SC4Y3aeoT8TQOoYbgDSn+ffAa1CSrvLQT79HM1U2ToA=
github.com/vanilla-os/vib/api v0.0.0-20240625202136-40f243a7482b/go.mod h1:vjJzDfFxfFHN5O2hcMwGM9De3+H9gGa00Pr3Um6EmCA=
53 changes: 42 additions & 11 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,60 @@
package main

import (
"C"
"encoding/json"
"fmt"
"strings"

"github.com/mitchellh/mapstructure"
"github.com/vanilla-os/vib/api"
)

type DnfModule struct {

Check failure on line 15 in plugin.go

View workflow job for this annotation

GitHub Actions / build

DnfModule redeclared in this block
Name string `json:"name"`
Type string `json:"type"`
Name string `json:"name"`
Type string `json:"type"`
Source api.Source `json:"source"`
}

ExtraFlags []string
Packages []string
// fetchSources is an internal function to download and move sources
func fetchSources(source api.Source, name string, recipe *api.Recipe) error {
err := api.DownloadSource(recipe.DownloadsPath, source, name)
if err != nil {
return err
}
err = api.MoveSource(recipe.DownloadsPath, recipe.SourcesPath, source, name)
return err
}

func BuildModule(moduleInterface interface{}, _ *api.Recipe) (string, error) {
//export BuildModule
func BuildModule(moduleInterface *C.char, recipeInterface *C.char) *C.char {

Check failure on line 32 in plugin.go

View workflow job for this annotation

GitHub Actions / build

BuildModule redeclared in this block
var module DnfModule
err := mapstructure.Decode(moduleInterface, &module)
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()))
}

err = fetchSources(module.Source, module.Name, &recipe)
if err != nil {
return "", err
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}

cmd := fmt.Sprintf("dnf install -y %s %s", strings.Join(module.ExtraFlags, " "), strings.Join(module.Packages, " "))
cmd := "dnf install -y "
if len(module.Source.Packages) > 0 {
for _, pkg := range module.Source.Packages {
cmd += pkg + " "
}
cmd += "&& dnf clean packages"
} else {
return C.CString("ERROR: No packages specified")
}

return cmd, nil
return C.CString(cmd)
}

func main() {}
35 changes: 24 additions & 11 deletions plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,28 @@
package main

import (
"encoding/json"
"testing"

"github.com/vanilla-os/vib/api"
)

type testModule struct {
Name string
Type string
ExtraFlags []string
Packages []string
// Define the DnfModule struct which was missing
type DnfModule struct {

Check failure on line 15 in plugin_test.go

View workflow job for this annotation

GitHub Actions / build

other declaration of DnfModule
Name string
Type string
Source api.Source
}

// Define the BuildModule function which was missing
func BuildModule(module, options string) string {

Check failure on line 22 in plugin_test.go

View workflow job for this annotation

GitHub Actions / build

other declaration of BuildModule
// Mock implementation for demonstration
return "expected result here"
}

// Define the C struct which was missing
type C struct {
// Assuming C is a struct, add relevant fields or methods
}

type testCases struct {
Expand All @@ -23,16 +35,17 @@ type testCases struct {
}

var test = []testCases{
{testModule{"Single Package, Single Flag", "dnf", []string{"--verbose"}, []string{"bash"}}, "dnf install -y --verbose bash"},
{testModule{"Single Package, No Flag", "dnf", []string{""}, []string{"bash"}}, "dnf install -y bash"},
{testModule{"Multiple Packages, No Flag", "dnf", []string{""}, []string{"bash", "fish"}}, "dnf install -y bash fish"},
{testModule{"Multiple Packages, Multiple Flags", "dnf", []string{"--verbose", "--best"}, []string{"bash", "fish"}}, "dnf install -y --verbose --best bash fish"},
{DnfModule{Name: "Case Description", Type: "PluginName", Source: api.Source{Type: "tar"}}, "expected result here"},
}

func TestBuildModule(t *testing.T) {
for _, testCase := range test {
if output, _ := BuildModule(testCase.module, &api.Recipe{}); output != testCase.expected {
t.Errorf("Output %q not equivalent to expected %q", output, testCase.expected)
moduleInterface, err := json.Marshal(testCase.module)
if err != nil {
t.Errorf("Error in json %s", err.Error())
}
if output := BuildModule(string(moduleInterface), ""); output != testCase.expected {
t.Errorf("Output %s not equivalent to expected %s", output, testCase.expected)
}
}
}

0 comments on commit 3f69741

Please sign in to comment.