Skip to content

Commit

Permalink
fix bug and optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
q.wu committed Jun 30, 2023
2 parents c652372 + c820465 commit 53c50a1
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 66 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/asteroid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,24 @@ jobs:
- name: Get project dependencies
run: go mod download

- name: Build android
- name: Build asteroid
run: |
mkdir -p build_assets
go build -v -o build_assets/android -trimpath -ldflags "-s -w -buildid="
go build -v -o build_assets/asteroid -trimpath -ldflags "-s -w -buildid="
cp -r LICENSE README.md misc/ build_assets/
- name: Rename Windows android
- name: Rename Windows asteroid
if: matrix.goos == 'windows'
run: |
cd ./build_assets || exit 1
mv android android.exe
mv asteroid asteroid.exe
- name: Create ZIP archive
run: |
pushd build_assets || exit 1
zip -9vr ../android-$ASSET_NAME.zip .
zip -9vr ../asteroid-$ASSET_NAME.zip .
popd || exit 1
FILE=./android-$ASSET_NAME.zip
FILE=./asteroid-$ASSET_NAME.zip
DGST=$FILE.dgst
openssl dgst -md5 $FILE | sed 's/([^)]*)//g' >>$DGST
openssl dgst -sha1 $FILE | sed 's/([^)]*)//g' >>$DGST
Expand All @@ -114,8 +114,8 @@ jobs:
- name: Upload ZIP file to Artifacts
uses: actions/upload-artifact@v3
with:
name: android-${{ steps.get_filename.outputs.ASSET_NAME }}.zip
path: android-${{ steps.get_filename.outputs.ASSET_NAME }}.zip
name: asteroid-${{ steps.get_filename.outputs.ASSET_NAME }}.zip
path: asteroid-${{ steps.get_filename.outputs.ASSET_NAME }}.zip

