func (s *SealosInstaller) appendAPIServer() error {标签:APIServer,读取,err,IO,write,etcHostPath,file,go,os From: https://www.cnblogs.com/cheyunhua/p/17389494.html
etcHostPath := "/etc/hosts"
etcHostMap := fmt.Sprintf("%s %s", IPFormat(s.Masters[0]), APIServer)
file, err := os.OpenFile(etcHostPath, os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
os.Exit(1)
}
defer file.Close()
reader := bufio.NewReader(file)
for {
str, err := reader.ReadString('\n')
if strings.Contains(str, APIServer) {
logger.Info("local %s is already exists %s", etcHostPath, APIServer)
return nil
}
if err == io.EOF {
break
}
}
write := bufio.NewWriter(file)
_, _ = write.WriteString(etcHostMap)
return write.Flush()
}