generated from adrienaury/go-template
-
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.
feat: split in two commands scan and dump
- Loading branch information
1 parent
26bf4dd
commit 70163e6
Showing
4 changed files
with
143 additions
and
36 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 |
---|---|---|
@@ -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 | ||
} |
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,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 | ||
} |
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