- name: Debug
env:
Expand All @@ -127,13 +127,13 @@ jobs:
echo "Files in root directory: $(ls /)"
echo "GitHub workspace directory: $(ls $GITHUB_WORKSPACE)"
echo ${{ github.ref }}
echo android-${{ steps.get_filename.outputs.ASSET_NAME }}.zip
echo asteroid-${{ steps.get_filename.outputs.ASSET_NAME }}.zip
- name: Upload files to GitHub release
uses: svenstaro/upload-release-action@v2
#if: github.event_name == 'release'
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file_glob: true
file: ./android-${{ steps.get_filename.outputs.ASSET_NAME }}.zip*
file: ./asteroid-${{ steps.get_filename.outputs.ASSET_NAME }}.zip*
tag: ${{ github.ref }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/
/.idea

/.idea
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ monitor-interval: 10 #Watch every 10 seconds
site-list:
- site-name: site1 #sitename cannot duplicate
site-dir: D:\site1 #Specify the site dir to watch
include-ext: [] #Watch file extensions, leave [] to watch all files
include-ext: ['.php','.asp'] #Watch file extensions, leave [] to watch all files
exclude-dir: #Ignore watch dirs, leave [] to watch all dirs
- D:\site1\exclude1
- D:\site1\exclude2
Expand Down Expand Up @@ -44,22 +44,22 @@ email:
### II. How to run:
1. Put `asteroid.exe` file and `misc` dir to the parent level of `D:\site1` like `D:\`
2. Open a terminal like cmd or powershell
#### . Init data
- #### Init data

`all` can be replace as specify site name, such as `site1`.
```cmd
asteroid.exe --act init --site all
```

#### . Monitor site
- #### Monitor site
```cmd
asteroid.exe --act watch --site all
```
#### . Uninstall
- #### Uninstall
```cmd
asteroid.exe --act uninstall --site all
```
#### . Version
- #### Version
```cmd
asteroid.exe version
```
Expand All @@ -86,7 +86,7 @@ nssm remove asteroid confirm
```

### Linux
Expect for the installation service part, the others are almost the same.
>` Expect for the installation service part, the others are almost the same.`

### I. Set config
Refer to windows configuration.
Expand All @@ -101,3 +101,6 @@ chmod +x install.sh
chmod +x uninstall.sh
./uninstall.sh
```



64 changes: 46 additions & 18 deletions global/Global.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log"
"os"
"sync"
"time"

"github.com/qcozof/asteroid/model"
Expand Down Expand Up @@ -43,6 +44,8 @@ var (
const LogDir = "logs/"
const dbName = "asteroid.db"

var mutex sync.Mutex

func InitDB() error {
var err error
dbPath := MiscDir + dbName
Expand Down Expand Up @@ -112,34 +115,59 @@ func InitConfig() error {
return err
}

func InitLog() error {
func InitLog(runOnce bool) error {
var err error
logDir := AsteroidDir + LogDir
err := os.MkdirAll(logDir, os.ModePerm)
if err != nil {
return err
}

LogDate = time.Now()
logFileFmt := fmt.Sprintf("%s%s.log", logDir, LogDate.Format("2006-01-02"))
logFile, err := os.OpenFile(logFileFmt, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
return err
if runOnce {
err = os.MkdirAll(logDir, os.ModePerm)
if err != nil {
return err
}
}

//defer logFile.Close()
var logFile os.File
for {
if !runOnce {
now := time.Now()
tomorrow0Ux := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, now.Location())
time.Sleep(time.Duration(tomorrow0Ux.Sub(now).Nanoseconds()) * time.Nanosecond) //10000s
logFile.Close()
}

logFileFmt := fmt.Sprintf("%s%s.log", logDir, LogDate.Format("2006-01-02"))
logFile, err := openFile(logFileFmt)
if err != nil {
fmt.Println("open log file failed, err:", err)
return err
}

//defer logFile.Close()

//both write to console and file
multiWriter := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(multiWriter)

//同时输出到控制台和文件中
multiWriter := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(multiWriter)
//only write to file
//log.SetOutput(logFile)

//只输出到文件中
//log.SetOutput(logFile)
log.SetPrefix(" [asteroid] ")
log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate)

if runOnce {
break
}
}

log.SetPrefix("[asteroid] ")
log.SetFlags(log.Lshortfile | log.LstdFlags)
return err
}

func openFile(logFileFmt string) (*os.File, error) {
mutex.Lock()
defer mutex.Unlock()
return os.OpenFile(logFileFmt, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
}

func ErrorToChan(msg string, err error) {
BroadcastErrorList <- msg + err.Error()
}
Expand Down
49 changes: 19 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"flag"
"fmt"
"log"
"os"
"strings"
"time"

Expand All @@ -26,10 +25,12 @@ const (
Uninstall ActionType = "uninstall"
)

const noticeTitleLen = 50

//go:embed misc/description.txt
var projectDescription string

const noticeTitleLen = 50
var commandUtils utils.CommandUtils

func main() {
var act ActionType
Expand All @@ -46,71 +47,72 @@ func main() {
args := flag.Args()
if len(args) > 0 && args[0] == "version" {
fmt.Println(utils.Pur("v0.1"))
pressAnyKeyToContinue()
commandUtils.PressAnyKeyToContinue()
}

fmt.Println(utils.Pur(projectDescription))

if len(act) == 0 {
log.Println(utils.Fata("missing param --act, usage:"), actSupported)
pressAnyKeyToContinue()
commandUtils.PressAnyKeyToContinue()
}
if len(site) == 0 {
log.Println(utils.Fata("missing param --site usage:"), actSupported)
pressAnyKeyToContinue()
commandUtils.PressAnyKeyToContinue()
}

if err := global.InitProjDir(); err != nil {
log.Println("global.InitProjDir:", utils.Fata(err))
pressAnyKeyToContinue()
commandUtils.PressAnyKeyToContinue()
}

if err := global.InitLog(); err != nil {
if err := global.InitLog(true); err != nil {
log.Println("global.InitLog:", utils.Fata(err))
pressAnyKeyToContinue()
commandUtils.PressAnyKeyToContinue()
}

if err := global.InitConfig(); err != nil {
log.Println("global.InitConfig:", utils.Fata(err))
pressAnyKeyToContinue()
commandUtils.PressAnyKeyToContinue()
}

if err := global.InitDB(); err != nil {
log.Println("global.InitDB:", utils.Fata(err))
pressAnyKeyToContinue()
commandUtils.PressAnyKeyToContinue()
}

db, err := global.GormDB.DB()
if err != nil {
log.Println(utils.Fata(err))
pressAnyKeyToContinue()
commandUtils.PressAnyKeyToContinue()
}
defer db.Close()

myNotify.InitConfig(global.ConfigFile)
go global.InitLog(false)

siteList, err := getMatchSites(string(site))
if err != nil {
log.Println(utils.Fata(err))
pressAnyKeyToContinue()
commandUtils.PressAnyKeyToContinue()
}

for _, siteModel := range siteList {
siteDir := siteModel.SiteDir
if strings.TrimSpace(siteDir) == "" {
log.Println(utils.Fata("siteDir cannot be empty."))
pressAnyKeyToContinue()
commandUtils.PressAnyKeyToContinue()
}

if !utils.ExistsDir(siteDir) {
log.Println(utils.Fata("siteDir is not a dir."))
pressAnyKeyToContinue()
commandUtils.PressAnyKeyToContinue()
}

siteDirName, err := utils.GetLastDirName(siteDir, true)
if err != nil {
log.Println(utils.Fata("GetLastDirName err:", err.Error()))
pressAnyKeyToContinue()
commandUtils.PressAnyKeyToContinue()
}

go grt(act, siteModel, actSupported, siteDir, siteDirName)
Expand Down Expand Up @@ -155,14 +157,8 @@ func main() {
fmt.Print(".")
}

/* if act != Watch {
time.Sleep(time.Second * 5)
break
}*/

time.Sleep(time.Second)
}

}

func grt(act ActionType, siteModel model.SiteModel, actSupported, siteDir, siteDirName string) {
Expand Down Expand Up @@ -195,7 +191,7 @@ _____monitor:
if err != nil {
global.ErrorToChan(tips, err)

pressAnyKeyToContinue()
commandUtils.PressAnyKeyToContinue()
return
}

Expand Down Expand Up @@ -239,11 +235,4 @@ func getMatchSites(siteNameStr string) ([]model.SiteModel, error) {
}

return siteList, nil
}

func pressAnyKeyToContinue() {
fmt.Println("Press any key to exit.")
var input string
fmt.Scanln(&input)
os.Exit(0)
}
}
2 changes: 1 addition & 1 deletion service/InitService.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func InitService(siteModel model.SiteModel, siteDirName string) error {
//add siteDirName data to repo

beginTime := time.Now()
global.InfoToChan(siteDirName, " it may take some minutes or hours...")
global.InfoToChan(siteDirName, " it may take some minutes ...")
if err := copyToRepository(siteModel, siteDirName, siteModel.SiteDir, global.RepositoryDir+siteDirName); err != nil {
return err
}
Expand Down
16 changes: 16 additions & 0 deletions utils/CommandUtils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package utils

import (
"fmt"
"os"
)

type CommandUtils struct {
}

func (CommandUtils) PressAnyKeyToContinue() {
fmt.Println("Press any key to exit.")
var input string
fmt.Scanln(&input)
os.Exit(0)
}

0 comments on commit 53c50a1

Please sign in to comment.