Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: nedlib.go function #127

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions cmd/convert/nedlib/nedlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nedlib

import (
"context"
"errors"
"fmt"
"os"
"os/signal"
Expand Down Expand Up @@ -68,38 +67,42 @@ func NewCommand() *cobra.Command {
}

func parseArgumentsAndCallNedlib(cmd *cobra.Command, args []string) error {
config := &conf{}
// The Nedlib data structure does not support direct filename transformations.
// Instead, we employ a custom generator that treats the input filename as a date.
// When we request a new warcwriter, we submit a synthetic fromFilename based on the date of the first record.
viper.Set(flag.NameGenerator, "nedlib")

if warcWriterConfig, err := warcwriterconfig.NewFromViper(cmd.Name()); err != nil {
return err
} else {
warcWriterConfig.WarcInfoFunc = func(recordBuilder gowarc.WarcRecordBuilder) error {
payload := &gowarc.WarcFields{}
payload.Set("software", cmdversion.SoftwareVersion()+" https://github.com/nlnwa/warchaeology")
payload.Set("format", fmt.Sprintf("WARC File Format %d.%d", warcWriterConfig.WarcVersion.Minor(), warcWriterConfig.WarcVersion.Minor()))
payload.Set("description", "Converted from Nedlib")
hostname, errInner := os.Hostname()
if errInner != nil {
return errInner
}
payload.Set("host", hostname)

_, err := recordBuilder.WriteString(payload.String())
return err
if len(args) == 0 && viper.GetString(flag.SrcFileList) == "" {
return fmt.Errorf("missing file or directory name")
}

var warcWriterConfig *warcwriterconfig.WarcWriterConfig

warcWriterConfig, err := warcwriterconfig.NewFromViper(cmd.Name())
if err != nil {
return fmt.Errorf("error creating warc writer config, original error: '%w'", err)
}

warcWriterConfig.WarcInfoFunc = func(recordBuilder gowarc.WarcRecordBuilder) error {
payload := &gowarc.WarcFields{}
payload.Set("software", cmdversion.SoftwareVersion()+" https://github.com/nlnwa/warchaeology")
payload.Set("format", fmt.Sprintf("WARC File Format %d.%d", warcWriterConfig.WarcVersion.Minor(), warcWriterConfig.WarcVersion.Minor()))
payload.Set("description", "Converted from Nedlib")
hostname, errInner := os.Hostname()
if errInner != nil {
return errInner
}
payload.Set("host", hostname)

config.writerConf = warcWriterConfig
_, err := recordBuilder.WriteString(payload.String())
return err
}
config.concurrency = viper.GetInt(flag.Concurrency)

if len(args) == 0 && viper.GetString(flag.SrcFileList) == "" {
return errors.New("missing file or directory name")
config := &conf{
files: args,
concurrency: viper.GetInt(flag.Concurrency),
writerConf: warcWriterConfig,
}
config.files = args
return runE(cmd.Name(), config)

}
Expand Down
Loading