shithub: plumb

Download patch

ref: 303886cf25bb8cec869a9dc7ec1c38871667aaf1
parent: 2e63d6807ab914e87d0e3e9b8b33ac4f8541d016
author: Michael Misch <michaelmisch1985@gmail.com>
date: Fri Sep 13 04:04:35 EDT 2024

"Fix

--- a/go.mod
+++ b/go.mod
@@ -1,4 +1,4 @@
-module github.com/halfwit/plumb
+module hlfw.ca/cli/plumb
 
 go 1.16
 
--- a/plumb.go
+++ b/plumb.go
@@ -12,7 +12,7 @@
 	"strings"
 
 	"9fans.net/go/plumb"
-        "9fans.net/go/plan9"
+        	"9fans.net/go/plan9"
 )
 
 var (
@@ -20,9 +20,9 @@
 	attributes = flag.String("a", "", "set the attr field of the message (default is empty), expects key=value")
 	source = flag.String("s", "", "set the src field of the message (default is store)")
 	destination = flag.String("d", "", "set the dst filed of the message (default is store)")
-        typefield = flag.String("t", "", "override the type message sent to the plumber")
+        	typefield = flag.String("t", "", "override the type message sent to the plumber")
 	directory = flag.String("w", "", "set the wdir field of the message (default is current directory)")
-        input = flag.Bool("i", false, "take the data from standard input rather than the argument strings")
+        	input = flag.Bool("i", false, "take the data from standard input rather than the argument strings")
 )
 
 type storeMsg struct {
@@ -141,11 +141,11 @@
 }
 
 func content(data string) (string, error) {
-        if *typefield != "" {
-            return *typefield, nil
-        }
+	if *typefield != "" {
+		return *typefield, nil
+	}
  
-	if _, err := os.Stat(data); os.IsNotExist(err)  {
+	if _, err := os.Stat(data); err != nil {
 		return contentTypeUrl(data)
 	}
 	return contentTypeFile(data)
@@ -152,43 +152,47 @@
 }
 
 func getMediaType(ct string) (string, *plumb.Attribute) {
-		if ct == "text" {
-			return ct, nil
-		}
-		mediaType, params, err := mime.ParseMediaType(ct)
-		if err != nil {
-			log.Fatal(err)
-		}
-		return mediaType, paramsToAttr(params)
+	if *typefield != "" {
+		return *typefield, nil
+	}
+	if ct == "text" {
+		return ct, nil
+	}
+	mediaType, params, err := mime.ParseMediaType(ct)
+	if err != nil {
+		log.Fatal(err)
+	}
+	return mediaType, paramsToAttr(params)
 }
 
 func readInput() string {
-        data := new(strings.Builder)
-        if *input {
+	data := new(strings.Builder)
+	if *input {
 		// readInput on stdin will come with a fancy newline
 		// return a slice without the last character 
 		io.Copy(data, os.Stdin)
 		return data.String()[:data.Len()-1]
-        } else {
+	} else {
 		data.WriteString(flag.Arg(0))
-        }
+	}
 
 	return data.String()
 }
 
 func main() {
-
+	var wdir string
 	flag.Parse()
 	if flag.Lookup("h") != nil {
 		flag.Usage()
 		os.Exit(1)
 	}
-	wdir, err := os.Getwd()
-	if err != nil {
-		log.Fatal(err)
-	}
 
-        data := readInput()
+	if *directory != "" {
+		wdir = *directory
+	} else {
+		wdir, _ = os.Getwd()
+	}
+        	data := readInput()
 	ct, err := content(data)
 	if err != nil {
 		log.Fatal(err)
--