migrate from com.* to alternatives (#14103)

* remove github.com/unknwon/com from models

* dont use "com.ToStr()"

* replace "com.ToStr" with "fmt.Sprint" where its easy to do

* more refactor

* fix test

* just "proxy" Copy func for now

* as per @lunny
This commit is contained in:
6543 2020-12-25 09:59:32 +00:00 committed by GitHub
parent 04ae0f2f3f
commit a19447aed1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
46 changed files with 230 additions and 220 deletions

View file

@ -26,7 +26,6 @@ import (
"code.gitea.io/gitea/modules/setting"
"github.com/dustin/go-humanize"
"github.com/unknwon/com"
)
// EncodeMD5 encodes string to md5 hex value.
@ -86,8 +85,8 @@ func VerifyTimeLimitCode(data string, minutes int, code string) bool {
// split code
start := code[:12]
lives := code[12:18]
if d, err := com.StrTo(lives).Int(); err == nil {
minutes = d
if d, err := strconv.ParseInt(lives, 10, 0); err == nil {
minutes = int(d)
}
// right active code
@ -131,7 +130,7 @@ func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string
// create sha1 encode string
sh := sha1.New()
_, _ = sh.Write([]byte(data + setting.SecretKey + startStr + endStr + com.ToStr(minutes)))
_, _ = sh.Write([]byte(fmt.Sprintf("%s%s%s%s%d", data, setting.SecretKey, startStr, endStr, minutes)))
encoded := hex.EncodeToString(sh.Sum(nil))
code := fmt.Sprintf("%s%06d%s", startStr, minutes, encoded)
@ -223,7 +222,7 @@ func TruncateString(str string, limit int) string {
func StringsToInt64s(strs []string) ([]int64, error) {
ints := make([]int64, len(strs))
for i := range strs {
n, err := com.StrTo(strs[i]).Int64()
n, err := strconv.ParseInt(strs[i], 10, 64)
if err != nil {
return ints, err
}

View file

@ -17,8 +17,6 @@ import (
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/webhook"
"github.com/unknwon/com"
)
// ToEmail convert models.EmailAddress to api.Email
@ -169,7 +167,7 @@ func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
return &api.PublicKey{
ID: key.ID,
Key: key.Content,
URL: apiLink + com.ToStr(key.ID),
URL: fmt.Sprintf("%s%d", apiLink, key.ID),
Title: key.Name,
Fingerprint: key.Fingerprint,
Created: key.CreatedUnix.AsTime(),
@ -263,7 +261,7 @@ func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
KeyID: key.KeyID,
Key: key.Content,
Fingerprint: key.Fingerprint,
URL: apiLink + com.ToStr(key.ID),
URL: fmt.Sprintf("%s%d", apiLink, key.ID),
Title: key.Name,
Created: key.CreatedUnix.AsTime(),
ReadOnly: key.Mode == models.AccessModeRead, // All deploy keys are read-only.

View file

@ -15,8 +15,6 @@ import (
"strconv"
"strings"
"time"
"github.com/unknwon/com"
)
// GPGSettings represents the default GPG settings for this repository
@ -309,21 +307,24 @@ func parseSize(objects string) *CountObject {
for _, line := range strings.Split(objects, "\n") {
switch {
case strings.HasPrefix(line, statCount):
repoSize.Count = com.StrTo(line[7:]).MustInt64()
repoSize.Count, _ = strconv.ParseInt(line[7:], 10, 64)
case strings.HasPrefix(line, statSize):
repoSize.Size = com.StrTo(line[6:]).MustInt64() * 1024
repoSize.Size, _ = strconv.ParseInt(line[6:], 10, 64)
repoSize.Size *= 1024
case strings.HasPrefix(line, statInpack):
repoSize.InPack = com.StrTo(line[9:]).MustInt64()
repoSize.InPack, _ = strconv.ParseInt(line[9:], 10, 64)
case strings.HasPrefix(line, statPacks):
repoSize.Packs = com.StrTo(line[7:]).MustInt64()
repoSize.Packs, _ = strconv.ParseInt(line[7:], 10, 64)
case strings.HasPrefix(line, statSizePack):
repoSize.SizePack = com.StrTo(line[11:]).MustInt64() * 1024
repoSize.Count, _ = strconv.ParseInt(line[11:], 10, 64)
repoSize.Count *= 1024
case strings.HasPrefix(line, statPrunePackage):
repoSize.PrunePack = com.StrTo(line[16:]).MustInt64()
repoSize.PrunePack, _ = strconv.ParseInt(line[16:], 10, 64)
case strings.HasPrefix(line, statGarbage):
repoSize.Garbage = com.StrTo(line[9:]).MustInt64()
repoSize.Garbage, _ = strconv.ParseInt(line[9:], 10, 64)
case strings.HasPrefix(line, statSizeGarbage):
repoSize.SizeGarbage = com.StrTo(line[14:]).MustInt64() * 1024
repoSize.SizeGarbage, _ = strconv.ParseInt(line[14:], 10, 64)
repoSize.SizeGarbage *= 1024
}
}
return repoSize

View file

@ -25,7 +25,6 @@ import (
"code.gitea.io/gitea/modules/util"
"github.com/gliderlabs/ssh"
"github.com/unknwon/com"
gossh "golang.org/x/crypto/ssh"
)
@ -58,13 +57,13 @@ func getExitStatusFromError(err error) int {
}
func sessionHandler(session ssh.Session) {
keyID := session.Context().Value(giteaKeyID).(int64)
keyID := fmt.Sprintf("%d", session.Context().Value(giteaKeyID).(int64))
command := session.RawCommand()
log.Trace("SSH: Payload: %v", command)
args := []string{"serv", "key-" + com.ToStr(keyID), "--config=" + setting.CustomConf}
args := []string{"serv", "key-" + keyID, "--config=" + setting.CustomConf}
log.Trace("SSH: Arguments: %v", args)
cmd := exec.Command(setting.AppPath, args...)
cmd.Env = append(

View file

@ -4,7 +4,10 @@
package util
import "sort"
import (
"sort"
"strings"
)
// Int64Slice attaches the methods of Interface to []int64, sorting in increasing order.
type Int64Slice []int64
@ -36,10 +39,22 @@ func ExistsInSlice(target string, slice []string) bool {
}
// IsStringInSlice sequential searches if string exists in slice.
func IsStringInSlice(target string, slice []string) bool {
func IsStringInSlice(target string, slice []string, insensitive ...bool) bool {
caseInsensitive := false
if len(insensitive) != 0 && insensitive[0] {
caseInsensitive = true
target = strings.ToLower(target)
}
for i := 0; i < len(slice); i++ {
if slice[i] == target {
return true
if caseInsensitive {
if strings.ToLower(slice[i]) == target {
return true
}
} else {
if slice[i] == target {
return true
}
}
}
return false

20
modules/util/copy.go Normal file
View file

@ -0,0 +1,20 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package util
import (
"github.com/unknwon/com"
)
// CopyFile copies file from source to target path.
func CopyFile(src, dest string) error {
return com.Copy(src, dest)
}
// CopyDir copy files recursively from source to target directory.
// It returns error when error occurs in underlying functions.
func CopyDir(srcPath, destPath string) error {
return com.CopyDir(srcPath, destPath)
}