From d66e6aebcfa7ea9ca7c869cb6ca8dc19851fa20f Mon Sep 17 00:00:00 2001 From: rsteube Date: Thu, 2 Dec 2021 13:23:28 +0100 Subject: [PATCH] added mount --- completers/dfc_completer/cmd/root.go | 2 +- completers/find_completer/cmd/root.go | 2 +- completers/mkfs_completer/cmd/root.go | 2 +- .../mount_completer/cmd/action/option.go | 71 ++++++++++ .../mount_completer/cmd/action/source.go | 48 +++++++ completers/mount_completer/cmd/root.go | 132 ++++++++++++++++++ completers/mount_completer/main.go | 7 + pkg/actions/fs/blockdevice.go | 99 +++++++++++++ pkg/actions/fs/filesystem.go | 8 +- pkg/actions/fs/fs.go | 39 ------ pkg/actions/fs/mount.go | 28 ++++ 11 files changed, 393 insertions(+), 45 deletions(-) create mode 100644 completers/mount_completer/cmd/action/option.go create mode 100644 completers/mount_completer/cmd/action/source.go create mode 100644 completers/mount_completer/cmd/root.go create mode 100644 completers/mount_completer/main.go create mode 100644 pkg/actions/fs/blockdevice.go create mode 100644 pkg/actions/fs/mount.go diff --git a/completers/dfc_completer/cmd/root.go b/completers/dfc_completer/cmd/root.go index b014149c81..c12274ff5b 100644 --- a/completers/dfc_completer/cmd/root.go +++ b/completers/dfc_completer/cmd/root.go @@ -61,7 +61,7 @@ func init() { }), "q": carapace.ActionValues("name", "type", "mount"), "t": carapace.ActionMultiParts(",", func(c carapace.Context) carapace.Action { - return fs.ActionFileSystemTypes().Invoke(c).Filter(c.Parts).ToA() + return fs.ActionFilesystemTypes().Invoke(c).Filter(c.Parts).ToA() }), "u": carapace.ActionValuesDescribed( "h", "Human readable", diff --git a/completers/find_completer/cmd/root.go b/completers/find_completer/cmd/root.go index 05fb255b64..ca80a90370 100644 --- a/completers/find_completer/cmd/root.go +++ b/completers/find_completer/cmd/root.go @@ -115,7 +115,7 @@ func init() { "all", "Enable all of the other debug options (but help).", "help", "Explain the debugging options.", ), - "fstype": fs.ActionFileSystemTypes(), + "fstype": fs.ActionFilesystemTypes(), "group": os.ActionGroups(), "perm": fs.ActionFileModes(), "regextype": carapace.ActionValues("findutils-default", "ed", "emacs", "gnu-awk", "grep", "posix-awk", "awk", "posix-basic", "posix-egrep", "egrep", "posix-extended", "posix-minimal-basic", "sed"), diff --git a/completers/mkfs_completer/cmd/root.go b/completers/mkfs_completer/cmd/root.go index 3c115b0b91..53de932973 100644 --- a/completers/mkfs_completer/cmd/root.go +++ b/completers/mkfs_completer/cmd/root.go @@ -25,7 +25,7 @@ func init() { rootCmd.Flags().BoolP("version", "V", false, "display version") carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{ - "type": fs.ActionFileSystemTypes(), + "type": fs.ActionFilesystemTypes(), }) carapace.Gen(rootCmd).PositionalCompletion( diff --git a/completers/mount_completer/cmd/action/option.go b/completers/mount_completer/cmd/action/option.go new file mode 100644 index 0000000000..ff5dd4d168 --- /dev/null +++ b/completers/mount_completer/cmd/action/option.go @@ -0,0 +1,71 @@ +package action + +import ( + "github.com/rsteube/carapace" + "github.com/rsteube/carapace-bin/pkg/actions/fs" +) + +func ActionOptions() carapace.Action { + return carapace.ActionMultiParts("=", func(c carapace.Context) carapace.Action { + switch len(c.Parts) { + case 0: + return carapace.ActionValuesDescribed( + "async", "All I/O to the filesystem should be done asynchronously.", + "atime", "Do not use the noatime feature.", + "noatime", "Do not update inode access times on this filesystem.", + "auto", "Can be mounted with the -a option.", + "noauto", "Can only be mounted explicitly.", + "context=", "Set context", + "fscontext=", "Set context", + "defcontext=", "Set context", + "rootcontext=", "Set context", + "defaults", "Use the default options", + "dev", "Interpret character or block special devices on the filesystem.", + "nodev", "Do not interpret character or block special devices on the filesystem.", + "diratime", "Update directory inode access times on this filesystem.", + "nodiratime", "Do not update directory inode access times on this filesystem.", + "dirsync", "All directory updates within the filesystem should be done synchronously.", + "exec", "Permit execution of binaries.", + "noexec", "Do not permit direct execution of any binaries on the mounted filesystem.", + "group", "Allow an ordinary user to mount the filesystem if one of that user’s groups matches the group of the device.", + "iversion", "Every time the inode is modified, the i_version field will be incremented.", + "noiversion", "Do not increment the i_version inode field.", + "mand", "Allow mandatory locks on this filesystem.", + "nomand", "Do not allow mandatory locks on this filesystem.", + "_netdev", "The filesystem resides on a device that requires network access", + "nofail", "Do not report errors for this device if it does not exist.", + "relatime", "Update inode access times relative to modify or change time.", + "norelatime", "Do not use the relatime feature.", + "strictatime", "Allows to explicitly request full atime updates.", + "nostrictatime", "Use the kernel’s default behavior for inode access time updates.", + "lazytime", "Only update times on the in-memory version of the file inode.", + "nolazytime", "Do not use the lazytime feature.", + "suid", "Honor set-user-ID and set-group-ID bits or file capabilities", + "nosuid", "Do not honor set-user-ID and set-group-ID bits or file capabilities", + "silent", "Turn on the silent flag.", + "loud", "Turn off the silent flag.", + "owner", "Allow an ordinary user to mount the filesystem if that user is the owner of the device.", + "remount", "Attempt to remount an already-mounted filesystem.", + "ro", "Mount the filesystem read-only.", + "rw", "Mount the filesystem read-write.", + "sync", "All I/O to the filesystem should be done synchronously.", + "user", "Allow an ordinary user to mount the filesystem.", + "nouser", "Forbid an ordinary user to mount the filesystem.", + "users", "Allow any user to mount and to unmount the filesystem", + "X-mount.mkdir", "Allow to make a target directory if it does not exist yet.", + "X-mount.subdir=", "Allow mounting sub-directory from a filesystem instead of the root directory.", + "nosymfollow", "Do not follow symlinks when resolving paths.", + ) + case 1: + switch c.Parts[0] { + case "X-mount.mkdir": + return fs.ActionFileModesNumeric() + + default: + return carapace.ActionValues() + } + default: + return carapace.ActionValues() + } + }) +} diff --git a/completers/mount_completer/cmd/action/source.go b/completers/mount_completer/cmd/action/source.go new file mode 100644 index 0000000000..cd63236657 --- /dev/null +++ b/completers/mount_completer/cmd/action/source.go @@ -0,0 +1,48 @@ +package action + +import ( + "github.com/rsteube/carapace" + "github.com/rsteube/carapace-bin/pkg/actions/fs" + "github.com/rsteube/carapace-bin/pkg/util" +) + +func ActionSources() carapace.Action { + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { + if util.HasPathPrefix(c.CallbackValue) { + return carapace.Batch( + carapace.ActionFiles(), + fs.ActionBlockDevices(), + ).ToA() + } + return carapace.ActionMultiParts("=", func(c carapace.Context) carapace.Action { + switch len(c.Parts) { + case 0: + return carapace.ActionValuesDescribed( + "LABEL", "specifies device by filesystem label", + "UUID", "specifies device by filesystem UUID", + "PARTLABEL", "specifies device by partition label", + "PARTUUID", "specifies device by partition UUID", + "ID", "specifies device by udev hardware ID", + ).Invoke(c).Suffix("=").ToA() + case 1: + switch c.Parts[0] { + case "LABEL": + return fs.ActionLabels() + case "UUID": + return fs.ActionUuids() + case "PARTLABEL": + return fs.ActionPartLabels() + case "PARTUUID": + return fs.ActionPartUuids() + case "ID": + // TODO + return carapace.ActionValues() + default: + return carapace.ActionValues() + } + default: + return carapace.ActionValues() + } + }) + }) +} diff --git a/completers/mount_completer/cmd/root.go b/completers/mount_completer/cmd/root.go new file mode 100644 index 0000000000..ce59b4f47d --- /dev/null +++ b/completers/mount_completer/cmd/root.go @@ -0,0 +1,132 @@ +package cmd + +import ( + "strings" + + "github.com/rsteube/carapace" + "github.com/rsteube/carapace-bin/completers/mount_completer/cmd/action" + "github.com/rsteube/carapace-bin/pkg/actions/fs" + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "mount", + Short: "mount a filesystem", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func Execute() error { + return rootCmd.Execute() +} +func init() { + carapace.Gen(rootCmd).Standalone() + + rootCmd.Flags().BoolP("all", "a", false, "mount all filesystems mentioned in fstab") + rootCmd.Flags().BoolP("bind", "B", false, "mount a subtree somewhere else (same as -o bind)") + rootCmd.Flags().BoolP("fake", "f", false, "dry run; skip the mount(2) syscall") + rootCmd.Flags().BoolP("fork", "F", false, "fork off for each device (use with -a)") + rootCmd.Flags().StringP("fstab", "T", "", "alternative file to /etc/fstab") + rootCmd.Flags().BoolP("help", "h", false, "display this help") + rootCmd.Flags().BoolP("internal-only", "i", false, "don't call the mount. helpers") + rootCmd.Flags().StringP("label", "L", "", "synonym for LABEL=