Commit c71d2a27 authored by ale's avatar ale

allow type-specific options in the "create" command

parent 34b8955b
......@@ -146,10 +146,13 @@ func (c *Client) doRequest(url string, args url.Values) error {
return outErr
}
func (c *Client) Create(setName, typeName string) error {
func (c *Client) Create(setName, typeName string, typeOptions []string) error {
values := url.Values{}
values.Set("set", setName)
values.Set("type", typeName)
if typeOptions != nil {
values.Set("type_options", strings.Join(typeOptions, "|"))
}
return c.doRequest("/api/1/ipset/create", values)
}
......@@ -193,7 +196,11 @@ func main() {
} else if *doDel {
err = client.Del(flag.Arg(0), flag.Arg(1))
} else if *doCreate {
err = client.Create(flag.Arg(0), flag.Arg(1))
var typeOptions []string
if flag.NArg() > 2 {
typeOptions = flag.Args()[2:]
}
err = client.Create(flag.Arg(0), flag.Arg(1), typeOptions)
}
if err != nil {
log.Fatal(err)
......
......@@ -9,14 +9,16 @@ import (
// Create a set.
type CreateCommand struct {
SetName string `json:"set"`
TypeName string `json:"type"`
SetName string `json:"set"`
TypeName string `json:"type"`
TypeOptions []string `json:"type_options"`
}
func NewCreateCommand(setName, typeName string) *CreateCommand {
func NewCreateCommand(setName, typeName string, typeOptions []string) *CreateCommand {
return &CreateCommand{
SetName: setName,
TypeName: typeName,
SetName: setName,
TypeName: typeName,
TypeOptions: typeOptions,
}
}
......@@ -25,8 +27,12 @@ func (c *CreateCommand) CommandName() string {
}
func (c *CreateCommand) Apply(server raft.Server) (interface{}, error) {
log.Printf("Apply(create, '%s', '%s')", c.SetName, c.TypeName)
log.Printf("Apply(create, '%s', '%s', %+v)", c.SetName, c.TypeName, c.TypeOptions)
ips := server.Context().(*ipset.IPSet)
ips.Run("create", c.SetName, c.TypeName)
createArgs := []string{"create", c.SetName, c.TypeName}
if c.TypeOptions != nil && len(c.TypeOptions) > 0 {
createArgs = append(createArgs, c.TypeOptions...)
}
ips.Run(createArgs...)
return nil, nil
}
......@@ -28,12 +28,14 @@ func (ips *IPSet) Run(args ...string) error {
setName := args[0]
ips.knownSets[setName] = struct{}{}
cmd := exec.Command(ipsetBinary, "-q", "-!", args...)
optArgs := []string{"-q", "-!"}
optArgs = append(optArgs, args...)
cmd := exec.Command(ipsetBinary, optArgs...)
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
log.Printf("Error: command \"%s %s\" failed:\n%s", ipsetBinary, args, stderr.String())
log.Printf("Error: command \"%s %s\" failed:\n%s", ipsetBinary, optArgs, stderr.String())
return err
}
......
......@@ -215,8 +215,13 @@ func (s *Server) createHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Empty set/type", http.StatusBadRequest)
return
}
typeOptions := []string{}
typeOptionsStr := r.FormValue("type_options")
if typeOptionsStr != "" {
typeOptions = strings.Split(typeOptionsStr, "|")
}
if _, err := s.raftServer.Do(command.NewCreateCommand(setName, typeName)); err != nil {
if _, err := s.raftServer.Do(command.NewCreateCommand(setName, typeName, typeOptions)); err != nil {
s.handleError(w, err)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment