Skip to content

Commit

Permalink
feat: os-dependent default entrypoint
Browse files Browse the repository at this point in the history
Co-authored-by: Roberto Abdelkader Martínez Pérez <robertomartinezp@gmail.com>
  • Loading branch information
panchoh and nilp0inter committed Dec 24, 2020
1 parent 0c16b54 commit e72c65c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 8 deletions.
10 changes: 7 additions & 3 deletions internal/client/route_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 0 additions & 1 deletion internal/client/route_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 4 additions & 0 deletions internal/server/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions internal/server/control/entrypoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build !windows

package control

var defaultEntrypoint = "/bin/sh -c"
3 changes: 3 additions & 0 deletions internal/server/control/entrypoint_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package control

var defaultEntrypoint = "cmd.exe /c"
6 changes: 3 additions & 3 deletions internal/server/model/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand All @@ -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"`
}

0 comments on commit e72c65c

Please sign in to comment.