Skip to content

Commit

Permalink
feat,test: add 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 Dec 16, 2023
1 parent 6462251 commit ac87579
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 39 deletions.
15 changes: 10 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ jobs:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: 1.19
go-version: 1.21

- name: Test
run: |
go get ./...
go test -v ./...
- name: Build
run: |
go get ./...
go build -trimpath -buildmode=plugin -o plugin.so -v ./...
go build -trimpath -buildmode=plugin -o dnf.so -v ./...
- name: Upload an artifact
uses: actions/upload-artifact@v3
with:
Name: Vib plugin
path: ./plugin.so
name: Vib plugin
path: ./dnf.so

46 changes: 12 additions & 34 deletions plugin.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,33 @@
// Copyright 2023 - 2023, axtlos <axtlos@disroot.org>
// Copyright 2023 - present, K.B.Dharun Krishna <mail@kbdharun.dev>
// SPDX-License-Identifier: GPL-3.0-ONLY

package main

import (
"fmt"
"strings"

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

type ExampleModule struct {
// Mandatory values, your plugin will not work if these are not present!
type DnfModule struct {
Name string `json:"name"`
Type string `json:"type"`

// Additional values such as Source can be added here
Source api.Source
ExtraFlags []string
Packages []string
}

// Plugins can define extra functions that are used internally
// Vib will never call any function other than BuildModule
func fetchSources(source api.Source, name string, recipe *api.Recipe) error {
// The plugin api offers functions to download sources
// To be able to use them, the use of api.Source for
// source definition is recommended
// Using these functions to fetch sources is not required
// but highly recommended to ensure sources are always in the right directory
err := api.DownloadSource(recipe.DownloadsPath, source, name)
if err != nil {
return err
}
err = api.MoveSource(recipe.DownloadsPath, recipe.SourcesPath, source, name)
return err
}

// This is the entry point for plugins that vib calls
// The arguments are required to be (interface{}, recipe) => (string, error)
func BuildModule(moduleInterface interface{}, recipe *api.Recipe) (string, error) {
// It is advisable to convert the interface to an actual struct
// The use of mapstructure for this is recommended, but not required
var module ExampleModule
func BuildModule(moduleInterface interface{}, _ *api.Recipe) (string, error) {
var module DnfModule
err := mapstructure.Decode(moduleInterface, &module)
if err != nil {
return "", err
}
err = fetchSources(module.Source, module.Name, recipe)
if err != nil {
return "", err
}

// The sources will be made available at /sources/ during build
// if the plugins requires manually downloaded sources, they will
// be available in /sources/<modulename>
cmd := fmt.Sprintf("cd /sources/%s && cp * /etc/%s", module.Name, module.Name)
cmd := fmt.Sprintf("dnf install -y %s %s", strings.Join(module.ExtraFlags, " "), strings.Join(module.Packages, " "))

return cmd, nil
}
41 changes: 41 additions & 0 deletions plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2023 - 2023, axtlos <axtlos@disroot.org>
// Copyright 2023 - present, K.B.Dharun Krishna <mail@kbdharun.dev>
// SPDX-License-Identifier: GPL-3.0-ONLY

// Copyright 2023 - 2023, axtlos <axtlos@disroot.org>
// SPDX-License-Identifier: GPL-3.0-ONLY

package main

import (
"testing"

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

type testModule struct {
Name string
Type string
ExtraFlags []string
Packages []string
}

type testCases struct {
module interface{}
expected string
}

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", "-y"}, []string{"bash", "fish"}}, "dnf install -y --verbose bash fish"},
}

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)
}
}
}

0 comments on commit ac87579

Please sign in to comment.