init commit
This commit is contained in:
71
cmd/remove.go
Normal file
71
cmd/remove.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/yeho/doks/internal/registry"
|
||||
"github.com/yeho/doks/internal/storage"
|
||||
)
|
||||
|
||||
var removeDelete bool
|
||||
|
||||
var removeCmd = &cobra.Command{
|
||||
Use: "remove <key>",
|
||||
Short: "Remove a document from doks",
|
||||
Long: `Remove a document from doks by its key.
|
||||
By default, only the registry entry is removed. Use --delete to also delete the stored file.`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
ValidArgsFunction: completeKeys,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
key := args[0]
|
||||
|
||||
// Load registry
|
||||
reg, err := registry.Load(cfg.RegistryPath())
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error loading registry: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Get entry
|
||||
entry, err := reg.Get(key)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Document not found: %s\n", key)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
store := storage.New(cfg.FilesDir())
|
||||
|
||||
// Remove symlink if exists
|
||||
if entry.HasSymlink {
|
||||
if err := store.RemoveSymlink(entry.OriginalPath); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not remove symlink: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete file if requested
|
||||
if removeDelete {
|
||||
if err := store.RemoveFile(entry.Filename); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not delete file: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove from registry
|
||||
if err := reg.Remove(key); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error removing from registry: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if removeDelete {
|
||||
fmt.Printf("Removed '%s' and deleted file\n", key)
|
||||
} else {
|
||||
fmt.Printf("Removed '%s' from registry (file kept in storage)\n", key)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(removeCmd)
|
||||
removeCmd.Flags().BoolVar(&removeDelete, "delete", false, "Also delete the stored file")
|
||||
}
|
||||
Reference in New Issue
Block a user