diff --git a/internal/client/route_add.go b/internal/client/route_add.go index f1cdffe..b79e8a0 100644 --- a/internal/client/route_add.go +++ b/internal/client/route_add.go @@ -27,10 +27,14 @@ import ( // AddRoute will add a new route in kapow func AddRoute(host, path, method, entrypoint, command string, w io.Writer) error { url := host + "/routes" - body, _ := json.Marshal(map[string]string{ + payload := map[string]string{ "method": method, "url_pattern": path, - "entrypoint": entrypoint, - "command": command}) + "command": command, + } + if entrypoint != "" { + payload["entrypoint"] = entrypoint + } + body, _ := json.Marshal(payload) return http.Post(url, "application/json", bytes.NewReader(body), w) } diff --git a/internal/client/route_add_test.go b/internal/client/route_add_test.go index e66779f..ca48086 100644 --- a/internal/client/route_add_test.go +++ b/internal/client/route_add_test.go @@ -31,7 +31,6 @@ func TestSuccessOnCorrectRoute(t *testing.T) { JSON(map[string]string{ "method": "GET", "url_pattern": "/hello", - "entrypoint": "", "command": "echo Hello World | kapow set /response/body", }). Reply(http.StatusCreated). diff --git a/internal/cmd/route.go b/internal/cmd/route.go index 4d9e720..e6310af 100644 --- a/internal/cmd/route.go +++ b/internal/cmd/route.go @@ -80,7 +80,7 @@ func init() { // TODO: Add default values for flags and remove path flag routeAddCmd.Flags().String("control-url", getEnv("KAPOW_CONTROL_URL", "http://localhost:8081"), "Kapow! control interface URL") routeAddCmd.Flags().StringP("method", "X", "GET", "HTTP method to accept") - routeAddCmd.Flags().StringP("entrypoint", "e", "/bin/sh -c", "Command to execute") + routeAddCmd.Flags().StringP("entrypoint", "e", "", "Command to execute") routeAddCmd.Flags().StringP("command", "c", "", "Command to pass to the shell") var routeRemoveCmd = &cobra.Command{ diff --git a/internal/server/control/control.go b/internal/server/control/control.go index 693b14e..377fd66 100644 --- a/internal/server/control/control.go +++ b/internal/server/control/control.go @@ -132,6 +132,10 @@ func addRoute(res http.ResponseWriter, req *http.Request) { return } + if route.Entrypoint == "" { + route.Entrypoint = defaultEntrypoint + } + route.ID = id.String() created := funcAdd(route) diff --git a/internal/server/control/entrypoint.go b/internal/server/control/entrypoint.go new file mode 100644 index 0000000..c88d83e --- /dev/null +++ b/internal/server/control/entrypoint.go @@ -0,0 +1,5 @@ +// +build !windows + +package control + +var defaultEntrypoint = "/bin/sh -c" diff --git a/internal/server/control/entrypoint_windows.go b/internal/server/control/entrypoint_windows.go new file mode 100644 index 0000000..cc9633c --- /dev/null +++ b/internal/server/control/entrypoint_windows.go @@ -0,0 +1,3 @@ +package control + +var defaultEntrypoint = "cmd.exe /c" diff --git a/internal/server/model/route.go b/internal/server/model/route.go index 53b9ed5..dd283dd 100644 --- a/internal/server/model/route.go +++ b/internal/server/model/route.go @@ -19,7 +19,7 @@ package model // Route contains the data needed to represent a Kapow! user route. type Route struct { // ID is the unique identifier of the Route. - ID string `json:"id"` + ID string `json:"id,omitempty"` // Method is the HTTP method that will match this Route. Method string `json:"method"` @@ -33,7 +33,7 @@ type Route struct { // // This string will be split according to the shell parsing rules to // be passed as a list to exec.Command. - Entrypoint string `json:"entrypoint"` + Entrypoint string `json:"entrypoint,omitempty"` // Command is the last argument to be passed to exec.Command when // executing the Entrypoint @@ -43,5 +43,5 @@ type Route struct { // It is an output field, its value is ignored as input. Index int `json:"index"` - Debug bool `json:"debug"` + Debug bool `json:"debug,omitempty"` }