refactor: allow any commands and user inputs

This commit is contained in:
Saw-jan
2024-07-08 14:19:55 +05:45
parent d8dff89c1c
commit 2b88c194bc
2 changed files with 56 additions and 26 deletions

View File

@@ -230,13 +230,14 @@ func waitUntilCompleteShutdown() (bool, string) {
return true, "oCIS server stopped successfully"
}
func RunCommand(command string) (int, string) {
func RunCommand(command string, inputs []string) (int, string) {
logs := new(strings.Builder)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// build the command
c := exec.CommandContext(ctx, config.Get("bin"), "idm", "resetpassword", "-u", "admin")
cmdArgs := strings.Split(command, " ")
c := exec.CommandContext(ctx, config.Get("bin"), cmdArgs...)
// Start the command with a pty (pseudo terminal)
// This is required to interact with the command
@@ -246,7 +247,6 @@ func RunCommand(command string) (int, string) {
}
defer ptyF.Close()
inputs := []string{"demo", "demo"}
for _, input := range inputs {
fmt.Fprintf(ptyF, "%s\n", input)
}

View File

@@ -33,16 +33,15 @@ func parseJsonBody(reqBody io.ReadCloser) (map[string]any, error) {
return bodyMap, nil
}
func sendResponse(res http.ResponseWriter, success bool, message string) {
func sendResponse(res http.ResponseWriter, statusCode int, message string) {
res.Header().Set("Content-Type", "application/json")
res.WriteHeader(statusCode)
var status string
if success {
if statusCode == http.StatusOK {
status = "OK"
res.WriteHeader(http.StatusOK)
} else {
status = "ERROR"
res.WriteHeader(http.StatusInternalServerError)
}
resBody := BasicResponse{
@@ -77,13 +76,13 @@ func sendCmdResponse(res http.ResponseWriter, exitCode int, message string) {
func SetEnvHandler(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPut {
http.Error(res, "Method not allowed", http.StatusMethodNotAllowed)
sendResponse(res, http.StatusMethodNotAllowed, "")
return
}
envBody, err := parseJsonBody(req.Body)
if err != nil {
http.Error(res, "Bad request", http.StatusBadRequest)
sendResponse(res, http.StatusMethodNotAllowed, "Invalid json body")
return
}
@@ -98,16 +97,17 @@ func SetEnvHandler(res http.ResponseWriter, req *http.Request) {
success, _ := ocis.Restart(ocis.EnvConfigs)
if success {
message = "oCIS configured successfully"
} else {
message = "Failed to restart oCIS with new configuration"
sendResponse(res, http.StatusOK, message)
return
}
sendResponse(res, success, message)
message = "Failed to restart oCIS with new configuration"
sendResponse(res, http.StatusInternalServerError, message)
}
func RollbackHandler(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodDelete {
http.Error(res, "Method not allowed", http.StatusMethodNotAllowed)
sendResponse(res, http.StatusMethodNotAllowed, "")
return
}
@@ -116,31 +116,37 @@ func RollbackHandler(res http.ResponseWriter, req *http.Request) {
success, _ := ocis.Restart(os.Environ())
if success {
message = "oCIS configuration rolled back successfully"
} else {
message = "Failed to restart oCIS with initial configuration"
sendResponse(res, http.StatusOK, message)
return
}
sendResponse(res, success, message)
message = "Failed to restart oCIS with initial configuration"
sendResponse(res, http.StatusInternalServerError, message)
}
func StopOcisHandler(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(res, "Method not allowed", http.StatusMethodNotAllowed)
sendResponse(res, http.StatusMethodNotAllowed, "")
return
}
success, message := ocis.Stop()
sendResponse(res, success, message)
if success {
sendResponse(res, http.StatusOK, message)
return
}
sendResponse(res, http.StatusInternalServerError, message)
}
func StartOcisHandler(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(res, "Method not allowed", http.StatusMethodNotAllowed)
sendResponse(res, http.StatusMethodNotAllowed, "")
return
}
if ocis.IsOcisRunning() {
sendResponse(res, false, "oCIS server is already running")
sendResponse(res, http.StatusInternalServerError, "oCIS server is already running")
return
}
@@ -148,27 +154,51 @@ func StartOcisHandler(res http.ResponseWriter, req *http.Request) {
go ocis.Start(nil)
success, message := ocis.WaitForConnection()
sendResponse(res, success, message)
if success {
sendResponse(res, http.StatusOK, message)
return
}
sendResponse(res, http.StatusInternalServerError, message)
}
func CommandHandler(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(res, "Method not allowed", http.StatusMethodNotAllowed)
sendResponse(res, http.StatusMethodNotAllowed, "")
return
}
if req.Body == nil {
http.Error(res, "Bad request", http.StatusBadRequest)
sendResponse(res, http.StatusBadRequest, "Body is missing")
return
}
body, err := io.ReadAll(req.Body)
body, err := parseJsonBody(req.Body)
if err != nil {
http.Error(res, "Bad request", http.StatusBadRequest)
sendResponse(res, http.StatusBadRequest, "Invalid json body")
return
}
if _, ok := body["command"]; !ok {
sendResponse(res, http.StatusBadRequest, "Command is missing")
return
}
exitCode, output := ocis.RunCommand(string(body))
command := body["command"].(string)
stdIn := []string{}
if _, ok := body["inputs"]; ok {
if inputs, ok := body["inputs"].([]interface{}); ok {
for _, input := range inputs {
if _, ok := input.(string); ok {
stdIn = append(stdIn, input.(string))
} else {
sendResponse(res, http.StatusBadRequest, "Invalid input data. Expected string")
return
}
}
}
}
exitCode, output := ocis.RunCommand(command, stdIn)
sendCmdResponse(res, exitCode, output)
}