Skip to content

Commit

Permalink
Merge pull request #2 from MohamTahaB/Feature/initiate-jotter
Browse files Browse the repository at this point in the history
Feature/initiate-jotter
  • Loading branch information
MohamTahaB authored Feb 14, 2024
2 parents e391ad7 + e9a2840 commit 334c2d2
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
44 changes: 44 additions & 0 deletions jotter/storage/identification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package storage

import (
"encoding/json"
"fmt"
"os"
)

// unmarshals the json file into a map of key = id, and value = name.
func unmarshal(JSONDir string) (*map[string]string, error) {
// Read the json file.
b, err := os.ReadFile(JSONDir)
if err != nil {
return nil, fmt.Errorf("error reading the json file: %v", err)
}

// Unmarshal the content of the json file.
var IDmap map[string]string

if err = json.Unmarshal(b, &IDmap); err != nil {
return nil, fmt.Errorf("error unmarshaling the json file: %v", err)
}

return &IDmap, nil
}

// marshals the id map.
func marshal(IDmap *map[string]string) (*[]byte, error) {

b, err := json.Marshal(*IDmap)
if err != nil {
return nil, fmt.Errorf("error marshaling the id map: %v", err)
}

return &b, nil
}

// Overwrites the content of the json file with the byte slice passed as a parameter.
func editJSON(JSONDir string, content *[]byte) error {
if err := os.WriteFile(JSONDir, *content, 0755); err != nil {
return fmt.Errorf("error writing into the json file: %v", err)
}
return nil
}
5 changes: 5 additions & 0 deletions jotter/storage/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package storage

func getName(JSONDir, id string) (string, error) {

}
100 changes: 100 additions & 0 deletions jotter/storage/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package storage

import (
"fmt"
"os"
"os/user"
"path/filepath"
)

// InitiateStorage checks if the storage directory is available.
// If not, it is initiated.
// Returns the dir to the JSON collection of present files, the dir to .md files, and an error.
func InitiateStorage() (string, string, error) {
usr, err := user.Current()
if err != nil {
return "", "", fmt.Errorf("error retrieving the current user: %v", err)
}

dir := filepath.Join(usr.HomeDir, ".toolbox", ".jotter", "mds")
JSONDir := filepath.Join(usr.HomeDir, ".toolbox", ".jotter", "storage.json")

// Check if both directories already exist.
_, dirErr := os.Stat(dir)
_, JSONErr := os.Stat(JSONDir)

// Both JSON and .md dir exist.
if !os.IsNotExist(dirErr) && !os.IsNotExist(JSONErr) {
return JSONDir, dir, nil
}

// TODO! for now, if one of the dirs do not exist, everything will be set anew. This should change to accomodate all possible edge cases.

// Create the dirs if one of them do not exist.
if err = os.MkdirAll(filepath.Dir(dir), 0755); err != nil {
return "", "", fmt.Errorf("error creating .md directory: %v", err)
}

if err = os.MkdirAll(filepath.Dir(JSONDir), 0755); err != nil {
return "", "", fmt.Errorf("error creating JSON file: %v", err)
}

return JSONDir, dir, nil
}

// Create creates a new .md file.
func Create(name string) (string, error) {

// Check if the storage is properly initiated.
JSONDir, dir, err := InitiateStorage()
if err != nil {
return "", err
}

dir = filepath.Join(dir, fmt.Sprintf("%s.md", name))

// Create the .md file.
if _, err = os.Create(dir); err != nil {
return "", fmt.Errorf("error creating the .md file: %v", err)
}

return dir, nil
}

// Pull pulls the content of a .md file name passed as a parameter.
func Pull(name string) (string, error) {

// Check if the storage is properly initiated.
dir, err := InitiateStorage()
if err != nil {
return "", err
}

dir = filepath.Join(dir, fmt.Sprintf("%s.md", name))

// Open the file.
b, err := os.ReadFile(dir)
if err != nil {
return "", fmt.Errorf("error reading the file: %v", err)
}

return string(b), nil
}

// Push overwrites the designated file with the content passed as parameter.
func Push(name, content string) error {

// Check if the storage is properly initiated.
dir, err := InitiateStorage()
if err != nil {
return err
}

dir = filepath.Join(dir, fmt.Sprintf("%s.md", name))

if err = os.WriteFile(dir, []byte(content), 0755); err != nil {
return fmt.Errorf("error pushing to the file: %v", err)
}

return nil
}

0 comments on commit 334c2d2

Please sign in to comment.