mirror of
https://github.com/Luzifer/sii.git
synced 2024-12-21 00:21:15 +00:00
Remove static target for decrypt util
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
b8d84dba6a
commit
2812974bc6
1 changed files with 45 additions and 8 deletions
|
@ -8,12 +8,15 @@ import (
|
|||
|
||||
"github.com/Luzifer/rconfig/v2"
|
||||
"github.com/Luzifer/sii"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
cfg = struct {
|
||||
Backup bool `flag:"backup,b" default:"true" description:"Bakcup encrypted file when in-place mode is used"`
|
||||
DecryptKey string `flag:"decrypt-key" default:"" description:"Hex formated decryption key" validate:"nonzero"`
|
||||
InPlace bool `flag:"in-place,i" default:"false" description:"Write the decrypted version to the same file the encrypted version was read from"`
|
||||
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
||||
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
||||
}{}
|
||||
|
@ -51,12 +54,14 @@ func main() {
|
|||
log.Fatal("Expecting exactly one SII file as an argument")
|
||||
}
|
||||
|
||||
stat, err := os.Stat(rconfig.Args()[1])
|
||||
var filename = rconfig.Args()[1]
|
||||
|
||||
stat, err := os.Stat(filename)
|
||||
if err != nil {
|
||||
log.WithError(err).Fatal("Unable to read info of save-file")
|
||||
}
|
||||
|
||||
f, err := os.Open(rconfig.Args()[1])
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
log.WithError(err).Fatal("Unable to open encrypted file")
|
||||
}
|
||||
|
@ -69,13 +74,45 @@ func main() {
|
|||
log.WithError(err).Fatal("Unable to decrypt file")
|
||||
}
|
||||
|
||||
tf, err := os.Create("/tmp/output")
|
||||
var output io.Writer = os.Stdout
|
||||
|
||||
if cfg.InPlace {
|
||||
if cfg.Backup {
|
||||
if err := copyFile(filename, filename+".bak"); err != nil {
|
||||
log.WithError(err).Fatal("Unable to create file backup")
|
||||
}
|
||||
}
|
||||
|
||||
// Close input file right now as we need to re-create it
|
||||
f.Close()
|
||||
|
||||
tf, err := os.Create(filename)
|
||||
if err != nil {
|
||||
log.WithError(err).Fatal("Unable to create output file")
|
||||
}
|
||||
defer tf.Close()
|
||||
|
||||
if _, err := io.Copy(tf, contentReader); err != nil {
|
||||
output = tf
|
||||
}
|
||||
|
||||
if _, err := io.Copy(output, contentReader); err != nil {
|
||||
log.WithError(err).Fatal("Unable to copy content")
|
||||
}
|
||||
}
|
||||
|
||||
func copyFile(src, dst string) error {
|
||||
s, err := os.Open(src)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Unable to open source file")
|
||||
}
|
||||
defer s.Close()
|
||||
|
||||
d, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Unable to open dest file")
|
||||
}
|
||||
defer d.Close()
|
||||
|
||||
_, err = io.Copy(d, s)
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue