Skip to content

Commit

Permalink
cleaner api and better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mscno committed Feb 7, 2025
1 parent 8a6a149 commit b2a0a19
Show file tree
Hide file tree
Showing 10 changed files with 638 additions and 510 deletions.
104 changes: 51 additions & 53 deletions cmd/esec/commands/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"fmt"
"github.com/alecthomas/kong"
"github.com/mscno/esec"
"github.com/mscno/esec/pkg/fileutils"
"io"
"os"
"path"
"strings"
)

Expand Down Expand Up @@ -51,77 +53,35 @@ func (c *KeygenCmd) Run(ctx *cliCtx) error {
}

type EncryptCmd struct {
File string `arg:"" help:"Message to encrypt" default:""`
File string `arg:"" help:"File or Environment to encrypt" default:""`
Format string `help:"File format" default:".ejson" short:"f"`
DryRun bool `help:"Print the encrypted message without writing to file" short:"d"`
}

func (c *EncryptCmd) Run(ctx *cliCtx) error {
//var data []byte
//var err error
//if c.File != "" {
// data, err = os.ReadFile(c.File)
// if err != nil {
// return err
// }
//}
//if c.File == "" {
// data, err = io.ReadAll(os.Stdin)
// if err != nil {
// return err
// }
//}
//
//format, err := esec.detectFormat(c.File)
//if err != nil {
// return err
//}
//
//bs := bytes.NewReader(data)
//var buf bytes.Buffer
//_, err = esec.Encrypt(bs, &buf, format)
//if err != nil {
// return err
//}
//if c.File != "" && !c.DryRun {
// err = os.WriteFile(c.File, buf.Bytes(), 0644)
// if err != nil {
// return err
// }
//} else {
// fmt.Println(buf.String())
//}

format, err := esec.ParseFormat(c.Format)
format, err := fileutils.ParseFormat(c.Format)
if err != nil {
return fmt.Errorf("error parsing format flag %q: %v", c.Format, err)
}

n, err := esec.EncryptInputInPlace(c.File, format)
filePath, err := processFileOrEnv(c.File, format)
if err != nil {
return fmt.Errorf("error processing file or env %q: %v", c.File, err)
}

n, err := esec.EncryptFileInPlace(filePath)
fmt.Printf("Encrypted %d bytes\n", n)
return err
}

type DecryptCmd struct {
File string `arg:"" help:"File to decrypt" default:""`
File string `arg:"" help:"File or Environment to decrypt" default:""`
Format string `help:"File format" default:".ejson" short:"f"`
KeyFromStdin bool `help:"Read the key from stdin" short:"k"`
KeyDir string `help:"Directory containing the '.esec_keyring' file" default:"." short:"d"`
}

func (c *DecryptCmd) Run(ctx *cliCtx) error {
//var reader io.Reader
//if c.File != "" {
// file, err := os.Open(c.File)
// if err != nil {
// return err
// }
// defer file.Close()
// reader = file
//} else {
// reader = os.Stdin
//}

var key string
if c.KeyFromStdin {
data, err := io.ReadAll(os.Stdin)
Expand All @@ -132,17 +92,55 @@ func (c *DecryptCmd) Run(ctx *cliCtx) error {
key = strings.TrimSpace(key)
}

format, err := esec.ParseFormat(c.Format)
format, err := fileutils.ParseFormat(c.Format)
if err != nil {
return fmt.Errorf("error parsing format flag %q: %v", c.Format, err)
}

fileName, err := processFileOrEnv(c.File, format)
if err != nil {
fmt.Errorf("error processing file or env: %v", err)
}

var buf bytes.Buffer
data, err := esec.DecryptInput(c.File, c.KeyDir, key, format)
data, err := esec.DecryptFile(fileName, c.KeyDir, key)
if err != nil {
return err
}

buf.Read(data)
fmt.Println(string(data))
return nil
}

func processFileOrEnv(input string, defaultFileFormat fileutils.FileFormat) (filename string, err error) {
// Check if input starts with any valid format
isFile := false
for _, format := range fileutils.ValidFormats() {
if strings.Contains(path.Base(input), string(format)) {
isFile = true
break
}
}

if isFile {
return input, nil
}

// Input is treated as an environment
environment := input
// Validate environment string
if strings.ContainsAny(environment, ".\\/") {
return "", fmt.Errorf("invalid environment name: %s - should not contain dots or path separators", input)
}

for _, char := range environment {
if !strings.Contains("abcdefghijklmnopqrstuvwxyz0123456789", string(char)) {
return "", fmt.Errorf("invalid environment name: %s - should be lowercase alphanumeric", input)
}
}

// Generate filename using the default format (.env)
filename = fileutils.GenerateFilename(defaultFileFormat, environment)
return path.Clean(filename), nil
}
Loading

0 comments on commit b2a0a19

Please sign in to comment.