Skip to content

Commit 67a62f4

Browse files
committed
添加设置默认 Git CDN 功能
## 新增 1. `setcdn <gitcdn>`、`getcdn`、`delcdn` 功能
1 parent d2bcd01 commit 67a62f4

5 files changed

Lines changed: 124 additions & 23 deletions

File tree

NetCoreBeautyGlobalTool/NetCoreBeautyGlobalTool.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<ToolCommandName>ncbeauty</ToolCommandName>
1010
<PackageOutputPath>.nupkg</PackageOutputPath>
1111

12-
<Version>1.2.0</Version>
12+
<Version>1.2.1</Version>
1313

1414
<Authors>LiesAuer</Authors>
1515
<Description>Move a .NET Core app runtime components and dependencies into a sub-directory and make it beauty.</Description>

NetCoreBeautyNuget/NetCoreBeauty.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<metadata>
44
<id>nulastudio.NetCoreBeauty</id>
55
<description>Move a .NET Core app runtime components and dependencies into a sub-directory and make it beauty.</description>
6-
<version>1.2.0</version>
6+
<version>1.2.1</version>
77
<authors>LiesAuer</authors>
88
<owners>nulastudio</owners>
99
<projectUrl>https://github.com/nulastudio/NetCoreBeauty</projectUrl>

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ when you run `dotnet publish -r` (only works with `SCD` mode), everything is don
5252
```
5353
Usage:
5454
ncbeauty [--<force=True|False>] [--<gitcdn>] [--<loglevel=Error|Detail|Info>] [--<nopatch=True|False>] <beautyDir> [<libsDir>]
55+
ncbeauty [--<loglevel=Error|Detail|Info>] setcdn <gitcdn>
56+
ncbeauty [--<loglevel=Error|Detail|Info>] getcdn
57+
ncbeauty [--<loglevel=Error|Detail|Info>] delcdn
5558
```
5659
for example
5760
```
@@ -69,3 +72,15 @@ if you have troble in connecting github, use this mirror
6972
```
7073
https://gitee.com/liesauer/HostFXRPatcher
7174
```
75+
76+
## Default Git CDN
77+
`ncbeauty` [1.2.1](https://github.com/nulastudio/NetCoreBeauty/releases/tag/v1.2.1) supports setting default Git CDN now, you don't need `--gitcdn` all the time if you are using binary distribution. but how ever default git cdn can be override by `--gitcdn`.
78+
Usage:
79+
```
80+
ncbeauty [--<loglevel=Error|Detail|Info>] setcdn <gitcdn>
81+
set current default git cdn, can be override by --gitcdn.
82+
ncbeauty [--<loglevel=Error|Detail|Info>] getcdn
83+
print current default git cdn.
84+
ncbeauty [--<loglevel=Error|Detail|Info>] delcdn
85+
remove current default git cdn, after removed, use --gitcdn to specify.
86+
```

src/main/beauty.go

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ func main() {
4242
// 设置CDN
4343
manager.GitCDN = gitcdn
4444

45-
// 设置LogLevel
46-
log.DefaultLogger.LogLevel = map[string]log.LogLevel{
47-
errorLevel: log.Error,
48-
detailLevel: log.Detail,
49-
infoLevel: log.Info,
50-
}[loglevel]
51-
manager.Logger.LogLevel = log.DefaultLogger.LogLevel
52-
5345
log.LogInfo("running ncbeauty...")
5446

5547
beautyCheck := path.Join(beautyDir, "NetCoreBeauty")
@@ -122,7 +114,7 @@ func initCLI() {
122114
flag.CommandLine = flag.NewFlagSet("ncbeauty", flag.ContinueOnError)
123115
flag.CommandLine.Usage = usage
124116
flag.CommandLine.SetOutput(os.Stdout)
125-
flag.StringVar(&gitcdn, "gitcdn", "https://github.com/nulastudio/HostFXRPatcher", `specify a HostFXRPatcher mirror repo if you have troble in connecting github.
117+
flag.StringVar(&gitcdn, "gitcdn", "", `specify a HostFXRPatcher mirror repo if you have troble in connecting github.
126118
RECOMMEND https://gitee.com/liesauer/HostFXRPatcher for mainland china users.
127119
`)
128120
flag.StringVar(&loglevel, "loglevel", "Error", `log level. valid values: Error/Detail/Info
@@ -152,30 +144,96 @@ hostfxr patch fixes https://github.com/nulastudio/NetCoreBeauty/issues/1`)
152144
usage()
153145
os.Exit(0)
154146
}
155-
beautyDir = args[0]
156147

