t

commit 056af5cee95b1902e8244890dcbcc45356cd0052

Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>

Sync notes PoC

This PoC make syncronization works.

And by syncronization I mean `git add -A && git commit -m '/note/path'`
Which starting to sound a lot awful actually. But I'll work on that
later.

I think a good improvement now is refactor this procedural code into
something more like an API, with a nice struct to hold a note and it's
information regarding if is editing or creating a new one.

 src/main.go | 97 +++++++++++++++++++++++++++++++++++++++++++++++++-----


diff --git a/src/main.go b/src/main.go
index 5da59ce5f393362b18de462f7753d5440d02b4f6..caf294934b5e4d2917b746fc252bbe11c1a93e5e 100644
--- a/src/main.go
+++ b/src/main.go
@@ -37,29 +37,37 @@
   return cmd.Run()
 }
 
-func captureText() ([]byte, error) {
+func captureText() (string, error) {
   var err error
   var fileName string 
 
   fileName, creationErr := createNoteFile()
   
   if creationErr != nil {
-    return []byte{}, creationErr
+    return "", creationErr
   }
 
   if err = executeEditor(fileName); err != nil {
-    return []byte{}, err
+    return "", err
   }
   
   bytes, err := ioutil.ReadFile(fileName)
 
   if err != nil {
-    return []byte{}, err
+    return "", err
   }
 
   fmt.Println(bytes)
 
-  return bytes, nil
+  return fileName, nil
+}
+
+func isAGitRepository() error {
+  if _, err := os.Stat(fmt.Sprintf("%s.git", notesDirectory())); os.IsNotExist(err) {
+	  return err
+  }
+
+  return nil
 }
 
 func notesDirectory() string {
@@ -74,13 +82,84 @@
   return notesDirectory
 }
 
+func syncNotes() error {
+  cmd := exec.Command("git", "-C", notesDirectory(), "push", "--set-upstream", "origin", "master")
+  cmd.Env = append(os.Environ())
+
+  cmd.Stdin  = os.Stdin
+  cmd.Stdout = os.Stdout
+  cmd.Stderr = os.Stderr
+
+  err := cmd.Run()
+  
+  if err == nil {
+    fmt.Println("It worked")
+    return nil
+  } else {
+    return err
+  }
+}
+
+func addChangedFiles() error {
+  cmd := exec.Command("git", "-C", notesDirectory(), "add", "-A")
+  cmd.Stderr = os.Stderr
+  
+  err := cmd.Run()
+
+  return err
+}
+
+func commitLastNote(fileName string) error {
+  // TODO: We should pretty format this date
+  // Also handle different actions as well
+
+  addingError := addChangedFiles()
+  
+  if addingError != nil {
+    return addingError
+  }
+
+  commitMessage := fmt.Sprintf("Added note '%s'", fileName)
+
+  cmd := exec.Command("git", "-C", notesDirectory(), "commit", "-am", commitMessage)
+  cmd.Env = append(os.Environ())
+  cmd.Stdin = os.Stdin
+  cmd.Stdout = os.Stdout
+  cmd.Stderr = os.Stderr
+
+  err := cmd.Run()
+
+  if err != nil {
+    fmt.Println("Deu treta ao commitar")
+    return err
+  } else {
+    return nil
+  }
+}
+
 func main() {
   fmt.Println("Starting...")
   
-  data, err := captureText()
+  lastNote, err := captureText()
 
-  fmt.Println(data)
-  fmt.Println(err)
+  fmt.Println(lastNote)
+  
+  if err != nil {
+    panic(err)
+  } else {
+    commitLastNote(lastNote)
+  }
+
+  fmt.Println("Syncing your notes...")
+
+  if err := isAGitRepository(); err != nil {
+    fmt.Println("Fudeu bahea")
+  } else {
+    if err := syncNotes(); err != nil {
+      fmt.Println("Could not sync notes")
+      fmt.Println(err)
+    }
+  }
 
   fmt.Println("Finished...")
-} 
+}