Skip to content
This repository has been archived by the owner on Jan 4, 2025. It is now read-only.

Commit

Permalink
Merge pull request #32 from tulilirockz/fix-discord-fixes
Browse files Browse the repository at this point in the history
fix: positional arguments + mkdirall fixes + other stuff
  • Loading branch information
bketelsen authored Feb 20, 2024
2 parents 3de1c75 + cd9a697 commit 4b40def
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 77 deletions.
41 changes: 25 additions & 16 deletions cmd/addToPath.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

var AddToPathCmd = &cobra.Command{
Use: "add-to-path",
Use: "add-to-path [...SHELL]",
Short: "Add the mounted layer binaries to your path",
Long: `Write a snippet for your shell of the mounted path for the activated bext layers`,
RunE: addToPathCmd,
Expand All @@ -37,7 +37,12 @@ func init() {

func addToPathCmd(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return internal.NewPositionalError("SHELL")
return internal.NewPositionalError("SHELL...")
}
if len(args) > 0 && *fRCPath != "" {
//TODO: make this be possible, honestly, there should be something like zip() to sync up those.
slog.Warn("Cannot write multiple shell snippets with rc path being specified, will write to default paths")
os.Exit(1)
}

user_home, err := os.UserHomeDir()
Expand Down Expand Up @@ -69,23 +74,27 @@ func addToPathCmd(cmd *cobra.Command, args []string) error {
valid_stuff = append(valid_stuff, key)
}

if !slices.Contains(valid_stuff, args[0]) {
slog.Warn(fmt.Sprintf("Could not find shell %s, valid shells are: %s", args[0], strings.Join(valid_stuff, ", ")))
os.Exit(1)
}
for _, shell := range args {
cleaned_shell := path.Base(path.Clean(shell))

var rcPath string
if *fRCPath != "" {
rcPath = path.Clean(*fRCPath)
} else {
rcPath = defaultValues[args[0]].RcPath
}
if !slices.Contains(valid_stuff, cleaned_shell) {
slog.Warn(fmt.Sprintf("Could not find shell %s, valid shells are: %s", cleaned_shell, strings.Join(valid_stuff, ", ")))
os.Exit(1)
}

if _, err := fileio.FileAppendS(rcPath, defaultValues[args[0]].Snippet); err != nil {
return err
}
var rcPath string
if *fRCPath != "" {
rcPath = path.Clean(*fRCPath)
} else {
rcPath = defaultValues[cleaned_shell].RcPath
}

slog.Info(fmt.Sprintf("Successfully written snippet to %s\n", rcPath))
if _, err := fileio.FileAppendS(rcPath, defaultValues[cleaned_shell].Snippet); err != nil {
slog.Warn(fmt.Sprintf("Failed writing %s snippet to %s", cleaned_shell, rcPath), slog.String("source", cleaned_shell), slog.String("target", rcPath))
return err
}
slog.Info(fmt.Sprintf("Successfully written snippet to %s", rcPath))
}

return nil
}
2 changes: 1 addition & 1 deletion cmd/layer/activate/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

var ActivateCmd = &cobra.Command{
Use: "activate",
Use: "activate [TARGET]",
Short: "Activate a layer and refresh sysext",
Long: `Activate a selected layer (symlink it to /var/lib/extensions) and refresh the system extensions store.`,
RunE: activateCmd,
Expand Down
2 changes: 1 addition & 1 deletion cmd/layer/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

var AddCmd = &cobra.Command{
Use: "add",
Use: "add [TARGET]",
Short: "Add a built layer onto the cache and activate it",
Long: `Copy TARGET over to cache-dir as a blob with the TARGET's sha256 as the filename`,
RunE: addCmd,
Expand Down
19 changes: 13 additions & 6 deletions cmd/layer/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"os"
"path"
Expand All @@ -24,7 +25,7 @@ import (
)

var BuildCmd = &cobra.Command{
Use: "build",
Use: "build [CONFIG]",
Short: "Build an image from a configuration file",
Long: `Build an image from a configuration file`,
RunE: buildCmd,
Expand All @@ -51,6 +52,10 @@ func init() {
}

func buildCmd(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return internal.NewPositionalError("CONFIG")
}

pw := percent.NewProgressWriter()

var (
Expand Down Expand Up @@ -78,10 +83,6 @@ func buildCmd(cmd *cobra.Command, args []string) error {
}
pw.AppendTracker(build_tracker.Tracker)

if len(args) < 1 {
return internal.NewPositionalError("CONFIG")
}

config_file_path, err := filepath.Abs(path.Clean(args[0]))
if err != nil {
return err
Expand All @@ -106,6 +107,7 @@ func buildCmd(cmd *cobra.Command, args []string) error {

conn, err := bindings.NewConnection(context.Background(), socket)
if err != nil {
slog.Warn("A podman socket is required, enable it with \"systemctl enable --now --user podman.socket\"")
return err
}
build_tracker.IncrementSection()
Expand All @@ -130,8 +132,13 @@ func buildCmd(cmd *cobra.Command, args []string) error {
tracker := progress.Tracker{Message: "Pulling image", Total: int64(100), Units: progress.UnitsDefault}
pw.AppendTracker(&tracker)

var pull_opt = &images.PullOptions{ProgressWriter: &io.Discard}
if *internal.Config.NoProgress {
pull_opt = &images.PullOptions{}
}

tracker.Increment(0)
if _, err := images.Pull(conn, full_image_name, &images.PullOptions{}); err != nil {
if _, err := images.Pull(conn, full_image_name, pull_opt); err != nil {
return err
}
tracker.Increment(100)
Expand Down
2 changes: 1 addition & 1 deletion cmd/layer/deactivate/deactivate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var DeactivateCmd = &cobra.Command{
Use: "deactivate",
Use: "deactivate [TARGET]",
Short: "Deactivate a layer and refresh sysext",
Long: `Deativate a selected layer (unsymlink it from /var/lib/extensions) and refresh the system extensions store.`,
RunE: deactivateCmd,
Expand Down
23 changes: 13 additions & 10 deletions cmd/layer/getProperty/getProperty.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
var validOptions []string = []string{"NAME", "PACKAGES", "ARCH", "OS", "BINARIES", "ISMOUNTED"}

var GetPropertyCmd = &cobra.Command{
Use: "get-property",
Use: "get-property [LAYER]",
Short: "Get properties from a selected layer or configuration file",
Long: fmt.Sprintf(`Get properties from a selected layer or configuration file
Expand All @@ -31,13 +31,13 @@ Supported properties:
}

var (
fFromFile *string
fFromFile *bool
fSeparator *string
fLogOnly *bool
)

func init() {
fFromFile = GetPropertyCmd.Flags().StringP("from-file", "f", "", "Read data from a file instead of layer")
fFromFile = GetPropertyCmd.Flags().BoolP("from-file", "f", false, "Read data from a file instead of layer")
fLogOnly = GetPropertyCmd.Flags().Bool("log", false, "Do not make a table, just log everything")
fSeparator = GetPropertyCmd.Flags().StringP("separator", "s", "\n", "Separator for listing things like arrays")
}
Expand All @@ -48,25 +48,28 @@ func remove(slice []string, s int) []string {

func getPropertyCmd(cmd *cobra.Command, args []string) error {
var (
target_layer string
config_file_path string
raw_configuration []byte
unmarshalled_config = &internal.LayerConfiguration{}
)
if len(args) < 1 && !*fFromFile {
return internal.NewPositionalError("LAYER")
} else if len(args) < 1 && *fFromFile {
return internal.NewPositionalError("PATH")
}

if !*fLogOnly {
slog.SetDefault(logging.NewMuteLogger())
}

if *fFromFile == "" {
if len(args) < 1 {
return internal.NewPositionalError("LAYER")
}
target_layer = args[0]
if !*fFromFile {
target_layer := args[0]
args = remove(args, 0)
config_file_path = path.Join(internal.Config.ExtensionsMount, target_layer, internal.MetadataFileName)
} else {
config_file_path = path.Clean(*fFromFile)
target_file := args[0]
args = remove(args, 0)
config_file_path = path.Clean(target_file)
}

raw_configuration, err := os.ReadFile(config_file_path)
Expand Down
10 changes: 5 additions & 5 deletions cmd/layer/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (
)

var ListCmd = &cobra.Command{
Use: "list",
Use: "list [LAYER/HASH]",
Short: "List layers in cache and in activation",
Long: `List layers in the cache directory, their blobs and symlinks in their cache`,
Long: `List layers in the cache directory, their blobs and symlinks in their cache. Can also single check a layer or hash specified.`,
RunE: listCmd,
}

var (
fLayer *string
fQuiet *bool
fCheck *bool
fVerbose *bool
fActivated *bool
fSeparator *string
Expand All @@ -34,7 +34,7 @@ var (
func init() {
fVerbose = ListCmd.Flags().BoolP("verbose", "v", false, "List all layer's hashes")
fLayer = ListCmd.Flags().StringP("layer", "l", "", "List hashes inside the target layer")
fQuiet = ListCmd.Flags().BoolP("quiet", "q", false, "Only check for layer or hash existence instead of listing")
fCheck = ListCmd.Flags().BoolP("check", "c", false, "Only check for layer or hash existence instead of listing")
fActivated = ListCmd.Flags().Bool("activated", false, "List only activated layers")
fSeparator = ListCmd.Flags().StringP("separator", "s", "\n", "Separator for listing things like arrays")
fLogOnly = ListCmd.Flags().Bool("log", false, "Do not make a table, just log everything")
Expand All @@ -53,7 +53,7 @@ func listCmd(cmd *cobra.Command, args []string) error {
return err
}

if *fQuiet {
if *fCheck {
if len(args) < 1 {
return internal.NewPositionalError("LAYER")
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/mount/extensions/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package extensions

import (
"fmt"
"log/slog"
"os/exec"
"strings"

"github.com/spf13/cobra"
"github.com/ublue-os/bext/internal"
Expand Down Expand Up @@ -41,6 +43,7 @@ func extensionsCmd(cmd *cobra.Command, args []string) error {
command = append(command, "merge")
}

slog.Debug("Running systemd-sysext", slog.String("command", strings.Join(command, " ")))
out, err := exec.Command("systemd-sysext", command...).Output()
if err != nil {
fmt.Printf("%s\n", out)
Expand Down
22 changes: 16 additions & 6 deletions cmd/mount/path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func pathCmd(cmd *cobra.Command, args []string) error {
}

if *internal.Config.UnmountFlag {
slog.Debug("Unmounting", slog.String("target", path_path))
if err := syscall.Unmount(path_path, 0); err != nil {
slog.Warn("Failed unmounting path", slog.String("target", path_path))
return err
}
slog.Info("Successfuly unmounted path "+path_path, slog.String("path", path_path))
Expand All @@ -48,6 +50,7 @@ func pathCmd(cmd *cobra.Command, args []string) error {

layers, err := os.ReadDir(extensions_mount)
if err != nil {
slog.Warn("No layers are mounted")
return err
}

Expand All @@ -59,30 +62,37 @@ func pathCmd(cmd *cobra.Command, args []string) error {
valid_layers = append(valid_layers, layer.Name())
}

if err := os.MkdirAll(path_path, 0755); err != nil {
slog.Warn("Failed creating mount path", slog.String("target", path_path))
return err
}

if len(layers) == 0 {
slog.Warn("No layers are mounted")
slog.Warn("No valid layers are mounted")
os.Exit(1)
} else if len(layers) == 1 {
mount_path := path.Join(extensions_mount, layers[0].Name(), "bin")
if _, err := os.Stat(path_path); err == nil {
slog.Debug("Unmounting", slog.String("path", path_path))
_ = syscall.Unmount(path_path, 0)
}

if err := syscall.Mount(mount_path, path_path, "bind", uintptr(syscall.MS_BIND|syscall.MS_RDONLY), ""); err != nil {
slog.Warn("Failed mounting bindmount to path", slog.String("source", mount_path), slog.String("target", path_path))
return err
}
} else {
if _, err := os.Stat(path_path); err == nil {
slog.Debug("Unmounting", slog.String("path", path_path))
_ = syscall.Unmount(path_path, 0)
}

slog.Debug("Unmounting", slog.String("path", path_path))
_ = syscall.Unmount(path_path, 0)

slog.Debug("Mounting path with overlayFS", slog.String("layers", strings.Join(valid_layers, " ")), slog.String("target", path_path))
err = syscall.Mount("none", path_path, "overlayfs", uintptr(syscall.MS_RDONLY|syscall.MS_NODEV|syscall.MS_NOATIME), "lowerdir="+strings.Join(valid_layers, ":"))
if err != nil {
return err
}
}

slog.Info("Successfully mounted PATH")

return nil
}
Loading

0 comments on commit 4b40def

Please sign in to comment.