Skip to content

Commit 201a276

Browse files
committed
feat: constants should be constants
1 parent c50434a commit 201a276

File tree

5 files changed

+17
-20
lines changed

5 files changed

+17
-20
lines changed

service/constants/constants.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,22 @@ var version string
1212
// -X 'logic.appBaseUrl=http://uni-token.app'
1313
var appBaseUrl string
1414

15-
func GetVersion() int {
16-
if version == "" {
17-
return 0
18-
}
15+
var Version = func() int {
1916
v, err := strconv.Atoi(version)
2017
if err != nil {
2118
return 0
2219
}
2320
return v
24-
}
21+
}()
2522

26-
func GetAppBaseUrl() string {
23+
var AppBaseUrl = func() string {
2724
if appBaseUrl == "" {
2825
return "http://localhost:5173"
2926
}
3027
return appBaseUrl
31-
}
28+
}()
3229

33-
func GetUserName() string {
30+
var UserName = func() string {
3431
if userName := os.Getenv("UNI_TOKEN_SERVICE_USER"); userName != "" {
3532
return userName
3633
}
@@ -39,12 +36,12 @@ func GetUserName() string {
3936
panic(err)
4037
}
4138
return currentUser.Username
42-
}
39+
}()
4340

44-
func ShouldChangeUser() bool {
41+
var ShouldChangeUser = func() bool {
4542
currentUser, err := user.Current()
4643
if err != nil {
4744
panic(err)
4845
}
49-
return currentUser.Username != GetUserName()
50-
}
46+
return currentUser.Username != UserName
47+
}()

service/logic/open.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88

99
"uni-token-service/constants"
10-
"uni-token-service/logic/open_browser"
10+
openBrowser "uni-token-service/logic/open_browser"
1111
"uni-token-service/store"
1212

1313
"github.com/google/uuid"
@@ -25,7 +25,7 @@ func OpenUI(params url.Values, auth bool) error {
2525

2626
var userName string
2727
if len(allUsers) == 0 {
28-
userName = constants.GetUserName()
28+
userName = constants.UserName
2929
} else {
3030
userName = allUsers[0].Username
3131
}
@@ -42,7 +42,7 @@ func OpenUI(params url.Values, auth bool) error {
4242
params.Set("session", sessionId)
4343
params.Set("port", strconv.Itoa(ServerPort))
4444

45-
err := openBrowser.OpenBrowser(constants.GetUserName(), constants.GetAppBaseUrl()+"?"+params.Encode())
45+
err := openBrowser.OpenBrowser(constants.UserName, constants.AppBaseUrl+"?"+params.Encode())
4646
if err != nil {
4747
return err
4848
}

service/logic/open_browser/linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func OpenBrowser(targetUser string, url string) error {
1515
for _, provider := range providers {
1616
if _, err := exec.LookPath(provider); err == nil {
1717
var cmd *exec.Cmd
18-
if constants.ShouldChangeUser() {
18+
if constants.ShouldChangeUser {
1919
cmd = exec.Command("sudo", "-i", "-u", targetUser, provider, url)
2020
} else {
2121
cmd = exec.Command(provider, url)

service/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (p *program) Stop(s service.Service) error {
6969
}
7070

7171
func main() {
72-
username := constants.GetUserName()
72+
username := constants.UserName
7373
serviceName := serviceNamePrefix + "-" + url.PathEscape(username)
7474

7575
svcConfig := &service.Config{
@@ -112,7 +112,7 @@ func main() {
112112
}
113113

114114
if command == "version" {
115-
fmt.Printf("Service Version: %d\n", constants.GetVersion())
115+
fmt.Println(constants.Version)
116116
return
117117
}
118118

@@ -205,7 +205,7 @@ func handleSudo(useInstalled bool, args []string) {
205205
&logic.SudoOptions{
206206
Name: serviceDisplayName,
207207
Env: map[string]string{
208-
"UNI_TOKEN_SERVICE_USER": constants.GetUserName(),
208+
"UNI_TOKEN_SERVICE_USER": constants.UserName,
209209
"UNI_TOKEN_SERVICE_ROOT": discovery.GetServiceRootPath(),
210210
},
211211
},

service/server/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func SetupActionAPI(router *gin.Engine) {
1919
func handleCheck(c *gin.Context) {
2020
c.JSON(http.StatusOK, gin.H{
2121
"__uni_token": true,
22-
"version": constants.GetVersion(),
22+
"version": constants.Version,
2323
})
2424
}
2525

0 commit comments

Comments
 (0)