status: add a function to parse a status

Michael Muré created

Change summary

bug/status.go | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)

Detailed changes

bug/status.go 🔗

@@ -1,5 +1,10 @@
 package bug
 
+import (
+	"fmt"
+	"strings"
+)
+
 type Status int
 
 const (
@@ -29,3 +34,16 @@ func (s Status) Action() string {
 		return "unknown status"
 	}
 }
+
+func StatusFromString(str string) (Status, error) {
+	cleaned := strings.ToLower(strings.TrimSpace(str))
+
+	switch cleaned {
+	case "open":
+		return OpenStatus, nil
+	case "closed":
+		return ClosedStatus, nil
+	default:
+		return 0, fmt.Errorf("unknow status")
+	}
+}