file_commands.go
1 package commands 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "strings" 8 "time" 9 ) 10 11 // LsCommand lists directory contents 12 type LsCommand struct{} 13 14 func (c *LsCommand) Handle(args []string) (Message, error) { 15 path := "." 16 if len(args) > 0 { 17 path = args[0] 18 } 19 20 entries, err := os.ReadDir(path) 21 if err != nil { 22 return Message{ 23 Role: "system", 24 Content: fmt.Sprintf("❌ Could not list directory: %s", err.Error()), 25 Timestamp: time.Now(), 26 }, nil 27 } 28 29 var result strings.Builder 30 result.WriteString(fmt.Sprintf("📂 Files in %s:\n", path)) 31 for _, entry := range entries { 32 if entry.IsDir() { 33 result.WriteString(fmt.Sprintf(" 📁 %s/\n", entry.Name())) 34 } else { 35 info, _ := entry.Info() 36 size := int64(0) 37 if info != nil { 38 size = info.Size() 39 } 40 result.WriteString(fmt.Sprintf(" 📄 %s (%d bytes)\n", entry.Name(), size)) 41 } 42 } 43 44 return Message{ 45 Role: "system", 46 Content: result.String(), 47 Timestamp: time.Now(), 48 }, nil 49 } 50 51 func (c *LsCommand) Usage() string { 52 return "/ls [path]" 53 } 54 55 func (c *LsCommand) Description() string { 56 return "List files in directory" 57 } 58 59 // CatCommand displays file contents 60 type CatCommand struct{} 61 62 func (c *CatCommand) Handle(args []string) (Message, error) { 63 if len(args) == 0 { 64 return Message{ 65 Role: "system", 66 Content: "❌ Usage: /cat <filename>", 67 Timestamp: time.Now(), 68 }, nil 69 } 70 71 content, err := os.ReadFile(args[0]) 72 if err != nil { 73 return Message{ 74 Role: "system", 75 Content: fmt.Sprintf("❌ Could not read file: %s", err.Error()), 76 Timestamp: time.Now(), 77 }, nil 78 } 79 80 return Message{ 81 Role: "system", 82 Content: fmt.Sprintf("📄 %s:\n```\n%s\n```", args[0], string(content)), 83 Timestamp: time.Now(), 84 }, nil 85 } 86 87 func (c *CatCommand) Usage() string { 88 return "/cat <filename>" 89 } 90 91 func (c *CatCommand) Description() string { 92 return "Show file contents" 93 } 94 95 // FindCommand finds files by pattern 96 type FindCommand struct{} 97 98 func (c *FindCommand) Handle(args []string) (Message, error) { 99 if len(args) == 0 { 100 return Message{ 101 Role: "system", 102 Content: "❌ Usage: /find <pattern>", 103 Timestamp: time.Now(), 104 }, nil 105 } 106 107 pattern := args[0] 108 var result strings.Builder 109 result.WriteString(fmt.Sprintf("🔍 Files matching '%s':\n", pattern)) 110 111 err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error { 112 if err != nil { 113 return nil 114 } 115 if matched, _ := filepath.Match(pattern, info.Name()); matched { 116 if info.IsDir() { 117 result.WriteString(fmt.Sprintf(" 📁 %s/\n", path)) 118 } else { 119 result.WriteString(fmt.Sprintf(" 📄 %s (%d bytes)\n", path, info.Size())) 120 } 121 } 122 return nil 123 }) 124 125 if err != nil { 126 result.WriteString(fmt.Sprintf("❌ Error searching: %s\n", err.Error())) 127 } 128 129 return Message{ 130 Role: "system", 131 Content: result.String(), 132 Timestamp: time.Now(), 133 }, nil 134 } 135 136 func (c *FindCommand) Usage() string { 137 return "/find <pattern>" 138 } 139 140 func (c *FindCommand) Description() string { 141 return "Find files by pattern" 142 } 143 144 // MkdirCommand creates directories 145 type MkdirCommand struct{} 146 147 func (c *MkdirCommand) Handle(args []string) (Message, error) { 148 if len(args) == 0 { 149 return Message{ 150 Role: "system", 151 Content: "❌ Usage: /mkdir <dirname>", 152 Timestamp: time.Now(), 153 }, nil 154 } 155 156 err := os.MkdirAll(args[0], 0755) 157 if err != nil { 158 return Message{ 159 Role: "system", 160 Content: fmt.Sprintf("❌ Could not create directory: %s", err.Error()), 161 Timestamp: time.Now(), 162 }, nil 163 } 164 165 return Message{ 166 Role: "system", 167 Content: fmt.Sprintf("✅ Created directory: %s", args[0]), 168 Timestamp: time.Now(), 169 }, nil 170 } 171 172 func (c *MkdirCommand) Usage() string { 173 return "/mkdir <dirname>" 174 } 175 176 func (c *MkdirCommand) Description() string { 177 return "Create directory" 178 } 179 180 // TouchCommand creates empty files 181 type TouchCommand struct{} 182 183 func (c *TouchCommand) Handle(args []string) (Message, error) { 184 if len(args) == 0 { 185 return Message{ 186 Role: "system", 187 Content: "❌ Usage: /touch <filename>", 188 Timestamp: time.Now(), 189 }, nil 190 } 191 192 file, err := os.Create(args[0]) 193 if err != nil { 194 return Message{ 195 Role: "system", 196 Content: fmt.Sprintf("❌ Could not create file: %s", err.Error()), 197 Timestamp: time.Now(), 198 }, nil 199 } 200 file.Close() 201 202 return Message{ 203 Role: "system", 204 Content: fmt.Sprintf("✅ Created file: %s", args[0]), 205 Timestamp: time.Now(), 206 }, nil 207 } 208 209 func (c *TouchCommand) Usage() string { 210 return "/touch <filename>" 211 } 212 213 func (c *TouchCommand) Description() string { 214 return "Create empty file" 215 }