Skip to content

Commit fdad8a6

Browse files
committed
Implemented the new info command
1 parent df06a39 commit fdad8a6

2 files changed

Lines changed: 147 additions & 1 deletion

File tree

main.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,51 @@ func main() {
675675

676676
settingsCmd.AddCommand(settingsInfoCmd, settingsSetCmd)
677677

678+
// --- INFO ---
679+
var infoCmd = &cobra.Command{
680+
Use: "info [file-path]",
681+
Short: "Display vault or file information",
682+
Long: `Display information about your vault or a specific file.
683+
684+
Without arguments: Shows vault statistics (file/folder counts, username) and settings.
685+
With file-path: Shows detailed file information (name, storage ID, encrypted size, file key).
686+
687+
Examples:
688+
zep info # Show vault statistics and settings
689+
zep info documents/file.pdf # Show file information`,
690+
Args: cobra.MaximumNArgs(1),
691+
Run: func(cmd *cobra.Command, args []string) {
692+
// 1. Check if the config file exists BEFORE starting
693+
_, err := os.Stat("zephyrus.conf")
694+
isPersistent := err == nil
695+
696+
session, err := getEffectiveSession()
697+
if err != nil {
698+
fmt.Printf("❌ Authentication failed: %v\n", err)
699+
return
700+
}
701+
702+
if len(args) == 0 {
703+
// Show general vault information
704+
utils.PrintVaultInfo(session)
705+
} else {
706+
// Show specific file information
707+
filePath := args[0]
708+
fileInfo, err := utils.GetFileInfo(filePath, session)
709+
if err != nil {
710+
fmt.Printf("❌ Failed to get file info: %v\n", err)
711+
return
712+
}
713+
utils.PrintFileInfo(fileInfo)
714+
}
715+
716+
// Save session if persistent
717+
if isPersistent {
718+
session.Save()
719+
}
720+
},
721+
}
722+
678723
// --- SHELL ---
679724
var shellCmd = &cobra.Command{
680725
Use: "shell [username]",
@@ -692,7 +737,7 @@ func main() {
692737
rootCmd.AddCommand(
693738
setupCmd, connectCmd, disconnectCmd,
694739
uploadCmd, downloadCmd, deleteCmd,
695-
listCmd, searchCmd, purgeCmd, shareCmd, readCmd, sharedCmd, settingsCmd,
740+
listCmd, searchCmd, purgeCmd, shareCmd, readCmd, sharedCmd, settingsCmd, infoCmd,
696741
shellCmd,
697742
)
698743

utils/info.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// VaultStats holds statistics about the vault
9+
type VaultStats struct {
10+
TotalFiles int
11+
TotalFolders int
12+
TotalSize int64
13+
}
14+
15+
// GetVaultStats calculates statistics about the vault
16+
func GetVaultStats(session *Session) VaultStats {
17+
stats := VaultStats{}
18+
19+
// Recursively count files and folders
20+
var countEntries func(Entry)
21+
countEntries = func(e Entry) {
22+
if e.Type == "file" {
23+
stats.TotalFiles++
24+
} else {
25+
stats.TotalFolders++
26+
for _, subEntry := range e.Contents {
27+
countEntries(subEntry)
28+
}
29+
}
30+
}
31+
32+
// Count all entries in the index
33+
for _, entry := range session.Index {
34+
countEntries(entry)
35+
}
36+
37+
return stats
38+
}
39+
40+
// GetFileInfo retrieves detailed information about a specific file
41+
func GetFileInfo(vaultPath string, session *Session) (map[string]interface{}, error) {
42+
entry, err := session.Index.FindEntry(vaultPath)
43+
if err != nil {
44+
return nil, fmt.Errorf("could not find file in vault: %w", err)
45+
}
46+
47+
if entry.Type == "folder" {
48+
return nil, fmt.Errorf("'%s' is a directory, not a file", vaultPath)
49+
}
50+
51+
// Fetch file from remote to get size
52+
PrintProgressStep(1, 2, "Fetching file metadata...")
53+
encryptedData, err := FetchRaw(session.Username, entry.RealName)
54+
if err != nil {
55+
return nil, fmt.Errorf("failed to fetch file from remote: %w", err)
56+
}
57+
PrintCompletionLine("File metadata retrieved")
58+
59+
info := map[string]interface{}{
60+
"name": strings.Split(vaultPath, "/")[len(strings.Split(vaultPath, "/"))-1],
61+
"vaultPath": vaultPath,
62+
"storageID": entry.RealName,
63+
"encryptedSize": len(encryptedData),
64+
"fileKey": entry.FileKey,
65+
}
66+
67+
return info, nil
68+
}
69+
70+
// PrintVaultInfo prints formatted vault information
71+
func PrintVaultInfo(session *Session) {
72+
stats := GetVaultStats(session)
73+
74+
fmt.Println("\n╔════════════════════════════════════════╗")
75+
fmt.Println("║ VAULT INFORMATION ║")
76+
fmt.Println("╚════════════════════════════════════════╝")
77+
fmt.Printf("Username: %s\n", session.Username)
78+
fmt.Printf("Total Files: %d\n", stats.TotalFiles)
79+
fmt.Printf("Total Folders: %d\n", stats.TotalFolders)
80+
fmt.Println("\n╔════════════════════════════════════════╗")
81+
fmt.Println("║ VAULT SETTINGS ║")
82+
fmt.Println("╚════════════════════════════════════════╝")
83+
fmt.Printf("Commit Author: %s <%s>\n", session.Settings.CommitAuthorName, session.Settings.CommitAuthorEmail)
84+
fmt.Printf("Commit Message: %s\n", session.Settings.CommitMessage)
85+
fmt.Printf("File Hash Length: %d characters\n", session.Settings.FileHashLength)
86+
fmt.Printf("Share Hash Length: %d characters\n", session.Settings.ShareHashLength)
87+
fmt.Println()
88+
}
89+
90+
// PrintFileInfo prints formatted file information
91+
func PrintFileInfo(fileInfo map[string]interface{}) {
92+
fmt.Println("\n╔════════════════════════════════════════╗")
93+
fmt.Println("║ FILE INFORMATION ║")
94+
fmt.Println("╚════════════════════════════════════════╝")
95+
fmt.Printf("File Name: %s\n", fileInfo["name"])
96+
fmt.Printf("Vault Path: %s\n", fileInfo["vaultPath"])
97+
fmt.Printf("Storage ID (Hash): %s\n", fileInfo["storageID"])
98+
fmt.Printf("Encrypted Size: %d bytes\n", fileInfo["encryptedSize"])
99+
fmt.Printf("File Key (encrypted): %s\n", fileInfo["fileKey"])
100+
fmt.Println()
101+
}

0 commit comments

Comments
 (0)