From 888ef8556d4424204cfb54a4c8ad899b08388d22 Mon Sep 17 00:00:00 2001 From: Arjan H Date: Mon, 8 Aug 2022 20:01:18 +0200 Subject: [PATCH] Refactor some deprecated code --- gui/certificate.go | 11 +++++------ gui/dashboard.go | 2 +- gui/main.go | 22 +++++++++------------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/gui/certificate.go b/gui/certificate.go index c73ca9b..f5a1937 100644 --- a/gui/certificate.go +++ b/gui/certificate.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "mime/multipart" "os" "os/exec" @@ -124,12 +123,12 @@ func preCreateTasks(path string) error { } if _, err := os.Stat(path + "serial"); os.IsNotExist(err) { - if err := ioutil.WriteFile(path+"serial", []byte("1000\n"), 0644); err != nil { + if err := os.WriteFile(path+"serial", []byte("1000\n"), 0644); err != nil { return err } } if _, err := os.Stat(path + "crlnumber"); os.IsNotExist(err) { - if err = ioutil.WriteFile(path+"crlnumber", []byte("1000\n"), 0644); err != nil { + if err = os.WriteFile(path+"crlnumber", []byte("1000\n"), 0644); err != nil { return err } } @@ -301,7 +300,7 @@ func (ci *CertificateInfo) Import(path string, certBase string, tmpDir string, t // Upload a certificate and key file func (ci *CertificateInfo) Upload(path string, certBase string, tmpKey string, tmpCert string) error { - if err := ioutil.WriteFile(tmpKey, []byte(ci.Key), 0644); err != nil { + if err := os.WriteFile(tmpKey, []byte(ci.Key), 0644); err != nil { return err } @@ -322,7 +321,7 @@ func (ci *CertificateInfo) Upload(path string, certBase string, tmpKey string, t return reportError(err) } - if err := ioutil.WriteFile(tmpCert, []byte(ci.Certificate), 0644); err != nil { + if err := os.WriteFile(tmpCert, []byte(ci.Certificate), 0644); err != nil { return err } @@ -494,7 +493,7 @@ func (ci *CertificateInfo) Create(path string, certBase string) error { return err } - tmpDir, err := ioutil.TempDir("", "labca") + tmpDir, err := os.MkdirTemp("", "labca") if err != nil { return err } diff --git a/gui/dashboard.go b/gui/dashboard.go index 7132acf..8092106 100644 --- a/gui/dashboard.go +++ b/gui/dashboard.go @@ -45,7 +45,7 @@ func _parseLine(line string, loc *time.Location) Activity { line = _removeAnsiColors(line) - re := regexp.MustCompile("^.*\\|\\s*(\\S)(\\S+) (\\S+) (\\S+) (.*)$") + re := regexp.MustCompile(`^.*\|\s*(\S)(\S+) (\S+) (\S+) (.*)$`) result := re.FindStringSubmatch(line) if len(result) == 0 { return activity diff --git a/gui/main.go b/gui/main.go index 43cf1b2..6387cf5 100644 --- a/gui/main.go +++ b/gui/main.go @@ -16,7 +16,6 @@ import ( "fmt" "html/template" "io" - "io/ioutil" "log" "math" "math/big" @@ -1250,7 +1249,7 @@ func getLog(w http.ResponseWriter, r *http.Request, logType string) string { fmt.Fprintf(conn, "log-"+logType+"\n") reader := bufio.NewReader(conn) - contents, err := ioutil.ReadAll(reader) + contents, err := io.ReadAll(reader) if err != nil { errorHandler(w, r, err, http.StatusInternalServerError) return "" @@ -1321,14 +1320,11 @@ func writer(ws *websocket.Conn, logType string) { go showLog(ws, logType) - for { - select { - case <-pingTicker.C: - ws.SetWriteDeadline(time.Now().Add(writeWait)) - if err := ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil { - // Probably "websocket: close sent" - return - } + for range pingTicker.C { + ws.SetWriteDeadline(time.Now().Add(writeWait)) + if err := ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil { + // Probably "websocket: close sent" + return } } } @@ -1396,7 +1392,7 @@ func _buildCI(r *http.Request, session *sessions.Session, isRoot bool) *Certific } func issuerNameID(certfile string) (int64, error) { - cf, err := ioutil.ReadFile(certfile) + cf, err := os.ReadFile(certfile) if err != nil { log.Printf("issuerNameID: could not read cert file: %v", err) return 0, err @@ -1534,7 +1530,7 @@ func _hostCommand(w http.ResponseWriter, r *http.Request, command string, params } reader := bufio.NewReader(conn) - message, err := ioutil.ReadAll(reader) + message, err := io.ReadAll(reader) if err != nil { errorHandler(w, r, err, http.StatusInternalServerError) return false @@ -1762,7 +1758,7 @@ func _setupAdminUser(w http.ResponseWriter, r *http.Request) bool { fmt.Fprintf(conn, "backup-restore\n"+header.Filename+"\n") reader := bufio.NewReader(conn) - message, err := ioutil.ReadAll(reader) + message, err := io.ReadAll(reader) if err != nil { fmt.Println(err) reg.Errors["File"] = "Could not import backup file: error reading control response"