Skip to content

Commit

Permalink
feat: parse cli args to set port, print version or help.
Browse files Browse the repository at this point in the history
  • Loading branch information
alfg committed Mar 17, 2021
1 parent c413a7f commit c36cbab
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ jobs:
run: |
docker login ghcr.io --username $GITHUB_ACTOR --password ${{ secrets.CR_TOKEN }}
docker build . --file Dockerfile --tag ghcr.io/alfg/ffmpegd:${GITHUB_REF/refs\/tags\//}
docker push ghcr.io/alfg/ffmpegd:latest
docker push ghcr.io/alfg/ffmpegd:${GITHUB_REF/refs\/tags\//}
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ A Docker image is available with [alfg/ffmpeg](https://github.com/alfg/docker-ff
$ docker run -it -p 8080:8080 -v /tmp/:/home alfg/ffmpegd
```

Or via GitHub Container Registry:
```
$ docker run -it -p 8080:8080 -v /tmp/:/home ghcr.io/alfg/ffmpegd
```

Or using the `docker-compose` example:
```
$ docker-compose up ffmpegd
Expand Down Expand Up @@ -94,7 +99,6 @@ go build -v cmd/ffmpegd.go
```

## TODO
* More CLI flags for server, ports, cwd and daemon mode.
* Logging levels and output
* More error handling
* API documentation
Expand Down
41 changes: 33 additions & 8 deletions cmd/ffmpegd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,24 @@ const (
██╔══╝ ██╔══╝ ██║╚██╔╝██║██╔═══╝ ██╔══╝ ██║ ██║██║ ██║
██║ ██║ ██║ ╚═╝ ██║██║ ███████╗╚██████╔╝██████╔╝
╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚══════╝ ╚═════╝ ╚═════╝
v0.0.5
v0.0.6
`
version = "0.0.5"
version = "ffmpegd version 0.0.6"
description = "[\u001b[32mffmpegd\u001b[0m] - websocket server for \u001b[33mffmpeg-commander\u001b[0m.\n"
usage = `
Usage:
ffmpegd [port]
ffmpegd version -- This version.
ffmpegd Run server.
ffmpegd [port] Run server on port.
ffmpegd version Print version.
ffmpegd help This help text.
`
progressInterval = time.Second * 1
)

var (
port = "8080"
allowedOrigins = []string{
"http://localhost:8080",
"http://localhost:8081",
"http://localhost:" + port,
"https://alfg.github.io",
}
clients = make(map[*websocket.Conn]bool)
Expand Down Expand Up @@ -85,6 +87,8 @@ type file struct {
}

func Run() {
parseArgs()

// CLI Banner.
printBanner()

Expand All @@ -100,6 +104,27 @@ func Run() {
startServer()
}

func parseArgs() {
args := os.Args

// Use defaults if no args are set.
if len(args) == 1 {
return
}

// Print version, help or set port.
if args[1] == "version" || args[1] == "-v" {
fmt.Println(version)
os.Exit(1)
} else if args[1] == "help" || args[1] == "-h" {
fmt.Println(usage)
os.Exit(1)
} else if _, err := strconv.Atoi(args[1]); err == nil {
port = args[1]
}

}

func printBanner() {
fmt.Println(logo)
fmt.Println(description)
Expand All @@ -113,12 +138,12 @@ func startServer() {
// Handles incoming WS messages from client.
go handleMessages()

fmt.Println(" Server started on port \u001b[33m:8080\u001b[0m.")
fmt.Println(" Server started on port \u001b[33m:" + port + "\u001b[0m.")
fmt.Println(" - Go to \u001b[33mhttps://alfg.github.io/ffmpeg-commander\u001b[0m to connect!")
fmt.Println(" - \u001b[33mffmpegd\u001b[0m must be enabled in ffmpeg-commander options.")
fmt.Println("")
fmt.Printf("Waiting for connection...")
err := http.ListenAndServe(":8080", nil)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
fmt.Println("ListenAndServe: ", err)
}
Expand Down

0 comments on commit c36cbab

Please sign in to comment.