-
Notifications
You must be signed in to change notification settings - Fork 39
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
Showing
11 changed files
with
610 additions
and
544 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package FlareCMD | ||
|
||
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.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 | ||
} | ||
|
||
// 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
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,79 @@ | ||
package FlareCMD_test | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
FlareCMD "github.com/soulteary/flare/cmd" | ||
FlareModel "github.com/soulteary/flare/config/model" | ||
"github.com/soulteary/flare/internal/version" | ||
flags "github.com/spf13/pflag" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func captureOutput(f func()) string { | ||
old := os.Stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
outC := make(chan string) | ||
go func() { | ||
var buf bytes.Buffer | ||
io.Copy(&buf, r) | ||
outC <- buf.String() | ||
}() | ||
f() | ||
w.Close() | ||
os.Stdout = old | ||
return <-outC | ||
} | ||
|
||
func TestExcuteCLI_ShowHelp(t *testing.T) { | ||
cliFlags := &FlareModel.Flags{ShowHelp: true} | ||
options := &flags.FlagSet{} | ||
|
||
output := captureOutput(func() { | ||
_ = FlareCMD.ExcuteCLI(cliFlags, options) | ||
}) | ||
|
||
assert.Contains(t, output, "支持命令:", "应该打印出支持命令") | ||
assert.True(t, FlareCMD.ExcuteCLI(cliFlags, options), "在 ShowHelp 为 true 时,应该返回 true") | ||
} | ||
|
||
func TestExcuteCLI_ShowVersion(t *testing.T) { | ||
cliFlags := &FlareModel.Flags{ShowVersion: true} | ||
options := &flags.FlagSet{} | ||
|
||
output := captureOutput(func() { | ||
_ = FlareCMD.ExcuteCLI(cliFlags, options) | ||
}) | ||
|
||
assert.Contains(t, output, version.Version, "应该打印出版本信息") | ||
assert.True(t, FlareCMD.ExcuteCLI(cliFlags, options), "在 ShowVersion 为 true 时,应该返回 true") | ||
} | ||
|
||
func TestExcuteCLI_NoFlags(t *testing.T) { | ||
cliFlags := &FlareModel.Flags{} | ||
options := &flags.FlagSet{} | ||
|
||
assert.False(t, FlareCMD.ExcuteCLI(cliFlags, options), "当没有任何标志被设置时,应该返回 false") | ||
} | ||
|
||
func TestGetVersionEcho(t *testing.T) { | ||
ver := "" | ||
// output := captureOutput(func() { | ||
ver = FlareCMD.GetVersion(true) | ||
// }) | ||
assert.Contains(t, ver, version.Version, "应该打印出版本信息") | ||
// assert.Contains(t, output, "Challenge all bookmarking apps and websites directories, Aim to Be a best performance monster.", "应该打印详细信息") | ||
} | ||
|
||
func TestGetVersionMute(t *testing.T) { | ||
ver := "" | ||
output := captureOutput(func() { | ||
ver = FlareCMD.GetVersion(false) | ||
}) | ||
assert.Contains(t, ver, version.Version, "应该打印出版本信息") | ||
assert.NotContains(t, output, "Challenge all bookmarking apps and websites directories, Aim to Be a best performance monster.", "不应该打印详细信息") | ||
} |
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,71 @@ | ||
package FlareCMD | ||
|
||
import ( | ||
"fmt" | ||
|
||
env "github.com/caarlos0/env/v6" | ||
|
||
FlareData "github.com/soulteary/flare/config/data" | ||
FlareDefine "github.com/soulteary/flare/config/define" | ||
FlareModel "github.com/soulteary/flare/config/model" | ||
FlareLogger "github.com/soulteary/flare/internal/logger" | ||
) | ||
|
||
func InitAccountFromEnvVars( | ||
username string, password string, targetUser *string, targetPass *string, defaultName string, | ||
isUserGenerate *bool, isPassGenerate *bool, disableLogin *bool) { | ||
|
||
if username == "" { | ||
*targetUser = defaultName | ||
*isUserGenerate = true | ||
} else { | ||
*isUserGenerate = false | ||
*targetUser = username | ||
} | ||
|
||
if password == "" { | ||
*targetPass = FlareData.GenerateRandomString(8) | ||
*isPassGenerate = true | ||
} else { | ||
*isPassGenerate = false | ||
*targetPass = password | ||
} | ||
} | ||
|
||
func ParseEnvVars() (stor FlareModel.Flags) { | ||
log := FlareLogger.GetLogger() | ||
|
||
// 1. init default values | ||
defaults := FlareDefine.GetDefaultEnvVars() | ||
|
||
// 2. overwrite with user input | ||
if err := env.Parse(&defaults); err != nil { | ||
log.Error(fmt.Sprintf("%+v\n", err)) | ||
return | ||
} | ||
|
||
// 3. update username and password | ||
InitAccountFromEnvVars( | ||
defaults.User, | ||
defaults.Pass, | ||
&stor.User, | ||
&stor.Pass, | ||
FlareDefine.DEFAULT_USER_NAME, | ||
&stor.UserIsGenerated, | ||
&stor.PassIsGenerated, | ||
&stor.DisableLoginMode, | ||
) | ||
|
||
// 4. merge | ||
stor.Port = defaults.Port | ||
stor.EnableGuide = defaults.EnableGuide | ||
stor.EnableDeprecatedNotice = defaults.EnableDeprecatedNotice | ||
stor.EnableMinimumRequest = defaults.EnableMinimumRequest | ||
stor.DisableLoginMode = defaults.DisableLoginMode | ||
stor.Visibility = defaults.Visibility | ||
stor.EnableOfflineMode = defaults.EnableOfflineMode | ||
stor.EnableEditor = defaults.EnableEditor | ||
stor.DisableCSP = defaults.DisableCSP | ||
|
||
return stor | ||
} |
Oops, something went wrong.