Skip to content

Commit a9bfd72

Browse files
committed
refactor: refactor system configuration detection
- Add system analysis to configuration initialization - Simplify distribution detection to use runtime architecture instead of `lsb_release` command - Improve username detection by checking both USER and USERNAME environment variables - Replace direct environment variable access with system configuration methods in command executor Signed-off-by: wecoding <wecoding@yeah.net>
1 parent c138c05 commit a9bfd72

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

internal/options/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ func EnsureConfig() (Config, error) {
376376
return c, err
377377
}
378378

379+
c.System = system.Analyse()
380+
379381
return c, nil
380382
}
381383

internal/system/system.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,7 @@ func GetOperatingSystem() OperatingSystem {
8686
}
8787

8888
func GetDistribution() string {
89-
dist, err := runner.Run("lsb_release", "-sd")
90-
if err != nil {
91-
return ""
92-
}
93-
94-
return strings.Trim(strings.TrimSpace(dist), "\"")
89+
return runtime.GOARCH
9590
}
9691

9792
func GetShell() string {
@@ -122,12 +117,14 @@ func GetHomeDirectory() string {
122117
}
123118

124119
func GetUsername() string {
125-
name, err := runner.Run("echo", os.Getenv("USER"))
126-
if err != nil {
127-
return ""
120+
name := strings.TrimSpace(os.Getenv("USER"))
121+
122+
if name == "" {
123+
name = strings.TrimSpace(os.Getenv("USERNAME"))
128124
}
129-
name = strings.TrimSpace(name)
125+
130126
if name == "" {
127+
var err error
131128
name, err = runner.Run("whoami")
132129
if err != nil {
133130
return ""

internal/ui/coders/commands.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"io/fs"
1111
"os"
1212
"path/filepath"
13-
"runtime"
1413
"strings"
1514

1615
"github.com/coding-hui/common/util/fileutil"
@@ -556,14 +555,10 @@ func (c *CommandExecutor) exec(_ context.Context, input string) error {
556555
}
557556

558557
// Get current user and OS info
559-
currentUser := os.Getenv("USER")
560-
if currentUser == "" {
561-
currentUser = os.Getenv("USERNAME")
562-
}
563-
564-
osInfo := runtime.GOOS
565-
archInfo := runtime.GOARCH
566-
558+
currentUser := c.coder.cfg.System.GetUsername()
559+
osInfo := c.coder.cfg.System.GetOperatingSystem()
560+
archInfo := c.coder.cfg.System.GetDistribution()
561+
567562
system := llms.SystemChatMessage{Content: strings.Join([]string{
568563
"You are a helpful terminal assistant.",
569564
"Convert the user's request into a single-line POSIX shell command.",

0 commit comments

Comments
 (0)