Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for 6.0.0 release #245

Merged
merged 12 commits into from
Jul 12, 2023
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CHANGELOG

## 6.0.0
## 6.0.0 (2023-07-12)

* `geoipupdate` now supports configuration via environment variables. Any
configuration set this way will override any value from the config file,
Expand All @@ -27,6 +27,8 @@
a positional config file path argument, which can now be passed in using the
option from `WithConfigFile` along with the other optional parameters.
* `geoipupdate` and `NewConfig` no longer require a config file to exist.
* The `--stack-trace` flag has been removed. This flag has been broken since
4.11.0.

## 5.1.1 (2023-05-08)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ website](https://golang.org).

The easiest way is via `go install`:

$ go install github.com/maxmind/geoipupdate/v5/cmd/geoipupdate@latest
$ go install github.com/maxmind/geoipupdate/v6/cmd/geoipupdate@latest

This installs `geoipupdate` to `$GOPATH/bin/geoipupdate`.

Expand Down
5 changes: 1 addition & 4 deletions cmd/geoipupdate/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"log"
"os"

"github.com/maxmind/geoipupdate/v5/pkg/geoipupdate/vars"
"github.com/maxmind/geoipupdate/v6/pkg/geoipupdate/vars"
flag "github.com/spf13/pflag"
)

