Skip to content

Commit

Permalink
refactor(entrypoint): 重构 code-server 配置文件生成逻辑
Browse files Browse the repository at this point in the history
- 移除了对 filepath 包的依赖
- 简化了配置文件生成流程
- 使用 goutils.WriteText 函数替代手动创建目录和写入文件
- 优化了错误处理方式

Signed-off-by: 117503445 <[email protected]>
  • Loading branch information
117503445 committed Feb 3, 2025
1 parent ab6af09 commit 3561939
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
26 changes: 9 additions & 17 deletions entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"

_ "embed"

Expand All @@ -21,22 +20,15 @@ func main() {
goutils.ExecOpt.DumpOutput = true

codeServerConfigPath := "/root/.config/code-server/config.yaml"
if !goutils.FileExists(codeServerConfigPath) {
codeServerPassword := os.Getenv("CODE_SERVER_PASSWORD")
if codeServerPassword == "" {
log.Warn().Msg("CODE_SERVER_PASSWORD is not set, use default password")
codeServerPassword = "123456"
}

codeServerConfigText := fmt.Sprintf(codeServerConfigTemplate, codeServerPassword)

if err := os.MkdirAll(filepath.Dir(codeServerConfigPath), 0755); err != nil {
log.Error().Err(err).Msg("Failed to create code-server config directory")
} else {
if err := os.WriteFile(codeServerConfigPath, []byte(codeServerConfigText), 0644); err != nil {
log.Error().Err(err).Msg("Failed to write code-server config file")
}
}
codeServerPassword := os.Getenv("CODE_SERVER_PASSWORD")
if codeServerPassword == "" {
log.Warn().Msg("CODE_SERVER_PASSWORD is not set, use default password")
codeServerPassword = "123456"
}
codeServerConfigText := fmt.Sprintf(codeServerConfigTemplate, codeServerPassword)
err := goutils.WriteText(codeServerConfigPath, codeServerConfigText)
if err != nil {
log.Fatal().Err(err).Msg("Failed to write code-server config file")
}

go func() {
Expand Down
4 changes: 3 additions & 1 deletion note.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ docker run -it --rm archlinux bash
https://geo.mirror.pkgbuild.com/$repo/os/$arch

https://mirrors.ustc.edu.cn/archlinux/core/os/x86_64/gcc-go-14.2.1%2Br134%2Bgab884fffe3fc-2-x86_64.pkg.tar.zst
https://geo.mirror.pkgbuild.com/core/os/x86_64/gcc-go-14.2.1%2Br134%2Bgab884fffe3fc-2-x86_64.pkg.tar.zst
https://geo.mirror.pkgbuild.com/core/os/x86_64/gcc-go-14.2.1%2Br134%2Bgab884fffe3fc-2-x86_64.pkg.tar.zst

docker run -it -p 4445:4444 --env CODE_SERVER_PASSWORD=123 --rm 117503445/dev-base

0 comments on commit 3561939

Please sign in to comment.