antonmarin/secret-yaml

View on GitHub
cmd/decrypt.go

Summary

Maintainability
A
0 mins
Test Coverage
package cmd

import (
    "fmt"
    "github.com/antonmarin/secret-yaml/document"
    encryption "github.com/antonmarin/secret-yaml/encryption/aes"
    "github.com/antonmarin/secret-yaml/io"
    "github.com/antonmarin/secret-yaml/useCases/decrypt"
    flag "github.com/spf13/pflag"

    "github.com/spf13/cobra"
)

// decryptCmd represents the encrypt command
var decryptCmd = &cobra.Command{
    Short:   "Decrypt yaml file",
    Long:    `Decrypts file with --secret and outputs to stdout`,
    Use:     "decrypt path-to-yaml-file",
    Example: "  syml decrypt ~/chart/values.prod.yaml > ~/secrets/values.prod.decrypted.yaml",
    Args:    cobra.ExactArgs(1),
    RunE: func(cmd *cobra.Command, args []string) error {
        secret := flag.String("secret", "", "")
        flag.Parse()

        inputFile := io.NewFile(args[0])
        data, err := inputFile.AsString()
        if err != nil {
            return err
        }
        encryptionService, err := encryption.NewEncryptionService(*secret)
        if err != nil {
            return err
        }

        useCase := decrypt.NewDecrypt(encryptionService, document.NewYamlManipulator())
        result, err := useCase.Execute(data)
        if err != nil {
            return err
        }

        _, err = fmt.Println(string(result))
        if err != nil {
            return err
        }

        return nil
    },
}

func init() {
    rootCmd.AddCommand(decryptCmd)

    decryptCmd.Flags().String("secret", "", "Secret key to decode with")
    if err := decryptCmd.MarkFlagRequired("secret"); err != nil {
        panic(fmt.Errorf("fatal error: %s", err))
    }
}