From 5257e6697b4a6c702a603d539a83901e43eebf74 Mon Sep 17 00:00:00 2001 From: burak Date: Thu, 31 Oct 2019 14:30:51 +0100 Subject: [PATCH 1/8] config log, paths cross compile support --- cmd/getsum/main.go | 2 +- generateReleases.sh | 11 +++++--- go.mod | 1 + go.sum | 2 ++ internal/config/parser.go | 38 ++++++++++++++++++++-------- internal/file/file.go | 2 +- internal/provider/providerfactory.go | 2 +- 7 files changed, 41 insertions(+), 17 deletions(-) diff --git a/cmd/getsum/main.go b/cmd/getsum/main.go index 2a44ae6..9a9bec0 100644 --- a/cmd/getsum/main.go +++ b/cmd/getsum/main.go @@ -31,7 +31,7 @@ func main() { //if user defined set log level logger.SetLevel(*config.LogLevel) - logger.Debug("Application started, using config %v", *config) + logger.Debug("Application started, using config %v", parser.ConfigJson) //check if user wants to run in listen mode if *config.Serve { diff --git a/generateReleases.sh b/generateReleases.sh index f1dcea2..ce38af2 100755 --- a/generateReleases.sh +++ b/generateReleases.sh @@ -1,10 +1,13 @@ #!/bin/sh - for GOOS in darwin linux windows openbsd freebsd ; do for GOARCH in 386 amd64; do - GO_BIN_PATH="builds/$GOOS/$GOARCH" + GO_BIN_PATH="builds/$GOOS/$GOARCH" mkdir -p $GO_BIN_PATH - go build -v -o $GO_BIN_PATH/getsum cmd/getsum/main.go - tar -czvf $GO_BIN_PATH/getsum-$GOOS-$GOARCH.tar.gz $GO_BIN_PATH/getsum + EXTENSION="" + if [[ "$GOOS" == "windows" ]];then + EXTENSION=".exe" + fi + time GOOS=$GOOS GOARCH=$GOARCH go build -v -o ./$GO_BIN_PATH/getsum$EXTENSION ./cmd/getsum/main.go + tar -czvf ./$GO_BIN_PATH/getsum-$GOOS-$GOARCH.tar.gz ./$GO_BIN_PATH/ done done diff --git a/go.mod b/go.mod index ccfe2ab..e9d4713 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.13 require ( github.com/logrusorgru/aurora v0.0.0-20191017060258-dc85c304c434 + github.com/mitchellh/go-homedir v1.1.0 golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 gopkg.in/yaml.v2 v2.2.4 ) diff --git a/go.sum b/go.sum index 986a574..58037c8 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/logrusorgru/aurora v0.0.0-20191017060258-dc85c304c434 h1:im9kkmH0WWwx github.com/logrusorgru/aurora v0.0.0-20191017060258-dc85c304c434/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y= github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/nsf/termbox-go v0.0.0-20190817171036-93860e161317 h1:hhGN4SFXgXo61Q4Sjj/X9sBjyeSa2kdpaOzCO+8EVQw= github.com/nsf/termbox-go v0.0.0-20190817171036-93860e161317/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/internal/config/parser.go b/internal/config/parser.go index 84cc799..96bce85 100644 --- a/internal/config/parser.go +++ b/internal/config/parser.go @@ -1,12 +1,14 @@ package config import ( + "encoding/json" "flag" "fmt" "io/ioutil" "os" "strings" + "github.com/mitchellh/go-homedir" "gopkg.in/yaml.v2" ) @@ -24,25 +26,35 @@ const ( defaultSupplier = "go" ) -var defaultConfig string = ".getsum/servers.yml" +var defaultConfig string = ".getsum" + string(os.PathSeparator) + "servers.yml" +var ConfigJson string //checks first if user specified a config file via params //if not then checks $HOME/.getsum/servers.yml file if exist //if yes attempts to parse it and set servers part of configuration func parseYaml(config *Config) error { + if *config.Dir == "" { + currentDir, err := os.Getwd() + if err != nil { + return err + } + config.Dir = ¤tDir + } if *config.ServerConfig == "" { //no config present check home folder - home := os.Getenv("HOME") - if home != "" { - homeConfig := strings.Join([]string{home, defaultConfig}, "/") - _, err := os.Stat(homeConfig) - if os.IsNotExist(err) { - return nil - } + home, err := homedir.Dir() + if err != nil { + return err + } + homeConfig := strings.Join([]string{home, defaultConfig}, string(os.PathSeparator)) + _, err = os.Stat(homeConfig) + if err != nil { + return nil + } else { config.ServerConfig = &homeConfig - } } + //read the file yamlFile, err := ioutil.ReadFile(*config.ServerConfig) if err != nil { @@ -89,7 +101,7 @@ func ParseConfig() (*Config, error) { c.All = flag.Bool("all", false, "Run all algorithms (MD5,SHA1 , SHA256 ...) for each running client") c.Key = flag.String("key", defaultKey, "Key for blake2 hashing") flag.StringVar(c.Key, "k", defaultKey, "shorthand of -key") - c.Dir = flag.String("dir", ".", "Default folder to save files, default is current folder") + c.Dir = flag.String("dir", "", "Default folder to save files, default is current folder") c.Supplier = flag.String("lib", defaultSupplier, "Algorithm lib default is [GO] that core golang libraries used, if you want to use unix, win, mac default apps set to [OS], for openssl set [openssl]") c.Keep = flag.Bool("keep", false, "If there is a checksum provided to validate and doesnt match with calculated results still keep the file") var empty string = "" @@ -123,5 +135,11 @@ func ParseConfig() (*Config, error) { } + configBytes, err := json.Marshal(*c) + if err != nil { + return nil, err + } + ConfigJson = string(configBytes) + return c, nil } diff --git a/internal/file/file.go b/internal/file/file.go index b3f233a..b4efe78 100644 --- a/internal/file/file.go +++ b/internal/file/file.go @@ -115,7 +115,7 @@ func fetchRemote(f *File, timeout int) error { defer mux.Unlock() filename := path.Base(f.Url) if f.StoragePath != "" { - filename = strings.Join([]string{f.StoragePath, filename}, "/") + filename = strings.Join([]string{f.StoragePath, filename}, string(os.PathSeparator)) } if fetchedSize > 0 { //another process already fetched f.Size = fetchedSize diff --git a/internal/provider/providerfactory.go b/internal/provider/providerfactory.go index a464fa5..1d8ebc1 100644 --- a/internal/provider/providerfactory.go +++ b/internal/provider/providerfactory.go @@ -149,7 +149,7 @@ func getLocalProviders(config *Config, factory ISupplierFactory) ([]*Provider, e } } if !supports { - logger.Warn(fmt.Sprintf("Algorithm %s not supported for local provider using %s libraries", a.Name(), *config.Supplier)) + logger.Info(fmt.Sprintf("Algorithm %s not supported for local provider using %s libraries", a.Name(), *config.Supplier)) continue } l := getProvider(Local, supplier, config, a) From ee1b087ecc804eeeca2a1c83e579f605c7bd3db7 Mon Sep 17 00:00:00 2001 From: burak Date: Thu, 31 Oct 2019 14:47:51 +0100 Subject: [PATCH 2/8] SHA224 not supported by certutil --- internal/supplier/commandsupplier.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/supplier/commandsupplier.go b/internal/supplier/commandsupplier.go index 6d982e1..38f233e 100644 --- a/internal/supplier/commandsupplier.go +++ b/internal/supplier/commandsupplier.go @@ -18,7 +18,7 @@ const ( OPENSSL ) -var unixAlgos []Algorithm = []Algorithm{MD5, SHA1, SHA224, SHA256, SHA384, SHA512} +var unixAlgos []Algorithm = []Algorithm{MD5, SHA1, SHA256, SHA384, SHA512} var winAlgos []Algorithm = []Algorithm{MD2, MD4, MD5, SHA1, SHA224, SHA256, SHA384, SHA512} var openSSLAlgos []Algorithm = []Algorithm{ From dc03859046097636db3368306ecc02512a9203b1 Mon Sep 17 00:00:00 2001 From: burak Date: Thu, 31 Oct 2019 14:56:01 +0100 Subject: [PATCH 3/8] certutil second line is hash --- internal/supplier/commandsupplier.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/supplier/commandsupplier.go b/internal/supplier/commandsupplier.go index 38f233e..880eb89 100644 --- a/internal/supplier/commandsupplier.go +++ b/internal/supplier/commandsupplier.go @@ -18,8 +18,8 @@ const ( OPENSSL ) -var unixAlgos []Algorithm = []Algorithm{MD5, SHA1, SHA256, SHA384, SHA512} -var winAlgos []Algorithm = []Algorithm{MD2, MD4, MD5, SHA1, SHA224, SHA256, SHA384, SHA512} +var unixAlgos []Algorithm = []Algorithm{MD5, SHA1, SHA224, SHA256, SHA384, SHA512} +var winAlgos []Algorithm = []Algorithm{MD2, MD4, MD5, SHA1, SHA256, SHA384, SHA512} var openSSLAlgos []Algorithm = []Algorithm{ MD4, @@ -125,6 +125,8 @@ func (s *CommandSupplier) Run() { s.status.Value = fmt.Sprintf("%dms", took.Milliseconds()) if s.Type == OPENSSL { s.status.Checksum = strings.Fields(val)[1] + } else if s.Type == WINDOWS { + s.status.Checksum = strings.Split(val, "\n")[1] } else { s.status.Checksum = strings.Fields(val)[0] } From cd263192a071ab38a633d7f10a125bdb0e328ef5 Mon Sep 17 00:00:00 2001 From: burak Date: Thu, 31 Oct 2019 16:18:59 +0100 Subject: [PATCH 4/8] gen script update --- generateReleases.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/generateReleases.sh b/generateReleases.sh index ce38af2..4cd4a6d 100755 --- a/generateReleases.sh +++ b/generateReleases.sh @@ -7,7 +7,10 @@ for GOOS in darwin linux windows openbsd freebsd ; do if [[ "$GOOS" == "windows" ]];then EXTENSION=".exe" fi - time GOOS=$GOOS GOARCH=$GOARCH go build -v -o ./$GO_BIN_PATH/getsum$EXTENSION ./cmd/getsum/main.go - tar -czvf ./$GO_BIN_PATH/getsum-$GOOS-$GOARCH.tar.gz ./$GO_BIN_PATH/ + time GOOS=$GOOS GOARCH=$GOARCH go build -a -v -o ./$GO_BIN_PATH/getsum$EXTENSION ./cmd/getsum/main.go + TAR_FILE=$(pwd)/$GO_BIN_PATH/getsum-$GOOS-$GOARCH.tar.gz + tar -czvf $TAR_FILE $GO_BIN_PATH done done + +tree -h builds From e357da7ab3a530f5ded96bfb4d1bfe612f8e5b34 Mon Sep 17 00:00:00 2001 From: burak Date: Thu, 31 Oct 2019 16:19:35 +0100 Subject: [PATCH 5/8] reset everything also if content length empty return meaningful message --- internal/file/file.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/file/file.go b/internal/file/file.go index b4efe78..961f321 100644 --- a/internal/file/file.go +++ b/internal/file/file.go @@ -61,6 +61,13 @@ func (f *File) IsRemote() bool { //a bit of ugly but resets global variable //check variable comment for details func (f *File) Reset() { + f.Url = "" + f.Size = 0 + f.path = "" + f.data = nil + f.Proxy = "" + f.StoragePath = "" + f.Status = nil fetchedSize = -1 } @@ -143,7 +150,7 @@ func fetchRemote(f *File, timeout int) error { defer header.Body.Close() size, err := strconv.Atoi(header.Header.Get("Content-Length")) if err != nil { - return err + return errors.New("Can not get content length, is this a binary?") } f.Size = int64(size) f.path = filename From 9a64e1f2bef670a2745b989acfc4817dfdcea14a Mon Sep 17 00:00:00 2001 From: burak Date: Thu, 31 Oct 2019 16:19:51 +0100 Subject: [PATCH 6/8] use suppliers from cache for server mode --- internal/provider/localprovider.go | 2 +- internal/supplier/commandsupplier.go | 4 ++ internal/supplier/gosupplier.go | 4 ++ internal/supplier/supplier.go | 1 + internal/supplier/supplierfactory.go | 55 +++++++++++++++++++++------- 5 files changed, 52 insertions(+), 14 deletions(-) diff --git a/internal/provider/localprovider.go b/internal/provider/localprovider.go index 0574dd2..30a8de1 100644 --- a/internal/provider/localprovider.go +++ b/internal/provider/localprovider.go @@ -66,7 +66,7 @@ func (l *LocalProvider) Terminate() error { //collect status from running process and returns func (l *LocalProvider) Status() *status.Status { stat := l.Supplier.Status() - logger.Trace("%s Returning status %v", l.Name, *stat) + logger.Trace("%s Returning status %v address %v supplier: %v ", l.Name, *stat, stat, l.Supplier) return stat } diff --git a/internal/supplier/commandsupplier.go b/internal/supplier/commandsupplier.go index 880eb89..e23097d 100644 --- a/internal/supplier/commandsupplier.go +++ b/internal/supplier/commandsupplier.go @@ -164,6 +164,10 @@ func (s *CommandSupplier) Terminate() error { return err } +func (s *CommandSupplier) Data() *BaseSupplier { + return &s.BaseSupplier +} + //returns i.e. openssl dgst -sha512 /file/path func getSSLCommand(algo Algorithm, path string) *exec.Cmd { algorithm := strings.ToLower(algo.Name()) diff --git a/internal/supplier/gosupplier.go b/internal/supplier/gosupplier.go index b7c9cd1..56c18af 100644 --- a/internal/supplier/gosupplier.go +++ b/internal/supplier/gosupplier.go @@ -121,6 +121,10 @@ func (s *GoSupplier) Terminate() error { } +func (s *GoSupplier) Data() *BaseSupplier { + return &s.BaseSupplier +} + //calculates given file and returns checksum func calculate(hash hash.Hash, status chan string, file *file.File) { f, _ := os.Open(file.Path()) diff --git a/internal/supplier/supplier.go b/internal/supplier/supplier.go index ebdf81b..28df9aa 100644 --- a/internal/supplier/supplier.go +++ b/internal/supplier/supplier.go @@ -12,6 +12,7 @@ type Supplier interface { Terminate() error Supports() []Algorithm Delete() + Data() *BaseSupplier } //embedded struct for suppliers diff --git a/internal/supplier/supplierfactory.go b/internal/supplier/supplierfactory.go index 182ff7e..4a6d379 100644 --- a/internal/supplier/supplierfactory.go +++ b/internal/supplier/supplierfactory.go @@ -25,26 +25,45 @@ func (factory *SupplierFactory) GetSupplierByAlgo(config *Config, algorithm *Alg } +var cache map[string]Supplier = make(map[string]Supplier) + //creates supplier instance func getSupplierInstance(config *Config, algo *Algorithm) Supplier { if *config.Supplier == "go" { - s := &GoSupplier{} - setFields(&s.BaseSupplier, *algo, config) + s, ok := cache["go"+string(*algo)] + if !ok { + s = &GoSupplier{} + cache["go"+string(*algo)] = s + + } + setFields(s.Data(), *algo, config) return s } else if *config.Supplier == "openssl" { - s := &CommandSupplier{Type: OPENSSL} - setFields(&s.BaseSupplier, *algo, config) - return s + s, ok := cache["openssl"+string(*algo)] + if !ok { + s = &CommandSupplier{Type: OPENSSL} + cache["openssl"+string(*algo)] = s + } + setFields(s.Data(), *algo, config) + return s } switch runtime.GOOS { case "linux", "mac": - s := &CommandSupplier{Type: UNIX} - setFields(&s.BaseSupplier, *algo, config) + s, ok := cache["mac"+string(*algo)] + if !ok { + s = &CommandSupplier{Type: UNIX} + cache["mac"+string(*algo)] = s + } + setFields(s.Data(), *algo, config) return s case "windows": - s := &CommandSupplier{Type: WINDOWS} - setFields(&s.BaseSupplier, *algo, config) + s, ok := cache["windows"+string(*algo)] + if !ok { + s = &CommandSupplier{Type: WINDOWS} + cache["windows"+string(*algo)] = s + } + setFields(s.Data(), *algo, config) return s default: return nil @@ -54,12 +73,22 @@ func getSupplierInstance(config *Config, algo *Algorithm) Supplier { //utility to set fields func setFields(base *BaseSupplier, algo Algorithm, config *Config) { - stat := &status.Status{status.PREPARED, "", ""} + if base.status == nil { + base.status = &status.Status{} + } + base.status.Type = status.PREPARED + base.status.Value = "" + base.status.Checksum = "" base.Algorithm = algo base.Key = *config.Key - base.File = &File{Url: *config.File, Status: stat, Proxy: *config.Proxy, StoragePath: *config.Dir} - base.TimeOut = *config.Timeout - base.status = stat + if base.File == nil { + base.File = &File{} + } base.File.Reset() + base.File.Url = *config.File + base.File.Status = base.status + base.File.Proxy = *config.Proxy + base.File.StoragePath = *config.Dir + base.TimeOut = *config.Timeout } From 2dac56056a6079c319c65632fada2571da21e028 Mon Sep 17 00:00:00 2001 From: burak Date: Thu, 31 Oct 2019 18:20:51 +0100 Subject: [PATCH 7/8] memory cleanup --- cmd/getsum/main.go | 5 ++-- internal/provider/onpremiseprovider.go | 41 ++++++++++++++++---------- internal/provider/providerfactory.go | 14 +++------ internal/servers/onpremise.go | 8 +++-- internal/supplier/supplierfactory.go | 39 ++++++++++++++++++------ internal/validation/validator.go | 4 +++ 6 files changed, 72 insertions(+), 39 deletions(-) diff --git a/cmd/getsum/main.go b/cmd/getsum/main.go index 9a9bec0..772a7e1 100644 --- a/cmd/getsum/main.go +++ b/cmd/getsum/main.go @@ -107,10 +107,11 @@ func checkMismatch(providers *Providers, config *parser.Config) { logger.Logsum(providers) //check if user still wants to keep the file if !*config.Keep { - logger.Debug("Exiting application no keep setted") providers.Delete() - os.Exit(1) } + logger.Debug("Exiting application no keep setted") + os.Exit(1) + } else { //all good print one last time statuses logger.Status(providers) diff --git a/internal/provider/onpremiseprovider.go b/internal/provider/onpremiseprovider.go index 85b56ff..45af36d 100644 --- a/internal/provider/onpremiseprovider.go +++ b/internal/provider/onpremiseprovider.go @@ -20,7 +20,8 @@ type RemoteProvider struct { client *http.Client config *config.Config address string - ErrorStatus *status.Status + status *status.Status + hasRunError bool } //notifies server to run @@ -35,20 +36,20 @@ func (l *RemoteProvider) Run() { } //utility to create a status struct with given value -func getErrorStatus(err error) *status.Status { - stat := &status.Status{} - stat.Type = status.ERROR - stat.Value = err.Error() - return stat +func setErrorStatus(err error, r *RemoteProvider) { + r.status.Type = status.ERROR + r.status.Value = err.Error() } //send request to server to start running func remoteRun(l *RemoteProvider) { + l.status = &status.Status{} //parse config to json body, err := json.Marshal(*l.config) if err != nil { - l.ErrorStatus = getErrorStatus(err) + setErrorStatus(err, l) + l.hasRunError = true return } @@ -58,11 +59,20 @@ func remoteRun(l *RemoteProvider) { if err != nil { //set error as provider status //status() method will handle - l.ErrorStatus = getErrorStatus(err) + setErrorStatus(err, l) + l.hasRunError = true return } + decoder := json.NewDecoder(resp.Body) + err = decoder.Decode(l.status) + if err != nil { + setErrorStatus(err, l) + l.hasRunError = true + } + l.hasRunError = l.status.Type == status.ERROR + } //utility to close response if present @@ -77,19 +87,20 @@ func remoteStatus(l *RemoteProvider) *status.Status { //reach the server resp, err := l.client.Get(l.address) if err != nil { - return getErrorStatus(err) + setErrorStatus(err, l) + return l.status } defer closeResponse(resp) //parse response decoder := json.NewDecoder(resp.Body) - status := &status.Status{} - err = decoder.Decode(status) + err = decoder.Decode(l.status) if err != nil { - return getErrorStatus(err) + setErrorStatus(err, l) + return l.status } - return status + return l.status } //trigger termination on remote server using http DELETE @@ -150,8 +161,8 @@ func (l *RemoteProvider) Status() *status.Status { var stat *status.Status //check if this provided already faced an error //if so dont bother raching to server - if l.ErrorStatus != nil { - stat = l.ErrorStatus + if l.hasRunError { + stat = l.status } else { stat = remoteStatus(l) } diff --git a/internal/provider/providerfactory.go b/internal/provider/providerfactory.go index 1d8ebc1..fa6c33a 100644 --- a/internal/provider/providerfactory.go +++ b/internal/provider/providerfactory.go @@ -1,7 +1,6 @@ package providers import ( - "fmt" "net/http" "net/url" "strings" @@ -141,17 +140,12 @@ func getLocalProviders(config *Config, factory ISupplierFactory) ([]*Provider, e logger.Debug("User requests all algos to be runned") for _, a := range algos { logger.Debug("Creating local provider for algorithm %s", a.Name()) - supplier := factory.GetSupplierByAlgo(config, &a) - supports := false - for _, supportedAlgo := range supplier.Supports() { - if supportedAlgo == a { - supports = true - } - } - if !supports { - logger.Info(fmt.Sprintf("Algorithm %s not supported for local provider using %s libraries", a.Name(), *config.Supplier)) + supplier, err := factory.GetSupplierByAlgo(config, &a) + if err != nil { + logger.Warn(err.Error()) continue } + l := getProvider(Local, supplier, config, a) logger.Debug("Generated provider: %v", l) locals = append(locals, &l) diff --git a/internal/servers/onpremise.go b/internal/servers/onpremise.go index d7874dd..a33c6a7 100644 --- a/internal/servers/onpremise.go +++ b/internal/servers/onpremise.go @@ -91,12 +91,14 @@ func handlePost(s *OnPremiseServer, w http.ResponseWriter, r *http.Request) { //get supplier instance, only single algo supported on server mode var algorithm = ValueOf(&config.Algorithm[0]) config.Dir = &s.StoragePath - s.Supplier = factory.GetSupplierByAlgo(config, &algorithm) + s.Supplier, err = factory.GetSupplierByAlgo(config, &algorithm) //something went wrong, TODO add error handler - if s.Supplier == nil { - handleError("Can not create algorithm runner instance", w) + if err != nil { + handleError("Can not create algorithm runner instance: "+err.Error(), w) + return } go s.Supplier.Run() + handleGet(s, w, r) logger.Info("Process started") } diff --git a/internal/supplier/supplierfactory.go b/internal/supplier/supplierfactory.go index 4a6d379..29d58f3 100644 --- a/internal/supplier/supplierfactory.go +++ b/internal/supplier/supplierfactory.go @@ -1,6 +1,8 @@ package supplier import ( + "errors" + "fmt" "runtime" . "github.com/getsumio/getsum/internal/config" @@ -10,7 +12,7 @@ import ( //reads the config and returns realted supplier type ISupplierFactory interface { - GetSupplierByAlgo(config *Config, algorithm *Algorithm) Supplier + GetSupplierByAlgo(config *Config, algorithm *Algorithm) (Supplier, error) } //factory struct @@ -19,16 +21,34 @@ type SupplierFactory struct { //returns supplier instance for the given algo and lib //i.e. for -lib go -a MD5 it will return GoSupplier to calculate MD5 -func (factory *SupplierFactory) GetSupplierByAlgo(config *Config, algorithm *Algorithm) Supplier { +func (factory *SupplierFactory) GetSupplierByAlgo(config *Config, algorithm *Algorithm) (Supplier, error) { - return getSupplierInstance(config, algorithm) + supplier, err := getSupplierInstance(config, algorithm) + if err != nil { + return nil, err + } + if !isSupplierSupportsAlgorithm(supplier, algorithm) { + msg := fmt.Sprintf("Algorithm %s not supported for provider using %s libraries", algorithm.Name(), *config.Supplier) + return nil, errors.New(msg) + } + return supplier, nil } var cache map[string]Supplier = make(map[string]Supplier) +func isSupplierSupportsAlgorithm(supplier Supplier, algo *Algorithm) bool { + supports := false + for _, supportedAlgo := range supplier.Supports() { + if supportedAlgo == *algo { + supports = true + } + } + return supports +} + //creates supplier instance -func getSupplierInstance(config *Config, algo *Algorithm) Supplier { +func getSupplierInstance(config *Config, algo *Algorithm) (Supplier, error) { if *config.Supplier == "go" { s, ok := cache["go"+string(*algo)] if !ok { @@ -37,7 +57,7 @@ func getSupplierInstance(config *Config, algo *Algorithm) Supplier { } setFields(s.Data(), *algo, config) - return s + return s, nil } else if *config.Supplier == "openssl" { s, ok := cache["openssl"+string(*algo)] if !ok { @@ -46,7 +66,7 @@ func getSupplierInstance(config *Config, algo *Algorithm) Supplier { } setFields(s.Data(), *algo, config) - return s + return s, nil } switch runtime.GOOS { case "linux", "mac": @@ -56,7 +76,7 @@ func getSupplierInstance(config *Config, algo *Algorithm) Supplier { cache["mac"+string(*algo)] = s } setFields(s.Data(), *algo, config) - return s + return s, nil case "windows": s, ok := cache["windows"+string(*algo)] if !ok { @@ -64,9 +84,10 @@ func getSupplierInstance(config *Config, algo *Algorithm) Supplier { cache["windows"+string(*algo)] = s } setFields(s.Data(), *algo, config) - return s + return s, nil default: - return nil + msg := fmt.Sprintf("Unsupported library: %s", *config.Supplier) + return nil, errors.New(msg) } } diff --git a/internal/validation/validator.go b/internal/validation/validator.go index f64589c..e0c699e 100644 --- a/internal/validation/validator.go +++ b/internal/validation/validator.go @@ -56,5 +56,9 @@ func ValidateConfig(config *Config, onPremise bool) error { return errors.New("You specified -tlscert but not -tlskey, both parameter required for https/tls mode") } + if *config.Key != "" && *config.Supplier != "go" { + return errors.New("-key parameter only used on go libs currently, set -lib go") + } + return nil } From aa5fac246172d8b5cd86daf6fe4fe01a3c946e07 Mon Sep 17 00:00:00 2001 From: burak Date: Thu, 31 Oct 2019 18:35:15 +0100 Subject: [PATCH 8/8] exit 1 only if mismatch or termination, QUITE log level --- internal/logger/logger.go | 24 +++++++++++++++++++----- internal/provider/types/providers.go | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 584b968..438ddd5 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -14,11 +14,12 @@ import ( ) const ( - LevelTrace = 0 - LevelDebug = 1 - LevelInfo = 2 - LevelWarn = 3 - LevelError = 4 + LevelTrace = iota + LevelDebug + LevelInfo + LevelWarn + LevelError + LevelQuite ) const ( @@ -27,6 +28,7 @@ const ( info = "INFO" warn = "WARNING" err = "ERROR" + quite = "QUITE" PADDING = "\t" ) @@ -46,6 +48,8 @@ func SetLevel(level string) { Level = LevelWarn case err: Level = LevelError + case quite: + Level = LevelQuite default: log.Fatal("Given log level not understood!") } @@ -84,6 +88,9 @@ func Trace(msg string, params ...interface{}) { //prints headers of running processes func Header(providers *Providers) { + if Level == LevelQuite { + return + } printHeader(providers, 0) } @@ -120,6 +127,10 @@ func Info(msg string, params ...interface{}) { func Logsum(providers *Providers) { fmt.Println("\n\n") for i, s := range providers.Statuses { + if Level == LevelQuite { + fmt.Println(s.Checksum) + return + } var c color.Value var val string @@ -148,6 +159,9 @@ var currentColumn int //if all processes finished in current row //prints new one func Status(providers *Providers) { + if Level == LevelQuite { + return + } stats := providers.Status() printStatus(stats, providers, currentColumn) var anyRunner bool diff --git a/internal/provider/types/providers.go b/internal/provider/types/providers.go index 2808ebf..45d281d 100644 --- a/internal/provider/types/providers.go +++ b/internal/provider/types/providers.go @@ -77,7 +77,7 @@ func (providers *Providers) Status() []*status.Status { func (providers *Providers) HasError() bool { for _, stat := range providers.Statuses { - if stat.Type > status.COMPLETED { + if stat.Type > status.SUSPENDED && stat.Type != status.VALIDATED { return true } }