diff options
| author | Joe Richey joerichey@google.com <joerichey@google.com> | 2017-10-12 17:59:45 -0700 |
|---|---|---|
| committer | Joseph Richey <joerichey94@gmail.com> | 2017-10-19 02:22:25 -0700 |
| commit | b4299090c3e503ba0c49a6086b1a46c218ca45f4 (patch) | |
| tree | 889adbf3da9616a5c6eaa783291e5f94c01955a2 /ext4/ext4.go | |
| parent | 921f1c977c4e0704f61e3a7c092d3a4317ab278c (diff) | |
Command, Context, command line splitting setup
Diffstat (limited to 'ext4/ext4.go')
| -rw-r--r-- | ext4/ext4.go | 155 |
1 files changed, 47 insertions, 108 deletions
diff --git a/ext4/ext4.go b/ext4/ext4.go index 063d68c..80d229d 100644 --- a/ext4/ext4.go +++ b/ext4/ext4.go @@ -20,124 +20,63 @@ package main import ( - "flag" "fmt" - "io" - "io/ioutil" - "os" - "time" - "github.com/urfave/cli" + "github.com/google/fscrypt/cmd" ) +// Arguments used with the ext4 enable/disable commands. var ( - // Setup command parsing - cmdName = os.Args[0] - set = flag.NewFlagSet(cmdName, flag.ContinueOnError) - // Flags for our command - forceFlag = set.Bool("force", false, "Suppress all warnings and do not prompt") - versionFlag = set.Bool("version", false, "Print the fscrypt version.") - helpFlag = set.Bool("help", false, "Print this help text.") - // fscrypt's version (set by Makefile) - version string -) - -const ( - manPage = "fscrypt-ext4(8)" - manBrief = "enable or disable encryption on an ext4 filesystem" - usageFmt = ` -Usage: - %[1]s [enable | disable] <mountpoint> [--force] - %[1]s --help - %[1]s --version - -Arguments: - <mountpoint> - path to an ext4 filesystem -` -) - -func printAndExit(err error, printUsage bool) { - var w io.Writer - var rc int - if err == nil { - w = os.Stdout - rc = 0 - fmt.Fprintf(w, "%s - %s\n", cmdName, manBrief) - } else { - w = os.Stderr - rc = 1 - fmt.Fprintf(w, "%s: %v\n", cmdName, err) + MountpointArg = &cmd.Argument{ + ArgName: "mountpoint", + Usage: "the path to an ext4 filesystem's mountpoint", } - if printUsage { - fmt.Fprintf(w, usageFmt, cmdName) - fmt.Fprintln(w, "\nOptions:") - set.VisitAll(func(f *flag.Flag) { - fmt.Fprintf(w, "\t--%s\n\t\t%s\n", f.Name, f.Usage) - }) - fmt.Fprintf(w, "\nSee the %s man page for more info.\n", manPage) + DeviceArg = &cmd.Argument{ + ArgName: "device", + Usage: "the path to a device containing an ext4 filesystem", } - os.Exit(rc) -} - -func main() { - // Create our command line application - app := cli.NewApp() - app.Usage = shortUsage - app.Authors = Authors - app.Copyright = apache2GoogleCopyright - - // Grab the version and compilation time passed in from the Makefile. - app.Version = version - app.Compiled, _ = time.Parse(time.UnixDate, buildTime) - app.OnUsageError = onUsageError - - // Setup global flags - cli.HelpFlag = helpFlag - cli.VersionFlag = versionFlag - cli.VersionPrinter = func(c *cli.Context) { - cli.HelpPrinter(c.App.Writer, versionInfoTemplate, c.App) - } - app.Flags = universalFlags - - // We hide the help subcommand so that "fscrypt <command> --help" works - // and "fscrypt <command> help" does not. - app.HideHelp = true + Ext4Usage = fmt.Sprintf("(%s | %s) [options]", MountpointArg, DeviceArg) +) - // Initialize command list and setup all of the commands. - app.Action = defaultAction - app.Commands = []cli.Command{Setup, Encrypt, Unlock, Purge, Status, Metadata} - for i := range app.Commands { - setupCommand(&app.Commands[i]) - } +// Commands for running the ext4 enable/disable commands. +var () + +var Ext4Command = &cmd.Command{ + Title: "toggle ext4 filesystem encryption flag", + UsageLines: []string{ + fmt.Sprintf("(enable | disable) %s", Ext4Usage), + cmd.VersionUsage, + }, + SubCommands: []*cmd.Command{EnableCommand, DisableCommand, cmd.VersionCommand}, + Arguments: []*cmd.Argument{MountpointArg, DeviceArg}, + Flags: []cmd.Flag{cmd.ForceFlag, cmd.VerboseFlag, cmd.HelpFlag}, + ManPage: &cmd.ManPage{ + Title: "fscrypt-ext4", + Section: 8, + }, +} - app.Run(os.Args) +var EnableCommand = &cmd.Command{ + Name: "enable", + Title: "turn on encryption for an ext4 filesystem", + UsageLines: []string{Ext4Usage}, + InheritArguments: true, + InheritFlags: true, + Action: func(ctx *cmd.Context) error { return toggleState(ctx, true) }, +} - set.SetOutput(ioutil.Discard) - if err := set.Parse(os.Args[1:]); err != nil { - printAndExit(err, true) - } - if *helpFlag { - printAndExit(nil, true) - } - if *versionFlag { - fmt.Println(version) - return - } - if set.NArg() != 2 { - printAndExit(fmt.Errorf("expected 2 arguments (got %d)", set.NArg()), true) - } +var DisableCommand = &cmd.Command{ + Name: "disable", + Title: "turn off encryption for an ext4 filesystem", + UsageLines: []string{Ext4Usage}, + InheritArguments: true, + InheritFlags: true, + Action: func(ctx *cmd.Context) error { return toggleState(ctx, false) }, +} - _, err := NewExt4Filesystem(set.Arg(1)) - if err != nil { - printAndExit(err, false) - } +func main() { Ext4Command.Run() } - switch command := set.Arg(0); command { - case "enable": - fmt.Println("Enabling encryption not implemented") - case "disable": - fmt.Println("Disabling encryption not implemented") - default: - printAndExit(fmt.Errorf("invalid command %q", command), true) - } +func toggleState(ctx *cmd.Context, enable bool) error { + fmt.Fprintf(cmd.Output, "Toggle value = %v", enable) + return nil } |