diff --git a/.github/workflows/asteroid.yml b/.github/workflows/asteroid.yml index efa2e26..078ca17 100644 --- a/.github/workflows/asteroid.yml +++ b/.github/workflows/asteroid.yml @@ -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 @@ -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: @@ -127,7 +127,7 @@ 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 @@ -135,5 +135,5 @@ jobs: 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 }} diff --git a/.gitignore b/.gitignore index b71045f..286db38 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ -/.idea + +/.idea \ No newline at end of file diff --git a/README.md b/README.md index 31593d7..7e8874e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ``` @@ -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. @@ -101,3 +101,6 @@ chmod +x install.sh chmod +x uninstall.sh ./uninstall.sh ``` + + + diff --git a/global/Global.go b/global/Global.go index e5bcb22..1640f31 100644 --- a/global/Global.go +++ b/global/Global.go @@ -6,6 +6,7 @@ import ( "io" "log" "os" + "sync" "time" "github.com/qcozof/asteroid/model" @@ -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 @@ -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() } diff --git a/main.go b/main.go index 063ff93..4cf6c2b 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "flag" "fmt" "log" - "os" "strings" "time" @@ -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 @@ -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) @@ -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) { @@ -195,7 +191,7 @@ _____monitor: if err != nil { global.ErrorToChan(tips, err) - pressAnyKeyToContinue() + commandUtils.PressAnyKeyToContinue() return } @@ -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) -} +} \ No newline at end of file diff --git a/service/InitService.go b/service/InitService.go index 75d343d..404e212 100644 --- a/service/InitService.go +++ b/service/InitService.go @@ -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 } diff --git a/utils/CommandUtils.go b/utils/CommandUtils.go new file mode 100644 index 0000000..eefd85c --- /dev/null +++ b/utils/CommandUtils.go @@ -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) +}