Added several flags to configure the program

testing
Justin Reichardt 2022-11-16 12:52:10 -06:00
parent df57d7a57e
commit f20308f7ce
3 changed files with 24 additions and 14 deletions

View File

@ -13,5 +13,9 @@ A Makefile is in progress but currently does not add the version.
# Run
The default port is 8080. Env VIDDOWNLOC is where videos are saved to.
The default port is 8080.
For help:
viddown -h

View File

@ -6,6 +6,4 @@ RUN apt-get clean
COPY viddown /usr/bin/viddown
ENV VIDDOWNLOC /srv/
CMD ["/usr/bin/viddown"]
CMD ["/usr/bin/viddown", "-o", "/srv"]

View File

@ -21,6 +21,7 @@ package main
import (
"encoding/json"
"flag"
"io"
"io/ioutil"
"log"
@ -80,6 +81,10 @@ var currentWork map[int]string
var config struct {
// Where files should be saved
saveLocation string
// Where to find youtube-dl program, must be full name
youtubedll string
// The port the server listens on
port int
}
// Global channel to send operation requests to
@ -134,7 +139,7 @@ func operationHandler() {
os.MkdirAll(dir, 0755)
// Build the download command
cmd := exec.Command("youtube-dl", o.URL)
cmd := exec.Command(config.youtubedll, o.URL)
cmd.Dir = dir + "/"
currentWork[i] = o.URL // Adds the URL to list of current work
output, err := cmd.CombinedOutput()
@ -171,15 +176,18 @@ func operationHandler() {
log.Fatal("Exited operationsHandler")
}
func main() {
// Set the location to save videos
if os.Getenv("VIDDOWNLOC") == "" {
config.saveLocation = "./"
} else {
config.saveLocation = os.Getenv("VIDDOWNLOC")
}
// handleConfigs configures the program at start, handling any flags or env variables
func handleConfigs() {
flag.StringVar(&config.saveLocation, "o", "./", "The output location for videos")
flag.StringVar(&config.youtubedll, "y", "youtube-dl", "The full location of youtube-dl")
flag.IntVar(&config.port, "p", 8080, "The port the server listens on")
flag.Parse()
log.Println("Downloading files to: " + config.saveLocation)
}
func main() {
handleConfigs()
// initialize currentWork with a blank map
currentWork = make(map[int]string)
@ -192,6 +200,6 @@ func main() {
// Start the http server
handler := http.HandlerFunc(handleRequest)
http.Handle("/", handler)
log.Printf("Starting server on 8080")
http.ListenAndServe(":8080", nil)
log.Printf("Starting server on " + strconv.Itoa(config.port))
http.ListenAndServe(":"+strconv.Itoa(config.port), nil)
}