mirror of
https://github.com/alibaba/higress.git
synced 2026-05-29 23:27:28 +08:00
feat: implement hgctl agent module (#3267)
This commit is contained in:
79
hgctl/pkg/util/env.go
Normal file
79
hgctl/pkg/util/env.go
Normal file
@@ -0,0 +1,79 @@
|
||||
// Copyright (c) 2025 Alibaba Group Holding Ltd.
|
||||
//
|
||||
// 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 util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetPythonVersion() (string, error) {
|
||||
re := regexp.MustCompile(`\d+\.\d+(\.\d+)?`)
|
||||
|
||||
for _, cmd := range []string{"python3", "python"} {
|
||||
out, err := exec.Command(cmd, "--version").CombinedOutput()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
version := strings.TrimSpace(string(out))
|
||||
match := re.FindString(version)
|
||||
if match != "" {
|
||||
return match, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("python not found")
|
||||
}
|
||||
|
||||
// compareVersions compares two version strings like "3.11.2" and "3.10".
|
||||
// Returns:
|
||||
//
|
||||
// 1 if v1 > v2
|
||||
// 0 if v1 == v2
|
||||
// -1 if v1 < v2
|
||||
func CompareVersions(v1, v2 string) int {
|
||||
// Extract numeric parts only (e.g. "3.12.0b1" → "3.12.0")
|
||||
re := regexp.MustCompile(`\d+`)
|
||||
nums1 := re.FindAllString(v1, -1)
|
||||
nums2 := re.FindAllString(v2, -1)
|
||||
|
||||
maxLen := len(nums1)
|
||||
if len(nums2) > maxLen {
|
||||
maxLen = len(nums2)
|
||||
}
|
||||
|
||||
// Compare each part
|
||||
for i := 0; i < maxLen; i++ {
|
||||
var n1, n2 int
|
||||
if i < len(nums1) {
|
||||
n1, _ = strconv.Atoi(nums1[i])
|
||||
}
|
||||
if i < len(nums2) {
|
||||
n2, _ = strconv.Atoi(nums2[i])
|
||||
}
|
||||
|
||||
if n1 > n2 {
|
||||
return 1
|
||||
} else if n1 < n2 {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
@@ -16,9 +16,11 @@ package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -93,3 +95,33 @@ func WriteFileString(fileName string, content string, perm os.FileMode) error {
|
||||
writer.Flush()
|
||||
return nil
|
||||
}
|
||||
|
||||
// This function return ~/.hgctl file_path string (Currently Linux only)
|
||||
func GetHomeHgctlDir() string {
|
||||
homeDir, _ := os.UserHomeDir()
|
||||
targetDir := filepath.Join(homeDir, ".hgctl")
|
||||
return targetDir
|
||||
}
|
||||
|
||||
func GetSpecificAgentDir(name string) (string, error) {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get user home directory: %w", err)
|
||||
}
|
||||
|
||||
targetDir := filepath.Join(homeDir, ".hgctl", "agents", name)
|
||||
|
||||
info, err := os.Stat(targetDir)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return "", fmt.Errorf("agent %q does not exist", name)
|
||||
}
|
||||
return "", fmt.Errorf("failed to stat agent directory %q: %w", targetDir, err)
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
return "", fmt.Errorf("agent %q exists but is not a directory", name)
|
||||
}
|
||||
|
||||
return targetDir, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user