Skip to content

Commit

Permalink
Place log into download folder
Browse files Browse the repository at this point in the history
  • Loading branch information
s-l-teichmann committed Aug 25, 2023
1 parent 241c4af commit 8c3ad34
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
40 changes: 38 additions & 2 deletions cmd/csaf_downloader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"log/slog"
"net/http"
"os"
"path/filepath"

"github.com/csaf-poc/csaf_distribution/v2/internal/certs"
"github.com/csaf-poc/csaf_distribution/v2/internal/filter"
Expand Down Expand Up @@ -48,7 +49,7 @@ const (
)

type config struct {
Directory *string `short:"d" long:"directory" description:"DIRectory to store the downloaded files in" value-name:"DIR" toml:"directory"`
Directory string `short:"d" long:"directory" description:"DIRectory to store the downloaded files in" value-name:"DIR" toml:"directory"`
Insecure bool `long:"insecure" description:"Do not check TLS certificates from provider" toml:"insecure"`
IgnoreSignatureCheck bool `long:"ignoresigcheck" description:"Ignore signature check results, just warn on mismatch" toml:"ignoresigcheck"`
ClientCert *string `long:"client-cert" description:"TLS client certificate file (PEM encoded data)" value-name:"CERT-FILE" toml:"client_cert"`
Expand Down Expand Up @@ -169,13 +170,47 @@ func (ll logLevel) slogLevel() slog.Level {
}
}

// prepareDirectory ensures that the working directory
// exists and is setup properly.
func (cfg *config) prepareDirectory() error {
// If no special given use current working directory.
if cfg.Directory == "" {
dir, err := os.Getwd()
if err != nil {
return err
}
cfg.Directory = dir
return nil
}
// Use given directory
if _, err := os.Stat(cfg.Directory); err != nil {
// If it does not exist create it.
if os.IsNotExist(err) {
if err = os.MkdirAll(cfg.Directory, 0755); err != nil {
return err
}
} else {
return err
}
}
return nil
}

// prepareLogging sets up the structured logging.
func (cfg *config) prepareLogging() error {
var w io.Writer
if cfg.LogFile == "" {
w = os.Stderr
} else {
f, err := os.OpenFile(cfg.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
var fname string
// We put the log inside the download folder
// if it is not absolute.
if filepath.IsAbs(cfg.LogFile) {
fname = cfg.LogFile
} else {
fname = filepath.Join(cfg.Directory, cfg.LogFile)
}
f, err := os.OpenFile(fname, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
return err
}
Expand Down Expand Up @@ -215,6 +250,7 @@ func (cfg *config) prepareCertificates() error {
// prepare prepares internal state of a loaded configuration.
func (cfg *config) prepare() error {
for _, prepare := range []func(*config) error{
(*config).prepareDirectory,
(*config).prepareLogging,
(*config).prepareCertificates,
(*config).compileIgnorePatterns,
Expand Down
35 changes: 1 addition & 34 deletions cmd/csaf_downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (

type downloader struct {
cfg *config
directory string
keys *crypto.KeyRing
eval *util.PathEval
validator csaf.RemoteValidator
Expand Down Expand Up @@ -575,7 +574,7 @@ nextAdvisory:
initialReleaseDate = initialReleaseDate.UTC()

// Write advisory to file
newDir := path.Join(d.directory, lower)
newDir := path.Join(d.cfg.Directory, lower)

// Do we have a configured destination folder?
if d.cfg.Folder != "" {
Expand Down Expand Up @@ -668,40 +667,8 @@ func loadHash(client util.Client, p string) ([]byte, []byte, error) {
return hash, data.Bytes(), nil
}

// prepareDirectory ensures that the working directory
// exists and is setup properly.
func (d *downloader) prepareDirectory() error {
// If no special given use current working directory.
if d.cfg.Directory == nil {
dir, err := os.Getwd()
if err != nil {
return err
}
d.directory = dir
return nil
}
// Use given directory
if _, err := os.Stat(*d.cfg.Directory); err != nil {
// If it does not exist create it.
if os.IsNotExist(err) {
if err = os.MkdirAll(*d.cfg.Directory, 0755); err != nil {
return err
}
} else {
return err
}
}
d.directory = *d.cfg.Directory
return nil
}

// run performs the downloads for all the given domains.
func (d *downloader) run(ctx context.Context, domains []string) error {

if err := d.prepareDirectory(); err != nil {
return err
}

for _, domain := range domains {
if err := d.download(ctx, domain); err != nil {
return err
Expand Down

0 comments on commit 8c3ad34

Please sign in to comment.