Skip to content

Commit 7c94db0

Browse files
committed
chore: use flags instead of env vars
1 parent 1d8603d commit 7c94db0

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ data
2424

2525
# build folder
2626
build
27+
memos-build
2728

2829
.DS_Store

bin/server/cmd/root.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ func Execute() {
4949
profile: profile,
5050
}
5151

52+
println("---")
53+
println("profile")
54+
println("mode:", profile.Mode)
55+
println("port:", profile.Port)
56+
println("dsn:", profile.DSN)
57+
println("version:", profile.Version)
58+
println("---")
5259
println(greetingBanner)
5360
fmt.Printf("Version %s has started at :%d\n", profile.Version, profile.Port)
5461

scripts/build.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Usage: sh ./scripts/build.sh
2+
set -e
3+
4+
cd "$(dirname "$0")/../"
5+
6+
echo "Start building..."
7+
8+
go build -o ./memos-build/memos ./bin/server/main.go
9+
10+
echo "Build finished"

server/profile/profile.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package profile
22

33
import (
4+
"flag"
45
"fmt"
56
"os"
67
"path/filepath"
7-
"strconv"
88
"strings"
99

1010
"github.com/usememos/memos/common"
@@ -16,6 +16,8 @@ type Profile struct {
1616
Mode string `json:"mode"`
1717
// Port is the binding port for server
1818
Port int `json:"port"`
19+
// Data is the data directory
20+
Data string `json:"data"`
1921
// DSN points to where Memos stores its own data
2022
DSN string `json:"dsn"`
2123
// Version is the current version of server
@@ -43,35 +45,30 @@ func checkDSN(dataDir string) (string, error) {
4345
return dataDir, nil
4446
}
4547

46-
// GetDevProfile will return a profile for dev.
48+
// GetDevProfile will return a profile for dev or prod.
4749
func GetProfile() *Profile {
48-
mode := os.Getenv("mode")
49-
if mode != "dev" && mode != "prod" {
50-
mode = "dev"
51-
}
50+
profile := Profile{}
51+
flag.StringVar(&profile.Mode, "mode", "dev", "mode of server")
52+
flag.IntVar(&profile.Port, "port", 8080, "port of server")
53+
flag.StringVar(&profile.Data, "data", "", "data directory")
54+
flag.Parse()
5255

53-
port, err := strconv.Atoi(os.Getenv("port"))
54-
if err != nil {
55-
port = 8080
56+
if profile.Mode != "dev" && profile.Mode != "prod" {
57+
profile.Mode = "dev"
5658
}
5759

58-
data := ""
59-
if mode == "prod" {
60-
data = "/var/opt/memos"
60+
if profile.Mode == "prod" && profile.Data == "" {
61+
profile.Data = "/var/opt/memos"
6162
}
6263

63-
dataDir, err := checkDSN(data)
64+
dataDir, err := checkDSN(profile.Data)
6465
if err != nil {
6566
fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err)
6667
os.Exit(1)
6768
}
6869

69-
dsn := fmt.Sprintf("%s/memos_%s.db", dataDir, mode)
70+
profile.DSN = fmt.Sprintf("%s/memos_%s.db", dataDir, profile.Mode)
71+
profile.Version = common.GetCurrentVersion(profile.Mode)
7072

71-
return &Profile{
72-
Mode: mode,
73-
Port: port,
74-
DSN: dsn,
75-
Version: common.GetCurrentVersion(mode),
76-
}
73+
return &profile
7774
}

0 commit comments

Comments
 (0)