generated from Vanilla-OS/vib-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: K.B.Dharun Krishna <kbdharunkrishna@gmail.com>
- Loading branch information
Showing
3 changed files
with
63 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |