Add some command line flags

This commit is contained in:
IamTheFij 2020-12-01 17:25:40 -08:00
parent e08a3cf14a
commit 7ee081f9f0
2 changed files with 31 additions and 7 deletions

View File

@ -4,4 +4,12 @@ Checks current running containers for newer tags according to semver
Usage: Usage:
go run . go run . -h
-max-pages int
max number of pages to retrieve from registry (default 10)
-registry-url string
base url of the registry you want to check against (default "https://registry.hub.docker.com")
-version
display the version of dockron and exit

28
main.go
View File

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
@ -15,12 +16,17 @@ import (
dockerClient "github.com/docker/docker/client" dockerClient "github.com/docker/docker/client"
) )
// Default values for fetching tags from registry var (
var registryBase = "https://registry.hub.docker.com" // registryBaseURL is the base URL of the docker registry
var maxPages = 10 registryBaseURL = "https://registry.hub.docker.com"
// maxPages is the max number of pages to fetch from docker registry results
maxPages = 10
// Regexp used to extract tag information // Regexp used to extract tag information
var tagRegexp = regexp.MustCompile(`(.*):[vV]{0,1}([0-9.]+)(-(.*)){0,1}`) tagRegexp = regexp.MustCompile(`(.*):[vV]{0,1}([0-9.]+)(-(.*)){0,1}`)
// version of tag checker
version = "dev"
)
// ImageTag is wraps an image and tag values for a container // ImageTag is wraps an image and tag values for a container
type ImageTag struct { type ImageTag struct {
@ -126,7 +132,7 @@ func listTags(current ImageTag) ([]ImageTag, error) {
} }
} }
url := fmt.Sprintf("%s/v2/repositories/%s/tags", registryBase, current.Image) url := fmt.Sprintf("%s/v2/repositories/%s/tags", registryBaseURL, current.Image)
pageCount := 0 pageCount := 0
var response tagsResponse var response tagsResponse
var newTag ImageTag var newTag ImageTag
@ -166,6 +172,16 @@ func getNewerTags(current ImageTag) ([]ImageTag, error) {
} }
func main() { func main() {
flag.StringVar(&registryBaseURL, "registry-url", registryBaseURL, "base url of the registry you want to check against")
flag.IntVar(&maxPages, "max-pages", maxPages, "max number of pages to retrieve from registry")
var showVersion = flag.Bool("version", false, "display the version and exit")
flag.Parse()
// Print version if asked
if *showVersion {
fmt.Println("version:", version)
os.Exit(0)
}
dockerClient, err := dockerClient.NewClientWithOpts(dockerClient.FromEnv) dockerClient, err := dockerClient.NewClientWithOpts(dockerClient.FromEnv)
if err != nil { if err != nil {
fmt.Println("Could not initialize docker client") fmt.Println("Could not initialize docker client")