Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an output flag for saving the output to a specified file #387

Merged
merged 8 commits into from
Nov 7, 2018
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,12 @@ provide more than one backend, and SOPS will log to all of them:
- connection_string: "postgres://sops:sops@localhost/sops?sslmode=verify-full"
- connection_string: "postgres://sops:sops@remotehost/sops?sslmode=verify-full"

Saving Output to a File
~~~~~~~~~~~~~~~~~~~~~~~
By default ``sops`` just dumps all the output to the standard output. We can use the
``--output`` flag followed by a filename to save the output to the file specified.
Beware using both ``--in-place`` and ``--output`` flags will result in an error.


Important information on types
------------------------------
Expand Down
36 changes: 24 additions & 12 deletions cmd/sops/main.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package main //import "go.mozilla.org/sops/cmd/sops"

import (
encodingjson "encoding/json"
"fmt"
"net"
"net/url"

"google.golang.org/grpc"

"go.mozilla.org/sops"

"fmt"
"os"
"strings"
"time"

encodingjson "encoding/json"
"reflect"

"strconv"
"strings"
"time"

"github.com/sirupsen/logrus"
"go.mozilla.org/sops"
"go.mozilla.org/sops/aes"
_ "go.mozilla.org/sops/audit"
"go.mozilla.org/sops/azkv"
Expand All @@ -36,6 +30,7 @@ import (
"go.mozilla.org/sops/pgp"
"go.mozilla.org/sops/stores/json"
yamlstores "go.mozilla.org/sops/stores/yaml"
"google.golang.org/grpc"
"gopkg.in/urfave/cli.v1"
)

Expand Down Expand Up @@ -407,6 +402,10 @@ func main() {
Name: "verbose",
Usage: "Enable verbose logging output",
},
cli.StringFlag{
Name: "output",
Usage: "Save the output after encryption or decryption to the file specified",
},
}, keyserviceFlags...)

app.Action = func(c *cli.Context) error {
Expand All @@ -416,6 +415,9 @@ func main() {
if c.NArg() < 1 {
return common.NewExitError("Error: no file specified", codes.NoFileSpecified)
}
if c.Bool("in-file") && c.String("output") != "" {
hellozee marked this conversation as resolved.
Show resolved Hide resolved
return common.NewExitError("Error: cannot operate on both --output and --in-file", codes.ErrorConflictingParameters)
hellozee marked this conversation as resolved.
Show resolved Hide resolved
}
fileName := c.Args()[0]
if _, err := os.Stat(fileName); os.IsNotExist(err) {
if c.String("add-kms") != "" || c.String("add-pgp") != "" || c.String("add-gcp-kms") != "" || c.String("add-azure-kv") != "" ||
Expand Down Expand Up @@ -619,7 +621,17 @@ func main() {
log.Info("File written successfully")
return nil
}
_, err = os.Stdout.Write(output)

outputFile := os.Stdout
if c.String("output") != "" {
file, err := os.Create(c.String("output"))
if err != nil {
return common.NewExitError(fmt.Sprintf("Could not open output file for writing: %s", err), codes.CouldNotWriteOutputFile)
}
defer file.Close()
outputFile = file
}
_, err = outputFile.Write(output)
return toExitError(err)
}
app.Run(os.Args)
Expand Down