1

Add basic context with config creation

This commit is contained in:
Vladimir Hodakov 2019-09-14 18:51:32 +04:00
parent bce5cef340
commit afc2a73ed6
Signed by: Vladimir Hodakov
GPG Key ID: 673980B6882F82C6
6 changed files with 178 additions and 3 deletions

7
go.mod
View File

@ -1,3 +1,10 @@
module gitlab.com/fat0troll/uploader_tools
go 1.13
require (
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
github.com/rs/zerolog v1.15.0
github.com/stretchr/testify v1.4.0 // indirect
gitlab.com/pztrn/flagger v0.0.0-20190122123836-d429d7149cc9
)

27
go.sum Normal file
View File

@ -0,0 +1,27 @@
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.15.0 h1:uPRuwkWF4J6fGsJ2R0Gn2jB1EQiav9k3S6CSdygQJXY=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
gitlab.com/pztrn/flagger v0.0.0-20190122123836-d429d7149cc9 h1:qLlIZybSEr3MSCaE+cJZpT6O/1q3IsBPV7GcZJc05vs=
gitlab.com/pztrn/flagger v0.0.0-20190122123836-d429d7149cc9/go.mod h1:2cxOO1JFwNeKmT/icFNkUOPzQnG+qeMXURKeVFsuQA8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

11
internal/config/config.go Normal file
View File

@ -0,0 +1,11 @@
// NNM-Club torrent filess mass downloader
// Created for Uploaders group
// Copyright (c) 2012-2019 Vladimir "fat0troll" Hodakov
package config
type Config struct {
Username string `json:"username"`
Password string `json:"password"`
URL string `json:"url"`
}

View File

@ -0,0 +1,97 @@
// NNM-Club torrent filess mass downloader
// Created for Uploaders group
// Copyright (c) 2012-2019 Vladimir "fat0troll" Hodakov
package context
import (
"encoding/json"
"fmt"
"github.com/kirsle/configdir"
"github.com/rs/zerolog"
"gitlab.com/pztrn/flagger"
"log"
"os"
"path/filepath"
"runtime"
)
// getMemoryUsage returns memory usage for logger.
func (c *Context) getMemoryUsage(e *zerolog.Event, level zerolog.Level, message string) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
e.Str("memalloc", fmt.Sprintf("%dMB", m.Alloc/1024/1024))
e.Str("memsys", fmt.Sprintf("%dMB", m.Sys/1024/1024))
e.Str("numgc", fmt.Sprintf("%d", m.NumGC))
}
// initFlagger initializes flags parser
func (c *Context) initFlagger() {
c.Flagger = flagger.New("NNM-Club mass torrent files downloader", flagger.LoggerInterface(log.New(os.Stdout, "", log.Lshortfile)))
c.Flagger.Initialize()
}
func (c *Context) readConfig() {
fh, err := os.Open(c.configFilePath)
if err != nil {
dlog.Fatal().Err(err).Msg("Failed to read config file")
}
defer fh.Close()
decoder := json.NewDecoder(fh)
err = decoder.Decode(&c.Config)
if err != nil {
dlog.Fatal().Err(err).Msg("Failed to decode config")
}
}
// Init is an initialization function for core context
// Without these parts of the application we can't start at all
func (c *Context) Init() {
c.initFlagger()
c.Logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout}).With().Timestamp().Logger()
c.Logger = c.Logger.Hook(zerolog.HookFunc(c.getMemoryUsage))
dlog = c.Logger.With().Str("domain", "context").Logger()
}
func (c *Context) InitConfig() {
configPath := configdir.LocalConfig("uploader_tools")
err := configdir.MakePath(configPath)
if err != nil {
dlog.Fatal().Err(err).Str("config directory", configPath).Msg("Failed to obtain config path")
}
dlog.Debug().Str("config directory", configPath).Msg("Got config directory")
configFile := filepath.Join(configPath, "settings.json")
c.configFilePath = configFile
if _, err = os.Stat(configFile); os.IsNotExist(err) {
// Generating new config on first run
dlog.Debug().Msg("Generating new config")
c.SaveConfig()
} else {
dlog.Debug().Msg("Using existing config")
c.readConfig()
}
}
func (c *Context) SaveConfig() {
fh, err := os.Create(c.configFilePath)
if err != nil {
dlog.Fatal().Err(err).Msg("Failed to create config file")
}
defer fh.Close()
encoder := json.NewEncoder(fh)
err = encoder.Encode(&c.Config)
if err != nil {
dlog.Fatal().Err(err).Msg("Failed to encode config")
}
}

View File

@ -0,0 +1,30 @@
// NNM-Club torrent filess mass downloader
// Created for Uploaders group
// Copyright (c) 2012-2019 Vladimir "fat0troll" Hodakov
package context
import (
"github.com/rs/zerolog"
"gitlab.com/fat0troll/uploader_tools/internal/config"
"gitlab.com/pztrn/flagger"
)
var (
dlog zerolog.Logger
)
// Context is the main application context.
type Context struct {
configFilePath string
Config config.Config
Flagger *flagger.Flagger
Logger zerolog.Logger
}
// New creates new Context
func New() *Context {
c := &Context{}
return c
}

View File

@ -1,13 +1,16 @@
// NNM-Club torrent filess mass downloader
// Created for Uploaders group
// (c) Vladimir Hodakov, 2012-2019
// Copyright (c) 2012-2019 Vladimir "fat0troll" Hodakov
package main
import (
"fmt"
"gitlab.com/fat0troll/uploader_tools/internal/context"
)
func main() {
fmt.Println("TO BE FILLED")
c := context.New()
c.Init()
c.Logger.Info().Msg("NNM-Club mass torrent files downloader is starting")
c.InitConfig()
}