"Fossies" - the Fresh Open Source Software Archive

Member "geoipupdate-4.3.0/cmd/geoipupdate/main.go" (16 Apr 2020, 1956 Bytes) of package /linux/misc/geoipupdate-4.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "main.go": 4.2.2_vs_4.3.0.

    1 package main
    2 
    3 import (
    4     "fmt"
    5     "log"
    6     "net/http"
    7     "os"
    8     "path/filepath"
    9 
   10     "github.com/maxmind/geoipupdate/v4/pkg/geoipupdate"
   11     "github.com/maxmind/geoipupdate/v4/pkg/geoipupdate/database"
   12     "github.com/pkg/errors"
   13 )
   14 
   15 var (
   16     version                  = "unknown"
   17     defaultConfigFile        string
   18     defaultDatabaseDirectory string
   19 )
   20 
   21 func main() {
   22     log.SetFlags(0)
   23 
   24     if defaultConfigFile == "" {
   25         defaultConfigFile = geoipupdate.DefaultConfigFile
   26     }
   27     if defaultDatabaseDirectory == "" {
   28         defaultDatabaseDirectory = geoipupdate.DefaultDatabaseDirectory
   29     }
   30 
   31     args := getArgs()
   32     fatalLogger := func(message string, err error) {
   33         if args.StackTrace {
   34             log.Print(fmt.Sprintf("%s: %+v", message, err))
   35         } else {
   36             log.Print(fmt.Sprintf("%s: %s", message, err))
   37         }
   38         os.Exit(1)
   39     }
   40 
   41     config, err := geoipupdate.NewConfig(args.ConfigFile, defaultDatabaseDirectory, args.DatabaseDirectory, args.Verbose)
   42     if err != nil {
   43         fatalLogger(fmt.Sprintf("error loading configuration file %s", args.ConfigFile), err)
   44     }
   45 
   46     if config.Verbose {
   47         log.Printf("Using config file %s", args.ConfigFile)
   48         log.Printf("Using database directory %s", config.DatabaseDirectory)
   49     }
   50 
   51     client := geoipupdate.NewClient(config)
   52 
   53     if err = run(client, config); err != nil {
   54         fatalLogger("error retrieving updates", err)
   55     }
   56 }
   57 
   58 func run(client *http.Client, config *geoipupdate.Config) error {
   59     dbReader := database.NewHTTPDatabaseReader(client, config)
   60 
   61     for _, editionID := range config.EditionIDs {
   62         filename, err := geoipupdate.GetFilename(config, editionID, client)
   63         if err != nil {
   64             return errors.Wrap(err, "error retrieving filename")
   65         }
   66         filePath := filepath.Join(config.DatabaseDirectory, filename)
   67         dbWriter, err := database.NewLocalFileDatabaseWriter(filePath, config.LockFile, config.Verbose)
   68         if err != nil {
   69             return errors.Wrap(err, "error creating database writer")
   70         }
   71         if err := dbReader.Get(dbWriter, editionID); err != nil {
   72             return err
   73         }
   74     }
   75     return nil
   76 }