-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve debug mode and config handling.
- config from CLI and file are now correctly merged - debug output can be enabled with -debug - config is now in its own file
- Loading branch information
Björn Mosler
committed
Jan 10, 2023
1 parent
cce4c95
commit 57d3960
Showing
7 changed files
with
179 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/bjorm/booxsync/pkg/booxsync" | ||
log "github.com/sirupsen/logrus" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
config, err := booxsync.GetConfig() | ||
|
||
if err != nil { | ||
panic(fmt.Sprintf("invalid config: %s", err)) | ||
log.Infof("invalid config: %s", err) | ||
return | ||
} | ||
|
||
log.Printf("syncing %s to %s", config.SyncRoot, config.Host) | ||
log.Infof("syncing %s to %s", config.SyncRoot, config.Host) | ||
|
||
uploadedFiles, err := booxsync.Sync(*config) | ||
uploadedFiles, err := booxsync.Sync(config) | ||
|
||
if err != nil { | ||
panic(fmt.Sprintf("sync failed: %s", err)) | ||
log.Infof("sync failed: %s", err) | ||
return | ||
} | ||
|
||
log.Printf("uploaded files: %s", uploadedFiles) | ||
if len(uploadedFiles) > 0 { | ||
log.Infof("uploaded files:\n%s", strings.Join(uploadedFiles, "\n")) | ||
} else { | ||
log.Infoln("nothing uploaded") | ||
} | ||
|
||
log.Println("sync complete") | ||
log.Infoln("sync complete") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,25 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= | ||
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= | ||
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/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= | ||
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= | ||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= | ||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
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.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package booxsync | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"github.com/imdario/mergo" | ||
log "github.com/sirupsen/logrus" | ||
"io/fs" | ||
"net/url" | ||
"os" | ||
) | ||
|
||
const ( | ||
configFilePath = "./config.json" | ||
) | ||
|
||
type SyncConfig struct { | ||
Host string | ||
SyncRoot string | ||
PathsToSkip []string | ||
DryRun bool | ||
Debug bool | ||
syncUrl url.URL | ||
} | ||
|
||
func getCmdLineConfig() SyncConfig { | ||
dryRun := flag.Bool("dry-run", false, "do everything except write to Boox library") | ||
debug := flag.Bool("debug", false, "enabled debug output") | ||
host := flag.String("host", "", "host to sync against") | ||
syncRoot := flag.String("sync-root", "", "path of folder to sync") | ||
|
||
log.Debug("parsing commandline flags") | ||
flag.Parse() | ||
|
||
return SyncConfig{DryRun: *dryRun, Host: *host, SyncRoot: *syncRoot, Debug: *debug} | ||
} | ||
|
||
func getFileConfig() (SyncConfig, error) { | ||
if _, err := os.Stat(configFilePath); errors.Is(err, fs.ErrNotExist) { | ||
log.Debugf("config file %q does not exist", configFilePath) | ||
return SyncConfig{}, nil | ||
} | ||
|
||
b, err := os.ReadFile(configFilePath) | ||
|
||
if err != nil { | ||
return SyncConfig{}, fmt.Errorf("config: could not open file: %w", err) | ||
} | ||
|
||
log.Debugf("config file %q exists", configFilePath) | ||
var config SyncConfig | ||
|
||
err = json.Unmarshal(b, &config) | ||
log.Debugf("read config: %v", config) | ||
|
||
if err != nil { | ||
return SyncConfig{}, fmt.Errorf("config: unmarshalling failed: %w", err) | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
func checkConfig(config SyncConfig) error { | ||
if len(config.Host) == 0 { | ||
return fmt.Errorf("check config: host cannot be empty") | ||
} | ||
if _, err := os.Stat(config.SyncRoot); errors.Is(err, fs.ErrNotExist) { | ||
return fmt.Errorf("check config: sync root %q does not exist", config.SyncRoot) | ||
} | ||
return nil | ||
} | ||
|
||
func GetConfig() (SyncConfig, error) { | ||
cmdLineConfig := getCmdLineConfig() | ||
config, err := getFileConfig() | ||
|
||
if err != nil { | ||
return SyncConfig{}, err | ||
} | ||
|
||
if config.Debug || cmdLineConfig.Debug { | ||
log.SetLevel(log.DebugLevel) | ||
log.SetFormatter(&log.TextFormatter{FullTimestamp: true}) | ||
log.Debug("debug mode enabled") | ||
} | ||
|
||
log.Debugf("command line config %#v", cmdLineConfig) | ||
log.Debugf("file config %#v", config) | ||
|
||
err = mergo.Merge(&config, cmdLineConfig, mergo.WithOverride) | ||
|
||
if err = checkConfig(config); err == nil { | ||
config.syncUrl = url.URL{Scheme: "http", Host: fmt.Sprintf("%s:%d", config.Host, 8085)} | ||
log.Debugf("final config %#v", config) | ||
return config, nil | ||
} else { | ||
return SyncConfig{}, err | ||
} | ||
} |
Oops, something went wrong.