* Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package utils
import (
"encoding/json"
"errors"
"regexp"
"runtime"
"strings"
)
var invalidRe *regexp.Regexp = regexp.MustCompile("[$&!?*;<>{}|`\n\\[\\]\\\\]")
func SupportSystem() bool {
return runtime.GOOS == "linux"
}
func ParamCheck(param string) error {
if len(param) == 0 || invalidRe == nil {
return nil
}
if !strings.HasPrefix(param, "-") {
if !invalidRe.MatchString(param) {
return nil
}
if json.Valid([]byte(param)) {
return nil
}
return errors.New("invalid parameter value: " + param)
}
index := strings.Index(param, "=")
if index == -1 {
if invalidRe.MatchString(param) {
return errors.New("invalid parameter field: " + param)
}
return nil
}
field := param[:index]
if invalidRe.MatchString(field) {
return errors.New("invalid parameter: " + param)
}
if index != len(param)-1 {
value := param[index+1:]
if !invalidRe.MatchString(value) {
return nil
}
if json.Valid([]byte(value)) {
return nil
}
return errors.New("invalid parameter: " + param)
}
return nil
}