Git Product home page Git Product logo

Comments (2)

skeeto avatar skeeto commented on June 10, 2024

To illustrate some of these things, here's what I mean about passing errors back up to the top level:

diff --git a/cmd/encrypt.go b/cmd/encrypt.go
index 5ab9863..4143a93 100644
--- a/cmd/encrypt.go
+++ b/cmd/encrypt.go
@@ -26,12 +26,16 @@ var encCommand = &cobra.Command{
                note, _ := cmd.Flags().GetString("note")
                file, _ := cmd.Flags().GetString("file")
                isPrint, _ := cmd.Flags().GetBool("print")
+               var err error
                if note != "" {
-                       encryptString(note, isPrint)
+                       err = encryptString(note, isPrint)
                }
 
                if file != "" {
-                       encryptFile(file, isPrint)
+                       err = encryptFile(file, isPrint)
+               }
+               if err != nil {
+                       log.Fatal(err)
                }
        },
 }
@@ -47,7 +51,7 @@ func init() {
        encCommand.Flags().BoolP("print", "p", false, "-p")
 }
 
-func encryptString(value string, isPrint bool) {
+func encryptString(value string, isPrint bool) error {
        cmd := exec.Command("gpg", "--encrypt", "-r", gpgID, "--armor")
 
        isDone, _ := pterm.DefaultSpinner.Start()
@@ -110,7 +114,7 @@ func encryptString(value string, isPrint bool) {
        defer isDone.Success("Successfully encrypted and saved! ", status)
 }
 
-func encryptFile(filePath string, isPrint bool) {
+func encryptFile(filePath string, isPrint bool) error {
        cmd := exec.Command("gpg", "--encrypt", "--armor", "-r", gpgID, "-o", "/dev/stdout", filePath)
 
        out, err := cmd.Output()

Then you can simply return err in these functions any time you have a non-nil error. No more panic.

For piping to the bucket, that looks something like this:

--- a/cmd/encrypt.go
+++ b/cmd/encrypt.go
@@ -113,32 +113,40 @@ func encryptString(value string, isPrint bool) {
 func encryptFile(filePath string, isPrint bool) error {
 	cmd := exec.Command("gpg", "--encrypt", "--armor", "-r", gpgID, "-o", "/dev/stdout", filePath)
 
-	out, err := cmd.Output()
+	var err error
+	var stdout io.Reader
+	if stdout, err = cmd.StdoutPipe(); err != nil {
+		return err
+	}
 
+	err = cmd.Start()
 	if err != nil {
-		log.Fatal(err)
+		return err
 	}
+	defer cmd.Wait()
 
-	result := string(out)
 	isDone, _ := pterm.DefaultSpinner.Start()
 
 	if isPrint {
-		fmt.Println(result)
-		return
+		stdout = io.TeeReader(stdout, os.Stdout)
 	}
 
-	readedResult := strings.NewReader(result)
 	fileName := filepath.Base(filePath)
 
 	bucketName := os.Getenv("MINIO_BUCKET_NAME")
 
-	status, err := Client.PutObject(bucketName, "/files/"+fileName+".asc", readedResult, int64(len(result)), minio.PutObjectOptions{
+	status, err := Client.PutObject(bucketName, "/files/"+fileName+".asc", stdout, -1, minio.PutObjectOptions{
 		ContentType: "application/pgp-encrypted",
 	})
 
 	if err != nil {
-		panic(err)
+		return err
+	}
+
+	if err := cmd.Wait(); err != nil {
+		return err
 	}
 
 	isDone.Success("Successfully encrypted and saved! ", status)
+	return nil
 }

from super-dollop.

jack5341 avatar jack5341 commented on June 10, 2024

Thank you for the your valuable feedback. If you want to be contributer you can directly create a PR and i'll happy to accept your PR.

from super-dollop.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.