forked from soulteary/flare
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Light
authored and
Light
committed
Jan 6, 2024
1 parent
accbf9d
commit 61771b5
Showing
80 changed files
with
8,815 additions
and
8,088 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/soulteary/flare/internal/version" | ||
flags "github.com/spf13/pflag" | ||
|
||
FlareDefine "github.com/soulteary/flare/config/define" | ||
FlareModel "github.com/soulteary/flare/config/model" | ||
) | ||
|
||
func ParseCLI(baseFlags FlareModel.Flags) FlareModel.Flags { | ||
|
||
var cliFlags = new(FlareModel.Flags) | ||
options := flags.NewFlagSet("appFlags", flags.ContinueOnError) | ||
options.SortFlags = false | ||
|
||
// port | ||
options.IntVarP(&cliFlags.Port, _KEY_PORT, _KEY_PORT_SHORT, FlareDefine.DEFAULT_PORT, "指定监听端口") | ||
// guide | ||
options.BoolVarP(&cliFlags.EnableGuide, _KEY_ENABLE_GUIDE, _KEY_ENABLE_GUIDE_SHORT, FlareDefine.DEFAULT_ENABLE_GUIDE, "启用应用向导") | ||
// visibility | ||
options.StringVarP(&cliFlags.Visibility, _KEY_VISIBILITY, _KEY_VISIBILITY_SHORT, FlareDefine.DEFAULT_VISIBILITY, "调整网站整体可见性") | ||
// mini_request | ||
options.BoolVarP(&cliFlags.EnableMinimumRequest, _KEY_MINI_REQUEST, _KEY_MINI_REQUEST_SHORT, FlareDefine.DEFAULT_ENABLE_MINI_REQUEST, "使用请求最小化模式") | ||
options.BoolVar(&cliFlags.EnableMinimumRequest, _KEY_MINI_REQUEST_OLD, FlareDefine.DEFAULT_ENABLE_MINI_REQUEST, "使用请求最小化模式") | ||
_ = options.MarkDeprecated(_KEY_MINI_REQUEST_OLD, "please use --"+_KEY_MINI_REQUEST+" instead") | ||
// offline | ||
options.BoolVarP(&cliFlags.EnableOfflineMode, _KEY_ENABLE_OFFLINE, _KEY_ENABLE_OFFLINE_SHORT, FlareDefine.DEFAULT_ENABLE_OFFLINE, "启用离线模式") | ||
// disable_login | ||
options.BoolVarP(&cliFlags.DisableLoginMode, _KEY_DISABLE_LOGIN, _KEY_DISABLE_LOGIN_SHORT, FlareDefine.DEFAULT_DISABLE_LOGIN, "禁用账号登陆") | ||
options.BoolVar(&cliFlags.DisableLoginMode, _KEY_DISABLE_LOGIN_OLD, FlareDefine.DEFAULT_DISABLE_LOGIN, "禁用账号登陆") | ||
_ = options.MarkDeprecated(_KEY_DISABLE_LOGIN_OLD, "please use --"+_KEY_DISABLE_LOGIN+" instead") | ||
// 启用废弃日志警告 | ||
options.BoolVarP(&cliFlags.EnableDeprecatedNotice, _KEY_ENABLE_DEPRECATED_NOTICE, _KEY_ENABLE_DEPRECATED_NOTICE_SHORT, FlareDefine.DEFAULT_ENABLE_DEPRECATED_NOTICE, "启用废弃日志警告") | ||
options.BoolVarP(&cliFlags.EnableEditor, _KEY_ENABLE_EDITOR, _KEY_ENABLE_EDITOR_SHORT, FlareDefine.DEFAULT_ENABLE_EDITOR, "启用编辑器") | ||
// 禁用 CSP | ||
options.BoolVarP(&cliFlags.DisableCSP, _KEY_DISABLE_CSP, _KEY_DISABLE_CSP_SHORT, FlareDefine.DEFAULT_DISABLE_CSP, "禁用CSP") | ||
// 输出日志级别 | ||
options.StringVar(&cliFlags.LogLevel, _KEY_LOG_LEVEL, FlareDefine.DEFAULT_LOG_LEVEL, "输出日志级别") | ||
// 其他 | ||
options.BoolVarP(&cliFlags.ShowVersion, "version", "v", false, "显示应用版本号") | ||
options.BoolVarP(&cliFlags.ShowHelp, "help", "h", false, "显示帮助") | ||
|
||
_ = options.Parse(os.Args) | ||
|
||
exit := ExcuteCLI(cliFlags, options) | ||
if exit { | ||
os.Exit(0) | ||
} | ||
GetVersion(true) | ||
|
||
// 用于判断参数是否存在 | ||
keys := make(map[string]bool) | ||
trimValue := regexp.MustCompile(`=.*`) | ||
for _, key := range os.Args[1:] { | ||
if key[:2] == "--" { | ||
keys[trimValue.ReplaceAllString(key[2:], "")] = true | ||
} else if key[:1] == "-" { | ||
keys[trimValue.ReplaceAllString(key[1:], "")] = true | ||
} | ||
} | ||
|
||
if keys[_KEY_PORT] || keys[_KEY_PORT_SHORT] { | ||
baseFlags.Port = cliFlags.Port | ||
} | ||
|
||
if keys[_KEY_MINI_REQUEST] || keys[_KEY_MINI_REQUEST_SHORT] || keys[_KEY_MINI_REQUEST_OLD] { | ||
baseFlags.EnableMinimumRequest = cliFlags.EnableMinimumRequest | ||
} | ||
|
||
if keys[_KEY_DISABLE_LOGIN] || keys[_KEY_DISABLE_LOGIN_SHORT] || keys[_KEY_DISABLE_LOGIN_OLD] { | ||
baseFlags.DisableLoginMode = cliFlags.DisableLoginMode | ||
} | ||
|
||
if keys[_KEY_DISABLE_CSP] || keys[_KEY_DISABLE_CSP_SHORT] { | ||
baseFlags.DisableCSP = cliFlags.DisableCSP | ||
} | ||
|
||
if keys[_KEY_VISIBILITY] || keys[_KEY_VISIBILITY_SHORT] { | ||
baseFlags.Visibility = cliFlags.Visibility | ||
// 判断是否为白名单中的词,以及强制转换内容为大写 | ||
if strings.ToUpper(cliFlags.Visibility) != FlareDefine.DEFAULT_VISIBILITY && | ||
strings.ToUpper(cliFlags.Visibility) != "PRIVATE" { | ||
baseFlags.Visibility = FlareDefine.DEFAULT_VISIBILITY | ||
} else { | ||
baseFlags.Visibility = strings.ToUpper(cliFlags.Visibility) | ||
} | ||
} else { | ||
baseFlags.Visibility = strings.ToUpper(baseFlags.Visibility) | ||
} | ||
|
||
if keys[_KEY_ENABLE_OFFLINE] || keys[_KEY_ENABLE_OFFLINE_SHORT] { | ||
baseFlags.EnableOfflineMode = cliFlags.EnableOfflineMode | ||
} | ||
|
||
if keys[_KEY_ENABLE_DEPRECATED_NOTICE] || keys[_KEY_ENABLE_DEPRECATED_NOTICE_SHORT] { | ||
baseFlags.EnableDeprecatedNotice = cliFlags.EnableDeprecatedNotice | ||
} | ||
|
||
if keys[_KEY_ENABLE_GUIDE] || keys[_KEY_ENABLE_GUIDE_SHORT] { | ||
baseFlags.EnableGuide = cliFlags.EnableGuide | ||
} | ||
|
||
if keys[_KEY_ENABLE_EDITOR] || keys[_KEY_ENABLE_EDITOR_SHORT] { | ||
baseFlags.EnableEditor = cliFlags.EnableEditor | ||
} | ||
|
||
if keys[_KEY_LOG_LEVEL] { | ||
baseFlags.LogLevel = cliFlags.LogLevel | ||
} | ||
|
||
// Forcibly disable `debug mode` in non-development mode | ||
if strings.ToLower(version.Version) != "dev" { | ||
baseFlags.DebugMode = false | ||
} else { | ||
if keys["D"] || keys["debug"] { | ||
baseFlags.DebugMode = true | ||
} | ||
} | ||
|
||
return baseFlags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,80 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/soulteary/flare/pkg/logger" | ||
"fmt" | ||
"log/slog" | ||
"runtime" | ||
"strings" | ||
|
||
FlareData "github.com/soulteary/flare/internal/data" | ||
FlareState "github.com/soulteary/flare/internal/state" | ||
) | ||
flags "github.com/spf13/pflag" | ||
|
||
func Parse() { | ||
FlareData "github.com/soulteary/flare/config/data" | ||
FlareDefine "github.com/soulteary/flare/config/define" | ||
FlareModel "github.com/soulteary/flare/config/model" | ||
FlareState "github.com/soulteary/flare/config/state" | ||
FlareLogger "github.com/soulteary/flare/internal/logger" | ||
"github.com/soulteary/flare/internal/version" | ||
) | ||
|
||
envs := parseEnvFile(parseEnvVars()) | ||
flags := parseCLI(envs) | ||
func Parse() FlareModel.Flags { | ||
envs := ParseEnvFile(ParseEnvVars()) | ||
flags := ParseCLI(envs) | ||
|
||
log := logger.GetLogger(flags.LogLevel) | ||
log.Println() | ||
log.Println("程序服务端口", flags.Port) | ||
log.Println("页面请求合并", flags.EnableMinimumRequest) | ||
log.Println("启用离线模式", flags.EnableOfflineMode) | ||
log := FlareLogger.GetLogger(flags.LogLevel) | ||
log.Info("程序服务端口", slog.Int(_KEY_PORT, flags.Port)) | ||
log.Info("页面请求合并", slog.Bool(_KEY_MINI_REQUEST, flags.EnableMinimumRequest)) | ||
log.Info("启用离线模式", slog.Bool(_KEY_ENABLE_OFFLINE, flags.EnableOfflineMode)) | ||
if flags.DisableLoginMode { | ||
log.Println("已禁用登陆模式,用户可直接调整应用设置。") | ||
log.Info("已禁用登陆模式,用户可直接调整应用设置。") | ||
} else { | ||
log.Println("启用登陆模式,调整应用设置需要先进行登陆。") | ||
log.Println("当前内容整体可见性为:", flags.Visibility) | ||
log.Info("启用登陆模式,调整应用设置需要先进行登陆。") | ||
log.Info("当前内容整体可见性为:", slog.String(_KEY_VISIBILITY, flags.Visibility)) | ||
|
||
if flags.UserIsGenerated { | ||
log.Println("用户未指定 `FLARE_USER`,使用默认用户名", _DEFAULT_USER_NAME) | ||
log.Info("用户未指定 `FLARE_USER`,使用默认用户名", slog.String("username", FlareDefine.DEFAULT_USER_NAME)) | ||
} else { | ||
log.Println("应用用户设置为", flags.User) | ||
log.Info("应用用户设置为", slog.String("username", flags.User)) | ||
|
||
} | ||
|
||
if flags.PassIsGenerated { | ||
log.Println("用户未指定 `FLARE_PASS`,自动生成应用密码", flags.Pass) | ||
log.Info("用户未指定 `FLARE_PASS`,自动生成应用密码", slog.String("password", flags.Pass)) | ||
} else { | ||
log.Println("应用登陆密码已设置为", FlareData.MaskTextWithStars(flags.Pass)) | ||
log.Info("应用登陆密码已设置为", slog.String("password", FlareData.MaskTextWithStars(flags.Pass))) | ||
} | ||
} | ||
|
||
FlareState.AppFlags = flags | ||
startDaemon(&flags) | ||
return flags | ||
} | ||
|
||
func ExcuteCLI(cliFlags *FlareModel.Flags, options *flags.FlagSet) (exit bool) { | ||
programVersion := GetVersion(false) | ||
if cliFlags.ShowHelp { | ||
fmt.Println(programVersion) | ||
fmt.Println() | ||
fmt.Println("支持命令:") | ||
options.PrintDefaults() | ||
return true | ||
} | ||
if cliFlags.ShowVersion { | ||
fmt.Println(version.Version) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func GetVersion(echo bool) string { | ||
programVersion := fmt.Sprintf("Flare v%s-%s %s/%s BuildDate=%s", version.Version, strings.ToUpper(version.Commit), runtime.GOOS, runtime.GOARCH, version.BuildDate) | ||
if echo { | ||
log := FlareLogger.GetLogger(FlareDefine.DEFAULT_LOG_LEVEL) | ||
log.Info("Flare - 🏂 Challenge all bookmarking apps and websites directories, Aim to Be a best performance monster.") | ||
log.Info("程序信息:", | ||
slog.String("version", version.Version), | ||
slog.String("commit", strings.ToUpper(version.Commit)), | ||
slog.String("GOGS/ARCH", fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)), | ||
slog.String("date", version.BuildDate), | ||
) | ||
} | ||
return programVersion | ||
} |
Oops, something went wrong.