wip: archive

This commit is contained in:
Suyono 2023-08-12 18:35:15 +10:00
parent 64e4ca400f
commit 7642caeb6b
5 changed files with 26 additions and 21 deletions

View File

@ -1 +1 @@
golang 1.20.5 golang 1.20.7

1
archive/dir.go Normal file
View File

@ -0,0 +1 @@
package archive

View File

@ -3,8 +3,9 @@ package spt
import ( import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"gitea.suyono.dev/suyono/simple-privacy-tool/privacy"
"io" "io"
"gitea.suyono.dev/suyono/simple-privacy-tool/privacy"
) )
type decryptApp struct { type decryptApp struct {
@ -32,9 +33,9 @@ func (d *decryptApp) ProcessFiles() (err error) {
) )
if f.IsBase64() { if f.IsBase64() {
src = base64.NewDecoder(base64.StdEncoding, d.srcFile) src = base64.NewDecoder(base64.StdEncoding, d.src)
} else { } else {
src = d.srcFile src = d.src
} }
d.r = privacy.NewPrivacyReaderWithKeyGen(src, f.KeyGen()) d.r = privacy.NewPrivacyReaderWithKeyGen(src, f.KeyGen())
@ -53,15 +54,15 @@ redo:
return return
} }
if _, err = io.Copy(d.dstFile, d.r); err != nil { if _, err = io.Copy(d.dst, d.r); err != nil {
return return
} }
if err = d.dstFile.Close(); err != nil { if err = d.dst.Close(); err != nil {
return return
} }
if err = d.srcFile.Close(); err != nil { if err = d.src.Close(); err != nil {
return return
} }

View File

@ -2,8 +2,9 @@ package spt
import ( import (
"encoding/base64" "encoding/base64"
"gitea.suyono.dev/suyono/simple-privacy-tool/privacy"
"io" "io"
"gitea.suyono.dev/suyono/simple-privacy-tool/privacy"
) )
type encryptApp struct { type encryptApp struct {
@ -38,9 +39,9 @@ func (e *encryptApp) ProcessFiles() (err error) {
var dst io.WriteCloser var dst io.WriteCloser
if f.IsBase64() { if f.IsBase64() {
dst = base64.NewEncoder(base64.StdEncoding, e.dstFile) dst = base64.NewEncoder(base64.StdEncoding, e.dst)
} else { } else {
dst = e.dstFile dst = e.dst
} }
if f.IncludeHint() { if f.IncludeHint() {
@ -58,7 +59,7 @@ func (e *encryptApp) ProcessFiles() (err error) {
return return
} }
if _, err = io.Copy(e.wc, e.srcFile); err != nil { if _, err = io.Copy(e.wc, e.src); err != nil {
return return
} }
@ -66,7 +67,7 @@ func (e *encryptApp) ProcessFiles() (err error) {
return return
} }
if err = e.srcFile.Close(); err != nil { if err = e.src.Close(); err != nil {
return return
} }

View File

@ -2,19 +2,21 @@ package spt
import ( import (
"errors" "errors"
"io"
"os" "os"
"log"
tw "gitea.suyono.dev/suyono/terminal_wrapper" tw "gitea.suyono.dev/suyono/terminal_wrapper"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"log"
) )
type cApp struct { type cApp struct {
term *tw.Terminal term *tw.Terminal
srcPath string srcPath string
dstPath string dstPath string
srcFile *os.File src io.ReadCloser
dstFile *os.File dst io.WriteCloser
passphrase string passphrase string
} }
@ -163,23 +165,23 @@ func decrypt(cmd *cobra.Command, args []string) (err error) {
func (a *cApp) ProcessArgs(args []string) (err error) { func (a *cApp) ProcessArgs(args []string) (err error) {
if len(args) == 0 { if len(args) == 0 {
a.srcFile = os.Stdin a.src = os.Stdin
a.dstFile = os.Stdout a.dst = os.Stdout
} else { } else {
a.srcPath = args[0] a.srcPath = args[0]
a.dstPath = args[1] a.dstPath = args[1]
if a.srcPath == "-" { if a.srcPath == "-" {
a.srcFile = os.Stdin a.src = os.Stdin
} else { } else {
if a.srcFile, err = os.Open(a.srcPath); err != nil { if a.src, err = os.Open(a.srcPath); err != nil {
return return
} }
} }
if a.dstPath == "-" { if a.dstPath == "-" {
a.dstFile = os.Stdout a.dst = os.Stdout
} else { } else {
if a.dstFile, err = os.OpenFile(a.dstPath, os.O_CREATE|os.O_WRONLY, 0640); err != nil { //TODO: allow user to define the destination file permission if a.dst, err = os.OpenFile(a.dstPath, os.O_CREATE|os.O_WRONLY, 0640); err != nil { //TODO: allow user to define the destination file permission
return return
} }
} }