func NewAction(action string, allowParseNames bool) *Action { var a Action a.CommandSet = getopt.New() a.CommandSet.SetProgram("docker-fw (init|start|allow|add|add-input|add-two-ways|add-internal|ls|save-hostconfig|replay|drop) containerId") a.CommandSet.SetParameters("\n\nSyntax for all add actions:\n\tdocker-fw (add|add-input|add-two-ways|add-internal) ...") a.Action = action a.VerboseArg = a.CommandSet.BoolVarLong(&a.verbose, "verbose", 'v', "use more verbose output, prints all iptables operations") // define all command line options a.SourceArg = a.CommandSet.StringVarLong(&a.source, "source", 's', "source-specification*", ".") a.SourcePortArg = a.CommandSet.Uint16VarLong(&a.sourcePort, "sport", 0, "Source port, optional", "port") a.DestArg = a.CommandSet.StringVarLong(&a.dest, "dest", 'd', "destination-specification*", ".") a.DestPortArg = a.CommandSet.Uint16VarLong(&a.destPort, "dport", 0, "Destination port, mandatory only for 'add-input', 'add-two-ways' and 'add-internal' actions", "port") a.ProtoArg = a.CommandSet.EnumVarLong(&a.proto, "protocol", 'p', []string{"tcp", "udp"}, "The protocol of the packet to check") a.FilterArg = a.CommandSet.StringVarLong(&a.filter, "filter", 0, "extra iptables conditions") if allowParseNames { a.ReverseLookupContainerIPv4Arg = a.CommandSet.BoolVarLong(&a.reverseLookupContainerIPv4, "rev-lookup", 0, "allow specifying addresses in 172.* subnet and map them back to container names") } // explicitly set all option defaults a.proto = "tcp" a.source = "." a.dest = "." a.sourcePort = 0 a.destPort = 0 a.filter = "" return &a }
func TestHandleArgs(t *testing.T) { /*Testing 3 invalid files*/ testCommand := getopt.New() testCommand.Parse([]string{"a.out", "faketextfile", "fakelogfile", "fakecodefile"}) if _, errs := handleArgs(nil, testCommand); len(errs) != 3 { t.Fatalf("Expected 3 errors, got: %d", len(errs)) } /*Testing 3 valid files*/ testCommand = getopt.New() textfile := createTempFile(t, "textfile") defer os.Remove(textfile.Name()) logfile := createTempFile(t, "logfile") defer os.Remove(logfile.Name()) codefile := createTempFile(t, "codefile") defer os.Remove(codefile.Name()) testCommand.Parse([]string{"a.out", textfile.Name(), logfile.Name(), codefile.Name()}) if files, errs := handleArgs(nil, testCommand); len(errs) != 0 { t.Fatalf("Errors encountered on treatment of valid files") } else if len(files) != 3 { t.Fatalf("Expected 3 files, got: %d", len(files)) } else if string(files[0]) != "Testing fpaste command line client with temp file: textfile" { t.Fatalf("Unexpected content for textfile, got: %s", string(files[0])) } else if string(files[1]) != "Testing fpaste command line client with temp file: logfile" { t.Fatalf("Unexpected content for logfile, got: %s", string(files[1])) } else if string(files[2]) != "Testing fpaste command line client with temp file: codefile" { t.Fatalf("Unexpected content for codefile, got: %s", string(files[2])) } /*Testing invalid stdin*/ testCommand = getopt.New() testCommand.Parse([]string{"a.out"}) if _, errs := handleArgs(&fakeReader{}, testCommand); len(errs) != 1 { t.Fatalf("Error expected on treatment of invalid stdin") } /*Testing stdin*/ testCommand = getopt.New() testCommand.Parse([]string{"a.out"}) testReader := bytes.NewBufferString("Testing fpaste command line client stdin option") if files, errs := handleArgs(testReader, testCommand); len(errs) != 0 { t.Fatalf("Errors encountered on treatment of stdin") } else if len(files) != 1 { t.Fatalf("Expected 1 file, got: %d", len(files)) } else if string(files[0]) != "Testing fpaste command line client stdin option" { t.Fatalf("Unexpected content for stdin, got: %s", string(files[0])) } /*Do not test nil as stdin because os.Stdin is the only option*/ }
func initConfig(args []string) *config { getopt.CommandLine = getopt.New() var flags config flags.help = getopt.BoolLong("help", 'h', "Display this help") flags.priv = getopt.BoolLong("private", 'P', "Private paste flag") flags.user = getopt.StringLong("user", 'u', "", "An alphanumeric username of the paste author") flags.pass = getopt.StringLong("pass", 'p', "", "Add a password") flags.lang = getopt.StringLong("lang", 'l', "Text", "The development language used") flags.expire = getopt.StringLong("expire", 'e', "0", "Seconds after which paste will be deleted from server") getopt.SetParameters("[FILE...]") getopt.CommandLine.Parse(args) return &flags }