Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
switch to status codes
Browse files Browse the repository at this point in the history
Signed-off-by: Jason McCallister <jason@craftcms.com>
  • Loading branch information
jasonmccallister committed Jul 24, 2020
1 parent fe2db52 commit 2b389a9
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions internal/api/iniset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,45 @@ package api

import (
"context"
"errors"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/craftcms/nitro/validate"
)

func (s *NitrodService) PhpIniSettings(ctx context.Context, request *ChangePhpIniSettingRequest) (*ServiceResponse, error) {
var setting string

switch request.GetSetting() {
case PhpIniSetting_MAX_EXECUTION_TIME:
if err := validate.MaxExecutionTime(request.GetValue()); err != nil {
return nil, err
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
setting = "max_execution_time"
case PhpIniSetting_MAX_INPUT_VARS:
if err := validate.MaxInputVars(request.GetValue()); err != nil {
return nil, err
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
setting = "max_input_vars"
default:
e := errors.New("changing this setting is not authorized")
s.logger.Println(e)
return nil, e
msg := "changing " + PhpIniSetting_name[int32(request.GetSetting())] + " setting is not authorized"
s.logger.Println(msg)
return nil, status.Errorf(codes.InvalidArgument, msg)
}

// change setting using sed
if output, err := s.command.Run("sed", []string{"-i", "s|" + setting + "|" + setting + " = " + request.GetValue() + "|g", "/etc/php/" + request.GetVersion() + "/fpm/php.ini"}); err != nil {
s.logger.Println("error changing ini setting, error:", err)
s.logger.Println("output:", string(output))
return nil, err
return nil, status.Errorf(codes.Unknown, string(output))
}

// restart php-fpm using service
if output, err := s.command.Run("service", []string{"php" + request.GetVersion() + "-fpm", "restart"}); err != nil {
s.logger.Println("error restarting php-fpm, error:", err)
s.logger.Println("output:", string(output))
return nil, err
return nil, status.Errorf(codes.Unknown, string(output))
}

return &ServiceResponse{Message: "successfully changed the ini setting for " + setting + " to " + request.GetValue()}, nil
Expand Down

0 comments on commit 2b389a9

Please # to comment.