mirror of
https://github.com/sipeed/NanoKVM.git
synced 2026-09-11 00:22:56 -05:00
service: hostname: update /etc/hosts
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
const (
|
||||
BootHostnameFile = "/boot/hostname"
|
||||
EtcHostname = "/etc/hostname"
|
||||
EtcHosts = "/etc/hosts"
|
||||
)
|
||||
|
||||
func (s *Service) SetHostname(c *gin.Context) {
|
||||
@@ -25,6 +26,29 @@ func (s *Service) SetHostname(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
dataRead, err := os.ReadFile(EtcHostname)
|
||||
if err != nil {
|
||||
rsp.ErrRsp(c, -1, "read Hostname failed")
|
||||
return
|
||||
}
|
||||
|
||||
oldHostname := strings.Replace(string(dataRead), "\n", "", -1)
|
||||
|
||||
if (oldHostname != req.Hostname) {
|
||||
dataRead, err = os.ReadFile(EtcHosts)
|
||||
if err != nil {
|
||||
rsp.ErrRsp(c, -1, "read Hosts failed")
|
||||
return
|
||||
}
|
||||
|
||||
data := []byte(strings.Replace(string(dataRead), oldHostname, req.Hostname, -1))
|
||||
|
||||
if err := os.WriteFile(EtcHosts, data, 0o644); err != nil {
|
||||
rsp.ErrRsp(c, -2, "failed to write data")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
data := []byte(fmt.Sprintf("%s", req.Hostname))
|
||||
|
||||
if err := os.WriteFile(BootHostnameFile, data, 0o644); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user