add --dry-run option

This commit is contained in:
ari melody 2026-06-28 13:35:06 +01:00
parent f6e42cd7e6
commit 0ac59c5407
Signed by: ari
GPG key ID: 60B5F0386E3DDB7E
2 changed files with 57 additions and 42 deletions

98
main.go
View file

@ -77,6 +77,7 @@ func main() {
var logout bool = false
var deleteFullVod bool = false
var forceUpload bool = false
var dryRun bool = false
var directory string = "."
for i, arg := range os.Args {
@ -110,6 +111,9 @@ func main() {
case "--force":
forceUpload = true
case "--dry-run":
dryRun = true
default:
fmt.Fprintf(os.Stderr, "Unknown option `%s`\n", arg)
os.Exit(1)
@ -273,29 +277,35 @@ func main() {
)
}
// concatenate VOD segments into full VOD
fullVodExists := func () bool {
if (!dryRun) {
// check if full VOD already exists with expected duration
fullVodExists := false
fullVodProbe, err := scanner.ProbeSegment(video.Filepath)
if err != nil { return false }
video.SizeBytes = fullVodProbe.Format.Size
var totalLength float64 = 0
if err == nil {
video.SizeBytes = fullVodProbe.Format.Size
var totalLength float64 = 0
for _, filename := range vodFiles {
probe, err := scanner.ProbeSegment(path.Join(footageDir, filename))
if err != nil { continue }
totalLength += probe.Format.Duration
for _, filename := range vodFiles {
probe, err := scanner.ProbeSegment(path.Join(footageDir, filename))
if err != nil { continue }
totalLength += probe.Format.Duration
}
// full VOD may exist, but not be complete (interrupted concat)
fullVodExists = math.Abs(fullVodProbe.Format.Duration - totalLength) < float64(0.1)
}
if fullVodExists {
log.Print("Full VOD appears to already exist- uploading this file...")
} else {
// concatenate VOD segments into full VOD
video.SizeBytes, err = vid.ConcatVideo(video, vodFiles, verbose)
if err != nil {
log.Fatalf("Failed to concatenate VOD segments: %v", err)
os.Exit(1)
}
}
return math.Abs(fullVodProbe.Format.Duration - totalLength) < float64(0.1)
}()
if fullVodExists {
log.Print("Full VOD appears to already exist- uploading this file...")
} else {
video.SizeBytes, err = vid.ConcatVideo(video, vodFiles, verbose)
if err != nil {
log.Fatalf("Failed to concatenate VOD segments: %v", err)
os.Exit(1)
}
log.Print("Dry run: Skipping VOD concatenation")
}
// youtube oauth flow
@ -329,35 +339,39 @@ func main() {
log.Fatalf("Failed to save OAuth token: %v", err)
}
// okay actually upload now!
ytVideo, err := yt.UploadVideo(ctx, tokenSource, video, thumbnail, templates)
if err != nil {
log.Fatalf("Failed to upload video: %v", err)
os.Exit(1)
}
if verbose {
jsonString, err := json.MarshalIndent(ytVideo, "", " ")
if (!dryRun) {
// okay actually upload now!
ytVideo, err := yt.UploadVideo(ctx, tokenSource, video, thumbnail, templates)
if err != nil {
log.Fatalf("Failed to marshal video data json: %v", err)
log.Fatalf("Failed to upload video: %v", err)
os.Exit(1)
}
fmt.Println(string(jsonString))
}
log.Print("Video uploaded successfully!")
if verbose {
jsonString, err := json.MarshalIndent(ytVideo, "", " ")
if err != nil {
log.Fatalf("Failed to marshal video data json: %v", err)
}
fmt.Println(string(jsonString))
}
log.Print("Video uploaded successfully!")
// update metadata to reflect VOD is uploaded
// TODO: rather than a boolean flag, link to actual video
metadata.Uploaded = true
err = scanner.WriteMetadata(directory, metadata)
if err != nil {
log.Fatalf("Failed to update metadata: %v", err)
}
// delete full VOD after upload, if requested
if deleteFullVod {
err = os.Remove(video.Filepath)
// update metadata to reflect VOD is uploaded
// TODO: rather than a boolean flag, link to actual video
metadata.Uploaded = true
err = scanner.WriteMetadata(directory, metadata)
if err != nil {
log.Fatalf("Failed to delete full VOD: %v", err)
log.Fatalf("Failed to update metadata: %v", err)
}
// delete full VOD after upload, if requested
if deleteFullVod {
err = os.Remove(video.Filepath)
if err != nil {
log.Fatalf("Failed to delete full VOD: %v", err)
}
}
} else {
log.Print("Dry run: Skipping video upload")
}
}

View file

@ -8,6 +8,7 @@ OPTIONS:
--init: Initialise `directory` as a VOD directory.
--logout: Logs out of the current YouTube account.
-d, --deleteAfter: Deletes the full VOD after upload.
--dry-run: Don't upload or modify any files (Useful with -v)
-f, --force: Force uploading the VOD, even if it already exists.
SOURCE: https://codeberg.org/arimelody/vodular