Skip to content

Commit

Permalink
Merge branch 'gorun'
Browse files Browse the repository at this point in the history
  • Loading branch information
chunrichi committed Mar 3, 2023
2 parents a36e3d1 + 73f95d0 commit c8df59b
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 21 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/go-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Go build

# 触发事件 含标签v**g
on:
push:
tags:
- "v**g"

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Change go env
run: go env -w GO111MODULE=on

- name: Build
env:
GOOS: windows
GOARCH: amd64
working-directory: ./backup4abap_go
run: go build -o backup.exe ./main.go

- name: Create Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
body: "压缩代码下载器,用于从已配置 backup4abap_interface 的 ICF 服务通过 HTTP 请求下载代码包到本地,并自动解压。目前仅生成 win64 的运行程序"
files: ./backup4abap_go/backup.exe
token: ${{ secrets.RELEASE_TOKEN }}
9 changes: 7 additions & 2 deletions backup4abap_go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func main() {
delta = "YES"

if set.AutoUnZip == false {
fmt.Print("是否增量?(Yes/No) 默认 Yes:")
fmt.Print("\t程序下载\n是否增量?(Yes/No) 默认 Yes:")
fmt.Scanln(&delta)
}

Expand Down Expand Up @@ -129,13 +129,18 @@ func main() {
return
}

UzipTime := time.Now()

// 解压
err = util.DeCompressed(filename)
err = util.DeCompressed(filename, set.ProcessBar)
if err != nil {
fmt.Println(err)
return
}

useTime = time.Since(UzipTime)
fmt.Printf("\nUzip Cost Time: %s\n", useTime)

// 删除文件
err = os.Remove(filename)
if err != nil {
Expand Down
48 changes: 48 additions & 0 deletions backup4abap_go/util/probar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package util

import (
"fmt"
"strings"
)

type Bar struct {
percent int64 // 百分比
curr int64
total int64
grap string
show string
IsInit bool
}

func (bar *Bar) Init(total int64, grap string) {
bar.IsInit = true
bar.total = total
if grap == "" {
bar.grap = "#"
}
bar.show = strings.Repeat(bar.grap, 100)
}

func (bar *Bar) percents() {
bar.percent = int64(float32(bar.curr) / float32(bar.total) * 100)
}

func (bar *Bar) Add(desc string) {
if !bar.IsInit {
return
}

bar.curr++

bar.percents()

fmt.Printf("\r[%-50s]%3d%% %8d/%d %-10s", bar.show[:bar.percent/2], bar.percent, bar.curr, bar.total, desc)
}

func (bar *Bar) End() {
if !bar.IsInit {
return
}

fmt.Println()
}
47 changes: 30 additions & 17 deletions backup4abap_go/util/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
const ConfigFileName string = "config.json"

type Set struct {
SapSever string
Username string
Password string
AutoUnZip bool
SapSever string
Username string
Password string
AutoUnZip bool
ProcessBar bool
}

func (set Set) IsEmpty() bool {
Expand Down Expand Up @@ -109,21 +110,12 @@ func settingProcess(set *Set) error {
fmt.Scanln(&set.Password)

// 是否自动解压
var autoUnZip string
set.AutoUnZip = yesOrNo("是否自动解压到当前文件夹", true)

autoUnZip = "y"
// 是否启用进度条
set.ProcessBar = yesOrNo("是否启用进度条(不显示详情)", true)

fmt.Print("\n是否自动解压到当前文件夹(y/n):")
fmt.Scanln(&autoUnZip)

autoUnZip = strings.ToLower(autoUnZip)
autoUnZip = strings.TrimSpace(autoUnZip)

if autoUnZip[:1] == "y" {
set.AutoUnZip = true
} else {
set.AutoUnZip = false
}
fmt.Println()

return nil
}
Expand All @@ -137,3 +129,24 @@ func checkHttpUrl(url string) error {

return nil
}

func yesOrNo(choeseDest string, defBool bool) bool {
var boolChoese string

if defBool {
boolChoese = "y"
}

fmt.Printf("\n%s(y/n)", choeseDest)
fmt.Scanln(&boolChoese)

boolChoese = strings.ToLower(boolChoese)
boolChoese = strings.TrimSpace(boolChoese)

if boolChoese[:1] == "y" {
return true
} else {
return false
}

}
19 changes: 17 additions & 2 deletions backup4abap_go/util/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,39 @@ import (
"strings"
)

func DeCompressed(src string) error {
var probar Bar

func DeCompressed(src string, procBar bool) error {
ZipReader, err := zip.OpenReader(src)
if err != nil {
return err
}

defer ZipReader.Close()

fmt.Printf("解压数量:%6v\n", len(ZipReader.File))

if procBar {
probar.Init(int64(len(ZipReader.File)), "")
}

for _, f := range ZipReader.File {
if err := deCompressed(f); err != nil {
return err
}
}

probar.End()

return nil
}

func deCompressed(f *zip.File) error {
fmt.Println(f.Name)
if probar.IsInit {
probar.Add(f.Name)
} else {
fmt.Println(f.Name)
}

if f.FileInfo().IsDir() {
err := os.MkdirAll(f.Name, 0755)
Expand Down

0 comments on commit c8df59b

Please sign in to comment.