Skip to content

Commit

Permalink
feat: split in two commands scan and dump
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienaury committed Mar 24, 2024
1 parent 26bf4dd commit 70163e6
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 36 deletions.
40 changes: 4 additions & 36 deletions cmd/silo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
"runtime"
"strings"

"github.com/cgi-fr/silo/internal/infra"
"github.com/cgi-fr/silo/pkg/silo"
"github.com/cgi-fr/silo/internal/app/cli"
"github.com/mattn/go-isatty"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -67,12 +66,6 @@ There is NO WARRANTY, to the extent permitted by law.`, version, commit, buildDa
Str("color", colormode).
Msg("start SILO")
},
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, _ []string) {
if err := run(cmd); err != nil {
log.Fatal().Err(err).Msg("end SILO")
}
},
PersistentPostRun: func(_ *cobra.Command, _ []string) {
log.Info().Int("return", 0).Msg("end SILO")
},
Expand All @@ -85,40 +78,15 @@ There is NO WARRANTY, to the extent permitted by law.`, version, commit, buildDa
rootCmd.PersistentFlags().StringVar(&colormode, "color", "auto", "use colors in log outputs : yes, no or auto")
rootCmd.PersistentFlags().BoolVar(&profiling, "profiling", false, "enable cpu profiling and generate a cpu.pprof file")

rootCmd.AddCommand(cli.NewScanCommand(name, os.Stderr, os.Stdout, os.Stdin))
rootCmd.AddCommand(cli.NewDumpCommand(name, os.Stderr, os.Stdout, os.Stdin))

if err := rootCmd.Execute(); err != nil {
log.Err(err).Msg("error when executing command")
os.Exit(1)
}
}

func run(_ *cobra.Command) error {
backend, err := infra.NewBackend("silo-pebble")
if err != nil {
return fmt.Errorf("%w", err)
}

defer backend.Close()

writer := infra.NewDumpJSONLine()

driver := silo.NewDriver(backend, writer)

reader, err := infra.NewDataRowReaderJSONLine()
if err != nil {
return fmt.Errorf("%w", err)
}

if err := driver.Scan(reader); err != nil {
return fmt.Errorf("%w", err)
}

if err := driver.Dump(); err != nil {
return fmt.Errorf("%w", err)
}

return nil
}

func initLog() {
color := false

Expand Down
65 changes: 65 additions & 0 deletions internal/app/cli/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (C) 2024 CGI France
//
// This file is part of SILO.
//
// SILO is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SILO is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SILO. If not, see <http://www.gnu.org/licenses/>.

package cli

import (
"fmt"
"os"

"github.com/cgi-fr/silo/internal/infra"
"github.com/cgi-fr/silo/pkg/silo"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

func NewDumpCommand(parent string, stderr *os.File, stdout *os.File, stdin *os.File) *cobra.Command {
cmd := &cobra.Command{ //nolint:exhaustruct
Use: "dump path",
Short: "Dump silo database stored in given path into stdout",
Example: " " + parent + " dump clients",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
if err := dump(args[0]); err != nil {
log.Fatal().Err(err).Int("return", 1).Msg("end SILO")
}
},
}

cmd.SetOut(stdout)
cmd.SetErr(stderr)
cmd.SetIn(stdin)

return cmd
}

func dump(path string) error {
backend, err := infra.NewBackend(path)
if err != nil {
return fmt.Errorf("%w", err)
}

defer backend.Close()

driver := silo.NewDriver(backend, infra.NewDumpJSONLine())

if err := driver.Dump(); err != nil {
return fmt.Errorf("%w", err)
}

return nil
}
70 changes: 70 additions & 0 deletions internal/app/cli/scan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (C) 2024 CGI France
//
// This file is part of SILO.
//
// SILO is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SILO is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SILO. If not, see <http://www.gnu.org/licenses/>.

package cli

import (
"fmt"
"os"

"github.com/cgi-fr/silo/internal/infra"
"github.com/cgi-fr/silo/pkg/silo"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

func NewScanCommand(parent string, stderr *os.File, stdout *os.File, stdin *os.File) *cobra.Command {
cmd := &cobra.Command{ //nolint:exhaustruct
Use: "scan path",
Short: "Ingest data from stdin and update silo database stored in given path",
Example: " " + parent + " scan clients",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
if err := scan(args[0]); err != nil {
log.Fatal().Err(err).Int("return", 1).Msg("end SILO")
}
},
}

cmd.SetOut(stdout)
cmd.SetErr(stderr)
cmd.SetIn(stdin)

return cmd
}

func scan(path string) error {
backend, err := infra.NewBackend(path)
if err != nil {
return fmt.Errorf("%w", err)
}

defer backend.Close()

driver := silo.NewDriver(backend, nil)

reader, err := infra.NewDataRowReaderJSONLine()
if err != nil {
return fmt.Errorf("%w", err)
}

if err := driver.Scan(reader); err != nil {
return fmt.Errorf("%w", err)
}

return nil
}
4 changes: 4 additions & 0 deletions pkg/silo/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"errors"
"fmt"
"io"

"github.com/rs/zerolog/log"
)

type Driver struct {
Expand Down Expand Up @@ -102,6 +104,8 @@ func (d *Driver) Scan(input DataRowReader) error {

links := Scan(datarow)

log.Info().Int("links", len(links)).Interface("row", datarow).Msg("datarow scanned")

for _, link := range links {
if err := d.backend.Store(link.E1, link.E2); err != nil {
return fmt.Errorf("%w: %w", ErrPersistingData, err)
Expand Down

0 comments on commit 70163e6

Please sign in to comment.