Browse Source

refactor: remove workaround after the golang 1.7 release. (#1349)

* refactor: remove workaround after the golang 1.7 release.

* remove unused import.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* refactor: Add remove function.

* refactor: rename

* refactor: update build flag
Bo-Yi Wu 3 years ago
parent
commit
08f7fded3c
3 changed files with 34 additions and 19 deletions
  1. 4 19
      models/admin.go
  2. 14 0
      modules/util/remove.go
  3. 16 0
      modules/util/remove_windows.go

+ 4 - 19
models/admin.go

@@ -6,16 +6,13 @@ package models
6 6
 
7 7
 import (
8 8
 	"fmt"
9
-	"os"
10
-	"os/exec"
11
-	"strings"
12 9
 	"time"
13 10
 
11
+	"code.gitea.io/gitea/modules/log"
12
+	"code.gitea.io/gitea/modules/util"
13
+
14 14
 	"github.com/Unknwon/com"
15 15
 	"github.com/go-xorm/xorm"
16
-
17
-	"code.gitea.io/gitea/modules/log"
18
-	"code.gitea.io/gitea/modules/setting"
19 16
 )
20 17
 
21 18
 //NoticeType describes the notice type
@@ -79,19 +76,7 @@ func RemoveAllWithNotice(title, path string) {
79 76
 }
80 77
 
81 78
 func removeAllWithNotice(e Engine, title, path string) {
82
-	var err error
83
-	// workaround for Go not being able to remove read-only files/folders: https://github.com/golang/go/issues/9606
84
-	// this bug should be fixed on Go 1.7, so the workaround should be removed when Gogs don't support Go 1.6 anymore:
85
-	// https://github.com/golang/go/commit/2ffb3e5d905b5622204d199128dec06cefd57790
86
-	if setting.IsWindows {
87
-		// converting "/" to "\" in path on Windows
88
-		path = strings.Replace(path, "/", "\\", -1)
89
-		err = exec.Command("cmd", "/C", "rmdir", "/S", "/Q", path).Run()
90
-	} else {
91
-		err = os.RemoveAll(path)
92
-	}
93
-
94
-	if err != nil {
79
+	if err := util.RemoveAll(path); err != nil {
95 80
 		desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
96 81
 		log.Warn(desc)
97 82
 		if err = createNotice(e, NoticeRepository, desc); err != nil {

+ 14 - 0
modules/util/remove.go

@@ -0,0 +1,14 @@
1
+// +build !windows,!go1.7 go1.7
2
+
3
+// Copyright 2017 The Gitea Authors. All rights reserved.
4
+// Use of this source code is governed by a MIT-style
5
+// license that can be found in the LICENSE file.
6
+
7
+package util
8
+
9
+import "os"
10
+
11
+// RemoveAll files from Go version 1.7 onward
12
+func RemoveAll(path string) error {
13
+	return os.RemoveAll(path)
14
+}

+ 16 - 0
modules/util/remove_windows.go

@@ -0,0 +1,16 @@
1
+// +build windows,!go1.7
2
+
3
+// Copyright 2017 The Gitea Authors. All rights reserved.
4
+// Use of this source code is governed by a MIT-style
5
+// license that can be found in the LICENSE file.
6
+
7
+package util
8
+
9
+// RemoveAll files from path on windows
10
+// workaround for Go not being able to remove read-only files/folders: https://github.com/golang/go/issues/9606
11
+// this bug should be fixed on Go 1.7, so the workaround should be removed when Gitea don't support Go 1.6 anymore:
12
+// https://github.com/golang/go/commit/2ffb3e5d905b5622204d199128dec06cefd57790
13
+func RemoveAll(path string) error {
14
+	path = strings.Replace(path, "/", "\\", -1)
15
+	return exec.Command("cmd", "/C", "rmdir", "/S", "/Q", path).Run()
16
+}