feat: move random string to separate package

This commit is contained in:
Thomas Boerger 2024-04-12 10:00:44 +02:00
parent f353de742d
commit ca684a7b05
No known key found for this signature in database
GPG Key ID: F630596501026DB5
6 changed files with 48 additions and 39 deletions

View File

@ -3,7 +3,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/ansible-semaphore/semaphore/lib"
"os" "os"
"time" "time"
@ -11,6 +10,7 @@ import (
"github.com/ansible-semaphore/semaphore/db/bolt" "github.com/ansible-semaphore/semaphore/db/bolt"
"github.com/ansible-semaphore/semaphore/db/factory" "github.com/ansible-semaphore/semaphore/db/factory"
"github.com/ansible-semaphore/semaphore/db/sql" "github.com/ansible-semaphore/semaphore/db/sql"
"github.com/ansible-semaphore/semaphore/pkg/random"
"github.com/ansible-semaphore/semaphore/util" "github.com/ansible-semaphore/semaphore/util"
"github.com/go-gorp/gorp/v3" "github.com/go-gorp/gorp/v3"
"github.com/snikch/goodman/transaction" "github.com/snikch/goodman/transaction"
@ -297,7 +297,7 @@ func getUUID() string {
if !randSetup { if !randSetup {
randSetup = true randSetup = true
} }
return lib.RandomString(8) return random.String(8)
} }
func loadConfig() { func loadConfig() {

View File

@ -7,7 +7,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/ansible-semaphore/semaphore/lib"
"math/rand" "math/rand"
"net/http" "net/http"
"net/url" "net/url"
@ -17,17 +16,16 @@ import (
"text/template" "text/template"
"time" "time"
"golang.org/x/crypto/bcrypt"
"golang.org/x/oauth2"
"github.com/ansible-semaphore/semaphore/api/helpers" "github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db" "github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/pkg/random"
"github.com/ansible-semaphore/semaphore/util"
"github.com/coreos/go-oidc/v3/oidc" "github.com/coreos/go-oidc/v3/oidc"
"github.com/go-ldap/ldap/v3" "github.com/go-ldap/ldap/v3"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/ansible-semaphore/semaphore/util"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"golang.org/x/crypto/bcrypt"
"golang.org/x/oauth2"
) )
func tryFindLDAPUser(username, password string) (*db.User, error) { func tryFindLDAPUser(username, password string) (*db.User, error) {
@ -533,7 +531,7 @@ func claimOidcToken(idToken *oidc.IDToken, provider util.OidcProvider) (res oidc
} }
func getRandomUsername() string { func getRandomUsername() string {
return lib.RandomString(16) return random.String(16)
} }
func getRandomProfileName() string { func getRandomProfileName() string {

View File

@ -1,13 +1,14 @@
package projects package projects
import ( import (
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/lib"
"github.com/ansible-semaphore/semaphore/util"
"github.com/gorilla/context"
"net/http" "net/http"
"strings" "strings"
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/pkg/random"
"github.com/ansible-semaphore/semaphore/util"
"github.com/gorilla/context"
) )
type publicAlias struct { type publicAlias struct {
@ -70,7 +71,7 @@ func AddIntegrationAlias(w http.ResponseWriter, r *http.Request) {
} }
alias, err := helpers.Store(r).CreateIntegrationAlias(db.IntegrationAlias{ alias, err := helpers.Store(r).CreateIntegrationAlias(db.IntegrationAlias{
Alias: lib.RandomString(16), Alias: random.String(16),
ProjectID: project.ID, ProjectID: project.ID,
IntegrationID: integrationId, IntegrationID: integrationId,
}) })

View File

@ -1,22 +0,0 @@
package lib
import (
"math/rand"
"time"
)
var r *rand.Rand
func RandomString(strlen int) string {
if r == nil {
r = rand.New(rand.NewSource(time.Now().UnixNano()))
}
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
result := ""
for i := 0; i < strlen; i++ {
index := r.Intn(len(chars))
result += chars[index : index+1]
}
return result
}

31
pkg/random/string.go Normal file
View File

@ -0,0 +1,31 @@
package random
import (
"math/rand"
"time"
)
var (
r *rand.Rand
)
const (
chars = "abcdefghijklmnopqrstuvwxyz0123456789"
)
func String(strlen int) string {
if r == nil {
r = rand.New(rand.NewSource(
time.Now().UnixNano(),
))
}
result := ""
for i := 0; i < strlen; i++ {
index := r.Intn(len(chars))
result += chars[index : index+1]
}
return result
}

View File

@ -2,8 +2,9 @@ package project
import ( import (
"fmt" "fmt"
"github.com/ansible-semaphore/semaphore/db" "github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/lib" "github.com/ansible-semaphore/semaphore/pkg/random"
) )
func findNameByID[T db.BackupEntity](ID int, items []T) (*string, error) { func findNameByID[T db.BackupEntity](ID int, items []T) (*string, error) {
@ -47,7 +48,7 @@ func getScheduleByTemplate(templateID int, schedules []db.Schedule) *string {
} }
func getRandomName(name string) string { func getRandomName(name string) string {
return name + " - " + lib.RandomString(10) return name + " - " + random.String(10)
} }
func makeUniqueNames[T any](items []T, getter func(item *T) string, setter func(item *T, name string)) { func makeUniqueNames[T any](items []T, getter func(item *T) string, setter func(item *T, name string)) {