xmnt/main.go

92 lines
1.7 KiB
Go
Raw Normal View History

2022-10-07 02:19:04 +09:00
package main
import (
"flag"
"fmt"
"os"
"gensokyo.cafe/xmnt/cfg"
"gensokyo.cafe/xmnt/mnt"
"gensokyo.cafe/xmnt/msg"
)
var (
unmount = flag.Bool("u", false, "Unmount instead of mount")
)
func main() {
if err := cfg.LoadAuto(); err != nil {
2022-10-07 02:48:10 +09:00
msg.Errorf("Failed to load config: %v", err)
os.Exit(1)
2022-10-07 02:19:04 +09:00
}
flag.Parse()
args := flag.Args()
if len(args) == 0 {
flag.Usage()
os.Exit(1)
}
var name, mountPoint string
name = args[0]
if len(args) > 1 {
mountPoint = args[1]
}
matched, err := mnt.MatchAll(name)
if err != nil {
msg.Errorf("Failed to find match for %q: %v", name, err)
os.Exit(1)
}
if len(matched) == 0 {
msg.Errorf("No match for %q", name)
os.Exit(1)
}
if len(matched) > 1 {
msg.Errorf("Ambiguous name %q", name)
msg.Infof("%d matches:", len(matched))
for _, m := range matched {
msg.Infof(" %s", m)
}
os.Exit(1)
}
preset := matched[0]
if mountPoint != "" {
preset.MountPoint = mountPoint
}
mounter, err := mnt.MounterFromPreset(preset)
if err != nil {
msg.Errorf("Failed to initialize mounter: %v", err)
os.Exit(1)
}
switch *unmount {
case false:
err = mounter.Mount()
case true:
err = mounter.Unmount()
}
if err != nil {
msg.Errorf("%v", mountPoint, err)
os.Exit(1)
}
msg.Infof("Success")
}
func init() {
usageText := fmt.Sprintf("Usage: %s [options] <name> [mountpoint]\n\nOptions:\n", os.Args[0])
argsUsageText := `
Arguments:
name Name or path of the device to mount. Can also be name of a preset
mountpoint Mount point (optional)
`
flag.Usage = func() {
_, _ = fmt.Fprint(flag.CommandLine.Output(), usageText)
flag.PrintDefaults()
_, _ = fmt.Fprintln(flag.CommandLine.Output(), argsUsageText)
}
}