3 Commits

Author SHA1 Message Date
24f9b2b779 Added a quick and dirty file open check
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-09-01 15:16:56 -04:00
6d1a24cefb Added some basic unit tests for some of the utility functions
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-09-01 14:25:02 -04:00
cf854a91f0 Added a main loop on a timed interval
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2022-09-01 00:17:29 -04:00
7 changed files with 97 additions and 8 deletions

View File

@ -6,6 +6,11 @@ pipeline:
- GOOS=linux GOARCH=amd64 go build -o "dist/adept-linux-amd64-${CI_COMMIT_TAG}"
- GOOS=windows GOARCH=amd64 go build -o "dist/adept-windows-amd64-${CI_COMMIT_TAG}.exe"
test:
image: golang:1.16
commands:
- go test -v ./...
gitea_release:
image: plugins/gitea-release
settings:

17
Makefile Normal file
View File

@ -0,0 +1,17 @@
BINARY_NAME=adept
all: build test
build:
go build -o ${BINARY_NAME} adept.go
test:
go test -v ./...
run:
go build -o ${BINARY_NAME} adept.go
./${BINARY_NAME}
clean:
go clean
rm ${BINARY_NAME}

View File

@ -1,10 +1,15 @@
package main
import (
"log"
"path/filepath"
"time"
"github.com/spf13/viper"
"git.metaunix.net/BitGoblin/adept/config"
"git.metaunix.net/BitGoblin/adept/transcoder"
"git.metaunix.net/BitGoblin/adept/util"
)
func main() {
@ -12,13 +17,31 @@ func main() {
r := transcoder.NewRepository(viper.GetString("transcoder.repository"))
ingestFiles := r.SearchIngest()
for _, i := range ingestFiles {
// archive file
r.ArchiveFile(i.Name())
// transcode file
transcoder.Transcode(i.Name())
// clean up source file
r.CleanupFile(i.Name())
// main program loop - runs infinitely until externally terminated
for {
ingestFiles := r.SearchIngest()
if len(ingestFiles) < 1 {
log.Println("There were no files found in ingest to transcode; skipping run.")
} else {
for _, i := range ingestFiles {
// check if the file is open in another program (e.g. still being written to)
if util.IsFileLocked(filepath.Join(viper.GetString("transcoder.repository"), "ingest", i.Name())) {
log.Printf("%s appears to be open in another program; skipping it for this run.", i.Name())
continue
}
// archive file
r.ArchiveFile(i.Name())
// transcode file
transcoder.Transcode(i.Name())
// clean up source file
r.CleanupFile(i.Name())
}
}
// sleep for X minutes - specified in the adept.toml config file
interval := viper.GetInt("transcoder.interval")
time.Sleep(time.Duration(interval) * time.Minute)
}
}

View File

@ -30,6 +30,8 @@ func NewRepository(path string) *Repository {
func (r *Repository) SearchIngest() []os.FileInfo {
ingestPath := filepath.Join(r.basePath, "ingest")
log.Printf("Searching ingest directory for files to transcode...")
ingestDir, err := os.Open(ingestPath)
if err != nil {
log.Fatalf("Error opening ingest directory: %s", err)

19
util/env_test.go Normal file
View File

@ -0,0 +1,19 @@
package util
import "testing"
func TestResolveTilde(t *testing.T) {
resolvedPath := ResolveTilde("~")
if resolvedPath == "~" {
t.Logf("ResolveTilde returned '%s'; it should have expanded to an absolute path.", resolvedPath)
}
}
func TestResolveTildePath(t *testing.T) {
resolvedPath := ResolveTilde("~/test")
if resolvedPath == "~/test" {
t.Logf("ResolveTilde returned '%s'; it should have expanded to an absolute path.", resolvedPath)
}
}

View File

@ -1,6 +1,7 @@
package util
import (
"os/exec"
"path"
"strings"
)
@ -8,3 +9,14 @@ import (
func FilenameWithoutExtension(filename string) string {
return strings.TrimSuffix(filename, path.Ext(filename))
}
func IsFileLocked(filepath string) bool {
cmd := exec.Command("/usr/bin/lsof", filepath)
stdout, _ := cmd.Output()
if strings.Contains(string(stdout), filepath) {
return true
}
return false
}

11
util/file_test.go Normal file
View File

@ -0,0 +1,11 @@
package util
import "testing"
func TestFilenameWithoutExtension(t *testing.T) {
filename := FilenameWithoutExtension("testfile.txt")
if filename != "testfile" {
t.Logf("FilenameWithoutExtension returned '%s'; it should be 'testfile'.", filename)
}
}