157-
if len(args) >= 2 {
158-
libsDir = flag.Arg(1)
148+
// logLevel检查
149+
if loglevel != errorLevel && loglevel != detailLevel && loglevel != infoLevel {
150+
loglevel = errorLevel
159151
}
160152

161-
beautyDir = strings.Trim(beautyDir, `"`)
162-
libsDir = strings.Trim(libsDir, `"`)
163-
absDir, err := filepath.Abs(beautyDir)
164-
if err != nil {
165-
log.LogPanic(fmt.Errorf("invalid beautyDir: %s", err.Error()), 1)
153+
// 设置LogLevel
154+
log.DefaultLogger.LogLevel = map[string]log.LogLevel{
155+
errorLevel: log.Error,
156+
detailLevel: log.Detail,
157+
infoLevel: log.Info,
158+
}[loglevel]
159+
manager.Logger.LogLevel = log.DefaultLogger.LogLevel
160+
161+
switch args[0] {
162+
case "setcdn":
163+
checkArgumentsCount(2, argv)
164+
if manager.SetCDN(strings.Trim(args[1], `"`)) {
165+
fmt.Println("set default git cdn successfully")
166+
} else {
167+
fmt.Println("set default git cdn failed")
168+
}
169+
exit()
170+
case "getcdn":
171+
checkArgumentsCount(1, argv)
172+
cdn := manager.GetCDN()
173+
if cdn == "" {
174+
fmt.Println("default git cdn has not been set yet")
175+
} else {
176+
fmt.Printf("current default git cdn: %s\n", cdn)
177+
}
178+
exit()
179+
case "delcdn":
180+
checkArgumentsCount(1, argv)
181+
cdn := manager.GetCDN()
182+
if cdn == "" {
183+
fmt.Println("default git cdn has not been set yet")
184+
} else {
185+
manager.DelCDN()
186+
fmt.Printf("current default git cdn has been deleted, it was: [%s] before\n", cdn)
187+
}
188+
exit()
189+
default:
190+
if gitcdn == "" {
191+
cdn := manager.GetCDN()
192+
if cdn == "" {
193+
gitcdn = "https://github.com/nulastudio/HostFXRPatcher"
194+
} else {
195+
gitcdn = cdn
196+
}
197+
}
198+
199+
beautyDir = args[0]
200+
201+
if len(args) >= 2 {
202+
libsDir = flag.Arg(1)
203+
}
204+
205+
beautyDir = strings.Trim(beautyDir, `"`)
206+
libsDir = strings.Trim(libsDir, `"`)
207+
absDir, err := filepath.Abs(beautyDir)
208+
if err != nil {
209+
log.LogPanic(fmt.Errorf("invalid beautyDir: %s", err.Error()), 1)
210+
}
211+
beautyDir = absDir
166212
}
167-
beautyDir = absDir
213+
}
168214

169-
// logLevel检查
170-
if loglevel != errorLevel && loglevel != detailLevel && loglevel != infoLevel {
171-
loglevel = errorLevel
215+
func checkArgumentsCount(excepted int, got int) bool {
216+
if excepted == got {
217+
return true
172218
}
219+
log.LogPanic(fmt.Errorf("Too few or many arguments, expected %d, got %d", excepted, got), 1)
220+
return false
173221
}
174222

175223
func usage() {
176224
fmt.Println("Usage:")
177225
fmt.Println("ncbeauty [--<force=True|False>] [--<gitcdn>] [--<loglevel=Error|Detail|Info>] [--<nopatch=True|False>] <beautyDir> [<libsDir>]")
178226
flag.PrintDefaults()
227+
fmt.Println("ncbeauty [--<loglevel=Error|Detail|Info>] setcdn <gitcdn>")
228+
fmt.Println(" set current default git cdn, can be override by --gitcdn.")
229+
fmt.Println("ncbeauty [--<loglevel=Error|Detail|Info>] getcdn")
230+
fmt.Println(" print current default git cdn.")
231+
fmt.Println("ncbeauty [--<loglevel=Error|Detail|Info>] delcdn")
232+
fmt.Println(" remove current default git cdn, after removed, use --gitcdn to specify.")
233+
}
234+
235+
func exit() {
236+
os.Exit(0)
179237
}
180238

181239
func patch(fxrVersion string, rid string) bool {

src/manager/manager.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ var timeout = 60 * time.Second
2929
var localPath = filepath.Clean(os.TempDir()) + "/NetCoreBeauty"
3030
var localArtifactsPath = localPath + "/artifacts"
3131
var artifactsVersionTXT = "/ArtifactsVersion.txt"
32+
var gitCDNTXT = "/git.cdn"
3233
var artifactsVersionJSON = "/ArtifactsVersion.json"
3334
var onlineArtifactsVersionJSON = "/OnlineArtifactsVersion.json"
3435
var artifactsVersionOldPath = localArtifactsPath + artifactsVersionTXT
36+
var gitCDNPath = localPath + gitCDNTXT
3537
var artifactsVersionPath = localArtifactsPath + artifactsVersionJSON
3638
var onlineArtifactsVersionPath = localArtifactsPath + onlineArtifactsVersionJSON
3739

@@ -571,3 +573,29 @@ func FixDeps(deps string) ([]string, string, string) {
571573

572574
return files, "v" + fxrVersion, rid
573575
}
576+
577+
// SetCDN 设置默认CDN
578+
func SetCDN(cdn string) bool {
579+
if err := ioutil.WriteFile(gitCDNPath, []byte(cdn), 0666); err != nil {
580+
log.LogError(err, false)
581+
return false
582+
}
583+
return true
584+
}
585+
586+
// GetCDN 获取默认CDN
587+
func GetCDN() string {
588+
if gitcdn, err := ioutil.ReadFile(gitCDNPath); err == nil {
589+
return string(gitcdn)
590+
}
591+
return ""
592+
}
593+
594+
// DelCDN 删除默认CDN
595+
func DelCDN() bool {
596+
if err := os.Remove(gitCDNPath); err != nil {
597+
log.LogError(err, false)
598+
return false
599+
}
600+
return true
601+
}

0 commit comments

Comments
 (0)