Skip to content

Commit

Permalink
Renamed filesystem to vaults and pkg to internal
Browse files Browse the repository at this point in the history
  • Loading branch information
grd committed Dec 21, 2024
1 parent 6a19ae9 commit 285d55c
Show file tree
Hide file tree
Showing 43 changed files with 160 additions and 92 deletions.
17 changes: 12 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
FREEPDM=https://github.com/grd/FreePDM
FREECAD_FILES=$(FREEPDM)/ConceptOfDesign/TestFiles
BIN_DIR=${HOME}/bin
CONTAINER_NAME := freepdm

Expand All @@ -19,14 +18,22 @@ removevault:
test:
go test -v ./...

fstest:
go test -failfast pkg/filesystem/main_test.go
vaultstest:
go test -failfast internal/vaults/main_test.go

pdmserver:
go build -o $(BIN_DIR)/pdmserver cmd/pdmserver/main.go
go build -o $(BIN_DIR)/pdmserver ./cmd/pdmserver
pdmserver

runserver:
go run ./cmd/pdmserver

pdmterm:
go build -o $(BIN_DIR)/pdmterm cmd/pdmterm/main.go
go build -o $(BIN_DIR)/pdmterm ./cmd/pdmterm
pdmterm

runterm:
go run ./cmd/pdmterm

pdmclient:
go build -o $(BIN_DIR)/pdmclient cmd/pdmclient/main.go
Expand Down
2 changes: 1 addition & 1 deletion cmd/add_users/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/rivo/tview"

"github.com/grd/FreePDM/pkg/config"
"github.com/grd/FreePDM/internal/config"
)

// Script for user management.
Expand Down
2 changes: 1 addition & 1 deletion cmd/createvault/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os"
"strings"

"github.com/grd/FreePDM/pkg/util"
"github.com/grd/FreePDM/internal/util"
)

// Script for creating a new PDM vault.
Expand Down
1 change: 1 addition & 0 deletions cmd/pdmserver/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func createLogFile() {
logFileName := path.Join(freePdmDir, fmt.Sprintf("logs/%s.log", today))

fmt.Printf("logFileName = %s\n", logFileName)

// Open or create the log file
var err error
logFile, err = os.OpenFile(logFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
Expand Down
2 changes: 1 addition & 1 deletion cmd/pdmserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"log"
"net/http"

"github.com/grd/FreePDM/pkg/shared"
"github.com/grd/FreePDM/internal/shared"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/pdmterm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"fmt"
"net/http"

"github.com/grd/FreePDM/pkg/shared"
"github.com/grd/FreePDM/internal/shared"
)

func sendCommand(command string, params map[string]string) (*shared.CommandResponse, error) {
Expand Down
87 changes: 60 additions & 27 deletions cmd/pdmterm/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ var (
)

// handleCommand processes the input command and executes corresponding actions.
func handleCommand(input string, directory *string) {
func handleCommand(input string, directory string) {
// Split the command and arguments
parts := strings.Fields(input)
if len(parts) == 0 {
fmt.Println(Red + "No command entered.")
fmt.Println(Red + "No command entered." + Reset)
return
}

Expand All @@ -63,16 +63,18 @@ func handleCommand(input string, directory *string) {
handleList()
case "vault":
handleVault(args[0])
case "tree":
handleTree(args[0])
case "pwd":
handlePwd()
case "ls":
handleLs(*directory)
handleLs(directory)
case "cd":
if len(args) < 1 {
fmt.Println(Cyan + "Usage: cd <directory>" + Reset)
return
}
handleCd(args[0], directory)
handleCd(args[0])
case "exit", "quit":
fmt.Println(Cyan + "Exiting the shell." + Reset)
os.Exit(0)
Expand Down Expand Up @@ -115,19 +117,51 @@ func handleList() {
resp, err := sendCommand("list", nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if resp.Status == "error" {
fmt.Println(Red + resp.Message + Reset)
if resp.Error == "Failed to show the list of vaults" {
fmt.Println(Red + resp.Error + Reset)
} else {
fmt.Printf(Cyan+"Data = %s\n"+Reset, resp.Data)
}
}

// handleVault changes the current vault.
func handleVault(vault string) {
currentVault = vault
fmt.Printf(Cyan+"Switched to vault: %s\n", vault)
resp, err := sendCommand("list", nil)
if err != nil {
fmt.Println(err)
}
if resp.Error == "Failed to show the list of vaults" {
fmt.Println(Cyan + resp.Error + Reset)
return
}

for _, elem := range resp.Data {
if vault == elem {
currentVault = elem
currentDir = "."
break
}
}
fmt.Printf(BrightGreen+"Switched to vault: %s\n"+Reset, vault)
}

// handleTree changes the current vault.
func handleTree(path string) {
resp, err := sendCommand("tree", map[string]string{
"path": path,
})
if err != nil {
fmt.Println(err)
}
if resp.Error == "Failed to show the tree" {
fmt.Println(Cyan + resp.Error + Reset)
return
}

for _, elem := range resp.Data {
fmt.Println(elem)
}
}

// handleLs lists files and directories in the current directory.
Expand All @@ -137,45 +171,44 @@ func handleLs(directory string) {
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Status = %s\n", resp.Status)
fmt.Printf("Message = %s\n", resp.Message)
fmt.Printf("Error = %s\n", resp.Error)
fmt.Printf("Data = %s\n", resp.Data)
}

// handleCd changes the current working directory.
func handleCd(target string, directory *string) {
func handleCd(target string) {
fmt.Printf("dir = %s\n", target)
if currentVault == "" {
fmt.Println(Red + "First set the vault with the command vault" + Reset)
return
}
resp, err := sendCommand("direxists", map[string]string{
"path": *directory,
"path": target,
})
if err != nil {
fmt.Println(err)
}
if resp.Status != "success" {
fmt.Println(Red + "directory does not exist" + Reset)
if resp.Error != "" {
fmt.Printf(Red+"directory %s does not exist\n"+Reset, target)
return
}
if target == ".." { // Up one level
if *directory != "/" {
lastSlash := strings.LastIndex(*directory, "/")
*directory = (*directory)[:lastSlash]
if *directory == "" {
*directory = "/"
if target != "/" {
lastSlash := strings.LastIndex(target, "/")
target = (target)[:lastSlash]
if target == "" {
target = "/"
}
}
} else { // Subdirectory
if *directory == "/" {
*directory += target
if target == "/" {
target += target
} else {
*directory += "/" + target
target += "/" + target
}
}
fmt.Printf(Cyan+"Changed directory to: %s\n", *directory)
fmt.Printf(Cyan+"Changed directory to: %s\n", target)
}

// handlePwd prints the current working directory.
Expand All @@ -193,12 +226,12 @@ func newPrompt() {

for {
// Show prompt
fmt.Printf("\033[96m%s:%s> \033[0m", currentVault, currentDir)
fmt.Printf(BrightCyan+"%s:%s>"+Reset, currentVault, currentDir)

// Read input
if scanner.Scan() {
input := strings.TrimSpace(scanner.Text())
handleCommand(input, &currentDir)
handleCommand(input, currentDir)
} else {
break
}
Expand Down
37 changes: 37 additions & 0 deletions cmd/pdmterm/tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

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

// tree recursively prints the directory structure starting from root
func tree(path string, indent string) error {
entries, err := os.ReadDir(path) // ReadDir returns a slice of fs.DirEntry
if err != nil {
return err
}

for i, entry := range entries {
isLast := i == len(entries)-1
prefix := "├── "
nextIndent := indent + "│ "

if isLast {
prefix = "└── "
nextIndent = indent + " "
}

fmt.Println(indent + prefix + entry.Name())

if entry.IsDir() {
err := tree(filepath.Join(path, entry.Name()), nextIndent)
if err != nil {
return err
}
}
}

return nil
}
4 changes: 2 additions & 2 deletions cmd/removevault/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"log"
"os"

"github.com/grd/FreePDM/pkg/config"
"github.com/grd/FreePDM/pkg/util"
"github.com/grd/FreePDM/internal/config"
"github.com/grd/FreePDM/internal/util"
)

// A simple "quick and dirty" remove all files from testvault script
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/config/config.go → internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"path"

"github.com/BurntSushi/toml"
"github.com/grd/FreePDM/pkg/util"
"github.com/grd/FreePDM/internal/util"
)

var (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"path"
"testing"

"github.com/grd/FreePDM/pkg/util"
"github.com/grd/FreePDM/internal/util"
"github.com/stretchr/testify/assert"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 285d55c

Please sign in to comment.