Skip to content

Commit

Permalink
group related methods together
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcz committed Sep 26, 2024
1 parent 4df9fed commit 83481de
Showing 1 changed file with 104 additions and 88 deletions.
192 changes: 104 additions & 88 deletions cmd/s3backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ func main() {
}
}

// ============================================================
// CLI flags
// ============================================================

func basicFlags(encrypt bool) []cli.Flag {
sym := "decryption"
asym := "private"
Expand Down Expand Up @@ -278,24 +282,9 @@ func genKeyFlags() []cli.Flag {
}
}

func version() string {
if tag != "" && commit != "" {
return fmt.Sprintf("%s (%s)", tag, commit)
}
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return setting.Value
}
}
}
return "unknown"
}

func printVersion(*cli.Context) error {
fmt.Println(version())
return nil
}
// ============================================================
// CLI args
// ============================================================

func setLocalRemote(c *cli.Context) error {
if c.NArg() != 2 {
Expand All @@ -320,6 +309,25 @@ func checkPaths() error {
return nil
}

func setInOutFiles(c *cli.Context) error {
if c.NArg() != 2 {
return errors.New("in and out files are required")
}
args := c.Args()
inFile = args.Get(0)
outFile = args.Get(1)
return nil
}

// ============================================================
// CLI commands
// ============================================================

func printVersion(*cli.Context) error {
fmt.Println(version())
return nil
}

func basicPut(*cli.Context) error {
c, err := newClient()
if err != nil {
Expand All @@ -336,6 +344,69 @@ func basicGet(*cli.Context) error {
return c.GetRemoteFile(remotePath, localPath)
}

func vaultPut(c *cli.Context) error {
if err := initWithVault(true); err != nil {
return err
}
defer removePemKeyFile()
return basicPut(c)
}

func vaultGet(c *cli.Context) error {
if err := initWithVault(false); err != nil {
return err
}
defer removePemKeyFile()
return basicGet(c)
}

func genSecretKey(*cli.Context) error {
key, err := crypto.GenerateAESKeyString()
if err != nil {
return err
}
fmt.Println(key)
return nil
}

func genKeyPair(*cli.Context) error {
return crypto.GenerateRSAKeyPair(rsaPrivKey, rsaPubKey)
}

func encryptLocalFile(*cli.Context) error {
cipher, err := requiredCipher()
if err != nil {
return err
}
return cipher.Encrypt(inFile, outFile)
}

func decryptLocalFile(*cli.Context) error {
cipher, err := requiredCipher()
if err != nil {
return err
}
return cipher.Decrypt(inFile, outFile)
}

// ============================================================
// Common actions
// ============================================================

func version() string {
if tag != "" && commit != "" {
return fmt.Sprintf("%s (%s)", tag, commit)
}
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return setting.Value
}
}
}
return "unknown"
}

func newClient() (*client.Client, error) {
s3, err := store.NewS3(
awsAccessKey,
Expand All @@ -362,28 +433,22 @@ func newClient() (*client.Client, error) {
}, nil
}

func vaultPut(c *cli.Context) error {
if err := initWithVault(true); err != nil {
return err
func optionalCipher() (client.Cipher, error) {
if symKey != "" {
return crypto.NewAESCipher(symKey)
}
defer maybeRemoveKeyFile()
return basicPut(c)
}

func vaultGet(c *cli.Context) error {
if err := initWithVault(false); err != nil {
return err
if pemKeyFile != "" {
return crypto.NewRSACipher(pemKeyFile)
}
defer maybeRemoveKeyFile()
return basicGet(c)
return nil, nil
}

func maybeRemoveKeyFile() {
if pemKeyFile != "" {
if err := os.Remove(pemKeyFile); err != nil {
log.Printf("WARNING: unable to remove key file %s: %v\n", pemKeyFile, err)
}
func requiredCipher() (client.Cipher, error) {
cipher, err := optionalCipher()
if err != nil || cipher != nil {
return cipher, err
}
return nil, errors.New("either one of symKey or pemKey is required")
}

func initWithVault(encrypt bool) error {
Expand Down Expand Up @@ -427,59 +492,10 @@ func configFromVault() (*config.Config, error) {
return nil, errors.New("vault credentials not provided")
}

func genSecretKey(*cli.Context) error {
key, err := crypto.GenerateAESKeyString()
if err != nil {
return err
}
fmt.Println(key)
return nil
}

func genKeyPair(*cli.Context) error {
return crypto.GenerateRSAKeyPair(rsaPrivKey, rsaPubKey)
}

func setInOutFiles(c *cli.Context) error {
if c.NArg() != 2 {
return errors.New("in and out files are required")
}
args := c.Args()
inFile = args.Get(0)
outFile = args.Get(1)
return nil
}

func encryptLocalFile(*cli.Context) error {
cipher, err := requiredCipher()
if err != nil {
return err
}
return cipher.Encrypt(inFile, outFile)
}

func decryptLocalFile(*cli.Context) error {
cipher, err := requiredCipher()
if err != nil {
return err
}
return cipher.Decrypt(inFile, outFile)
}

func optionalCipher() (client.Cipher, error) {
if symKey != "" {
return crypto.NewAESCipher(symKey)
}
func removePemKeyFile() {
if pemKeyFile != "" {
return crypto.NewRSACipher(pemKeyFile)
}
return nil, nil
}

func requiredCipher() (client.Cipher, error) {
cipher, err := optionalCipher()
if err != nil || cipher != nil {
return cipher, err
if err := os.Remove(pemKeyFile); err != nil {
log.Printf("WARNING: unable to remove key file %s: %v\n", pemKeyFile, err)
}
}
return nil, errors.New("either one of symKey or pemKey is required")
}

0 comments on commit 83481de

Please sign in to comment.