Skip to content

Commit

Permalink
Merge pull request #32 from getsumio/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
getsumio authored Oct 31, 2019
2 parents d8d8233 + 586fb19 commit f94d639
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 76 deletions.
7 changes: 4 additions & 3 deletions cmd/getsum/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions generateReleases.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#!/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 -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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
38 changes: 28 additions & 10 deletions internal/config/parser.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package config

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v2"
)

Expand All @@ -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 = &currentDir
}
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 {
Expand Down Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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
}
11 changes: 9 additions & 2 deletions internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -115,7 +122,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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 19 additions & 5 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import (
)

const (
LevelTrace = 0
LevelDebug = 1
LevelInfo = 2
LevelWarn = 3
LevelError = 4
LevelTrace = iota
LevelDebug
LevelInfo
LevelWarn
LevelError
LevelQuite
)

const (
Expand All @@ -27,6 +28,7 @@ const (
info = "INFO"
warn = "WARNING"
err = "ERROR"
quite = "QUITE"
PADDING = "\t"
)

Expand All @@ -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!")
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/localprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
41 changes: 26 additions & 15 deletions internal/provider/onpremiseprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
14 changes: 4 additions & 10 deletions internal/provider/providerfactory.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package providers

import (
"fmt"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -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.Warn(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)
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/types/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Loading

0 comments on commit f94d639

Please sign in to comment.