// Args are command line arguments.
type Args struct {
ConfigFile string
DatabaseDirectory string
StackTrace bool
Verbose bool
Output bool
Parallelism int
Expand All @@ -37,7 +36,6 @@ func getArgs() *Args {
"Store databases in this directory (uses config if not specified)",
)
help := flag.BoolP("help", "h", false, "Display help and exit")
stackTrace := flag.Bool("stack-trace", false, "Show a stack trace along with any error message")
verbose := flag.BoolP("verbose", "v", false, "Use verbose output")
output := flag.BoolP("output", "o", false, "Output download/update results in JSON format")
displayVersion := flag.BoolP("version", "V", false, "Display the version and exit")
Expand All @@ -62,7 +60,6 @@ func getArgs() *Args {
return &Args{
ConfigFile: *configFile,
DatabaseDirectory: *databaseDirectory,
StackTrace: *stackTrace,
Verbose: *verbose,
Output: *output,
Parallelism: *parallelism,
Expand Down
2 changes: 1 addition & 1 deletion cmd/geoipupdate/end_to_end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"testing"
"time"

"github.com/maxmind/geoipupdate/v5/pkg/geoipupdate"
"github.com/maxmind/geoipupdate/v6/pkg/geoipupdate"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
21 changes: 7 additions & 14 deletions cmd/geoipupdate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ package main
import (
"context"
"log"
"os"

"github.com/maxmind/geoipupdate/v5/pkg/geoipupdate"
"github.com/maxmind/geoipupdate/v5/pkg/geoipupdate/vars"
"github.com/maxmind/geoipupdate/v6/pkg/geoipupdate"
"github.com/maxmind/geoipupdate/v6/pkg/geoipupdate/vars"
)

const unknownVersion = "unknown"

// These values are set by build scripts. Changing the names of
// the variables should be considered a breaking change.
var (
version = "unknown"
version = unknownVersion
defaultConfigFile string
defaultDatabaseDirectory string
)
Expand All @@ -30,14 +31,6 @@ func main() {
}

args := getArgs()
fatalLogger := func(message string, err error) {
if args.StackTrace {
log.Printf("%s: %+v", message, err)
} else {
log.Printf("%s: %s", message, err)
}
os.Exit(1)
}

config, err := geoipupdate.NewConfig(
geoipupdate.WithConfigFile(args.ConfigFile),
Expand All @@ -47,7 +40,7 @@ func main() {
geoipupdate.WithOutput(args.Output),
)
if err != nil {
fatalLogger("error loading configuration", err)
log.Fatalf("Error loading configuration: %s", err)
}

if config.Verbose {
Expand All @@ -58,6 +51,6 @@ func main() {

client := geoipupdate.NewClient(config)
if err = client.Run(context.Background()); err != nil {
fatalLogger("error retrieving updates", err)
log.Fatalf("Error retrieving updates: %s", err)
}
}
40 changes: 39 additions & 1 deletion cmd/geoipupdate/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,45 @@ package main
import "runtime/debug"

func init() {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" {
info, ok := debug.ReadBuildInfo()
if !ok {
// Getting the build info failed, e.g., it was disabled on build.
return
}
if version == unknownVersion {
// This will set the version on go install ...
version = info.Main.Version
}

var rev, time, arch, os string
dirty := false
for _, kv := range info.Settings {
switch kv.Key {
case "vcs.revision":
rev = kv.Value
case "vcs.time":
time = kv.Value
case "vcs.modified":
dirty = kv.Value == "true"
case "GOARCH":
arch = kv.Value
case "GOOS":
os = kv.Value
}
}

bi := ""

if len(rev) >= 8 {
marselester marked this conversation as resolved.
Show resolved Hide resolved
bi += rev[:8]
if dirty {
bi += "-modified"
}
bi += ", "
}
if time != "" {
bi += time + ", "
}
bi += os + "-" + arch
version += " (" + bi + ")"
}
4 changes: 1 addition & 3 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
FROM alpine:3

RUN apk update && \
apk add jq
RUN adduser -D -h /var/lib/geoipupdate -u 1000 geoipupdate
RUN apk update && apk add jq

COPY geoipupdate /usr/bin/geoipupdate
COPY docker/entry.sh /usr/bin/entry.sh
Expand Down
5 changes: 3 additions & 2 deletions docker/entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ trap 'kill ${!}; term_handler' SIGTERM

pid=0
database_dir=/usr/share/GeoIP
log_dir="/var/lib/geoipupdate"
log_dir="/tmp/geoipupdate"
log_file="$log_dir/.healthcheck"
flags="--output"
frequency=$((GEOIPUPDATE_FREQUENCY * 60 * 60))
export GEOIPUPDATE_CONF_FILE=""

if [ -z "$GEOIPUPDATE_DB_DIR" ]; then
GEOIPUPDATE_DB_DIR="$database_dir"
export GEOIPUPDATE_DB_DIR="$database_dir"
fi

if [ -z "$GEOIPUPDATE_ACCOUNT_ID" ] && [ -z "$GEOIPUPDATE_ACCOUNT_ID_FILE" ]; then
Expand Down
2 changes: 1 addition & 1 deletion docker/healthcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cutoff_duration=$(($GEOIPUPDATE_FREQUENCY * 60 * 60 + 120))
current_time=$(date +%s)
cutoff_date=$(($current_time - $cutoff_duration))

log_file="/var/lib/geoipupdate/.healthcheck"
log_file="/tmp/geoipupdate/.healthcheck"
editions=$(cat "$log_file" | jq -r '.[] | select(.checked_at > '$cutoff_date') | .edition_id')
checked_editions=$(echo "$editions" | wc -l)
desired_editions=$(echo "$GEOIPUPDATE_EDITION_IDS" | awk -F' ' '{print NF}')
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/maxmind/geoipupdate/v5
module github.com/maxmind/geoipupdate/v6

go 1.19

Expand Down
6 changes: 3 additions & 3 deletions pkg/geoipupdate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"
"time"

"github.com/maxmind/geoipupdate/v5/pkg/geoipupdate/vars"
"github.com/maxmind/geoipupdate/v6/pkg/geoipupdate/vars"
)

// Config is a parsed configuration file.
Expand Down Expand Up @@ -274,7 +274,7 @@ func setConfigFromFile(config *Config, path string) error {
}

if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading file: %w", err)
return fmt.Errorf("reading file: %w", err)
}

return nil
Expand Down Expand Up @@ -436,7 +436,7 @@ func parseProxy(
// Now that we have a scheme, we should be able to parse.
u, err := url.Parse(proxyURL)
if err != nil {
return nil, fmt.Errorf("error parsing proxy URL: %w", err)
return nil, fmt.Errorf("parsing proxy URL: %w", err)
}

if !strings.Contains(u.Host, ":") {
Expand Down
2 changes: 1 addition & 1 deletion pkg/geoipupdate/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
"time"

"github.com/maxmind/geoipupdate/v5/pkg/geoipupdate/vars"
"github.com/maxmind/geoipupdate/v6/pkg/geoipupdate/vars"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
16 changes: 8 additions & 8 deletions pkg/geoipupdate/database/http_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (

"github.com/cenkalti/backoff/v4"

"github.com/maxmind/geoipupdate/v5/pkg/geoipupdate/internal"
"github.com/maxmind/geoipupdate/v5/pkg/geoipupdate/vars"
"github.com/maxmind/geoipupdate/v6/pkg/geoipupdate/internal"
"github.com/maxmind/geoipupdate/v6/pkg/geoipupdate/vars"
)

const urlFormat = "%s/geoip/databases/%s/update?db_md5=%s"
Expand Down Expand Up @@ -104,7 +104,7 @@ func (r *HTTPReader) Read(ctx context.Context, editionID, hash string) (*ReadRes
},
)
if err != nil {
return nil, fmt.Errorf("error getting update for %s: %w", editionID, err)
return nil, fmt.Errorf("getting update for %s: %w", editionID, err)
}

return result, nil
Expand All @@ -129,14 +129,14 @@ func (r *HTTPReader) get(

req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
return nil, fmt.Errorf("creating request: %w", err)
}
req.Header.Add("User-Agent", "geoipupdate/"+vars.Version)
req.SetBasicAuth(fmt.Sprintf("%d", r.accountID), r.licenseKey)

response, err := r.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error performing HTTP request: %w", err)
return nil, fmt.Errorf("performing HTTP request: %w", err)
}
// It is safe to close the response body reader as it wouldn't be
// consumed in case this function returns an error.
Expand All @@ -160,7 +160,7 @@ func (r *HTTPReader) get(
Body: string(buf),
StatusCode: response.StatusCode,
}
return nil, fmt.Errorf("unexpcted HTTP status code: %w", httpErr)
return nil, fmt.Errorf("unexpected HTTP status code: %w", httpErr)
}

newHash := response.Header.Get("X-Database-MD5")
Expand All @@ -170,7 +170,7 @@ func (r *HTTPReader) get(

modifiedAt, err := parseTime(response.Header.Get("Last-Modified"))
if err != nil {
return nil, fmt.Errorf("error reading Last-Modified header: %w", err)
return nil, fmt.Errorf("reading Last-Modified header: %w", err)
}

gzReader, err := gzip.NewReader(response.Body)
Expand All @@ -196,7 +196,7 @@ func (r *HTTPReader) get(
func parseTime(s string) (time.Time, error) {
t, err := time.ParseInLocation(time.RFC1123, s, time.UTC)
if err != nil {
return time.Time{}, fmt.Errorf("error parsing time: %w", err)
return time.Time{}, fmt.Errorf("parsing time: %w", err)
}

return t, nil
Expand Down
Loading
Loading