fixed correct proton drive upload with vertical slices
This commit is contained in:
+58
-36
@@ -17,33 +17,35 @@ var (
|
||||
rootCmd = &cobra.Command{
|
||||
Use: "dirmd [flags] <directory>",
|
||||
Short: "Generate markdown documentation from directory structure",
|
||||
Long: `dirmd walks a directory and generates a markdown file with:
|
||||
Long: `dirmd walks a directory and generates markdown files with:
|
||||
|
||||
- Directory tree structure
|
||||
- All readable text files with their contents
|
||||
|
||||
Output is always written to a directory specified by -o.
|
||||
|
||||
Examples:
|
||||
|
||||
# Create new documentation
|
||||
dirmd -o output.md ~/repos/myproject
|
||||
# Create new documentation (writes index.md inside ./docs/)
|
||||
dirmd -o ./docs ~/repos/myproject
|
||||
|
||||
# Overwrite existing output directory
|
||||
dirmd -o ./docs ~/repos/myproject -f
|
||||
|
||||
# Append a single file
|
||||
dirmd -a -i README.md -o existing.md
|
||||
dirmd -a -i README.md -o ./docs
|
||||
|
||||
# Append another directory
|
||||
dirmd -a -o existing.md ~/repos/anotherproject
|
||||
# Vertical slices (root index.md + one markdown per top-level dir)
|
||||
dirmd -o ./docs ~/repos/myproject --vertical-slices
|
||||
|
||||
# Overwrite existing local file
|
||||
dirmd -o output.md ~/repos/myproject -f
|
||||
|
||||
# Generate with vertical slices (root + one file per top-level dir)
|
||||
dirmd -o output.md ~/repos/myproject --vertical-slices
|
||||
# Skip frontend files
|
||||
dirmd -o ./docs ~/repos/myproject --skip-frontend
|
||||
|
||||
# Generate and upload to Proton Drive
|
||||
dirmd -o output.md ~/repos/myproject --proton-drive
|
||||
dirmd -o ./docs ~/repos/myproject --proton-drive
|
||||
|
||||
# Generate and upload to a specific remote path
|
||||
dirmd -o output.md ~/repos/myproject --proton-drive --drive-path /my-files/docs/api.md`,
|
||||
dirmd -o ./docs ~/repos/myproject --proton-drive --drive-path /my-files/docs/api`,
|
||||
RunE: run,
|
||||
}
|
||||
|
||||
@@ -68,17 +70,17 @@ func Execute() {
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.PersistentFlags().StringVarP(&outputPath, "output", "o", "", "output markdown file path (required)")
|
||||
rootCmd.Flags().BoolVarP(&appendMode, "append", "a", false, "append to existing output file")
|
||||
rootCmd.Flags().BoolVarP(&force, "force", "f", false, "overwrite existing output file (non-append mode)")
|
||||
rootCmd.PersistentFlags().StringVarP(&outputPath, "output", "o", "", "output directory (required)")
|
||||
rootCmd.Flags().BoolVarP(&appendMode, "append", "a", false, "append to existing index.md")
|
||||
rootCmd.Flags().BoolVarP(&force, "force", "f", false, "overwrite existing output directory")
|
||||
rootCmd.Flags().StringVarP(&singleFile, "input-file", "i", "", "single file input (append mode only)")
|
||||
rootCmd.Flags().Int64Var(&maxSize, "max-size", config.DefaultMaxSize, "max file size in bytes")
|
||||
rootCmd.Flags().IntVar(&maxDepth, "max-depth", config.DefaultMaxDepth, "max directory recursion depth")
|
||||
rootCmd.Flags().StringArrayVar(&ignorePatterns, "ignore", nil, "additional ignore patterns (glob)")
|
||||
rootCmd.Flags().StringArrayVar(&extensions, "extensions", nil, "additional file extensions to skip (repeatable)")
|
||||
rootCmd.Flags().BoolVar(&protonDrive, "proton-drive", false, "upload output to Proton Drive after writing locally")
|
||||
rootCmd.Flags().StringVar(&drivePath, "drive-path", "", "full remote path on Proton Drive (e.g. /my-files/md/report.md). Requires --proton-drive")
|
||||
rootCmd.Flags().BoolVar(&verticalSlices, "vertical-slices", false, "split output into root file + one markdown per top-level directory")
|
||||
rootCmd.Flags().BoolVar(&protonDrive, "proton-drive", false, "upload output directory to Proton Drive after writing locally")
|
||||
rootCmd.Flags().StringVar(&drivePath, "drive-path", "", "full remote directory on Proton Drive (e.g. /my-files/docs/api). Requires --proton-drive")
|
||||
rootCmd.Flags().BoolVar(&verticalSlices, "vertical-slices", false, "split output into root index.md + one markdown per top-level directory")
|
||||
rootCmd.Flags().BoolVar(&skipFrontend, "skip-frontend", false, "omit frontend file types (.html, .css, .js, .ts, .vue, .svelte, etc.)")
|
||||
|
||||
rootCmd.MarkPersistentFlagRequired("output")
|
||||
@@ -135,12 +137,25 @@ func run(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
cfg.AbsRoot = abs
|
||||
|
||||
absOutput, err := filepath.Abs(outputPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to resolve output path: %w", err)
|
||||
}
|
||||
|
||||
absOutput = filepath.Join(filepath.Dir(absOutput), filepath.Base(absOutput)+"_dirmd")
|
||||
|
||||
if !appendMode {
|
||||
if _, err := os.Stat(outputPath); err == nil && !force {
|
||||
return fmt.Errorf("output file %s already exists (use -f to overwrite or -a to append)", outputPath)
|
||||
if _, err := os.Stat(absOutput); err == nil && !force {
|
||||
return fmt.Errorf("output directory %s already exists (use -f to overwrite)", absOutput)
|
||||
}
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(absOutput, 0755); err != nil {
|
||||
return fmt.Errorf("cannot create output directory: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("%s\n", absOutput)
|
||||
|
||||
result, err := walker.Run(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -151,42 +166,49 @@ func run(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
if verticalSlices {
|
||||
generated, err := renderer.WriteSlices(result, outputPath)
|
||||
generated, err := renderer.WriteSlices(result, absOutput)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "generated %d files\n", len(generated))
|
||||
fmt.Fprintf(os.Stderr, "generated %d files in %s\n", len(generated), absOutput)
|
||||
|
||||
if protonDrive {
|
||||
remoteDir := drivePath
|
||||
if remoteDir == "" {
|
||||
remoteDir = "/my-files/md/" + filepath.Base(absOutput)
|
||||
}
|
||||
|
||||
if err := drive.CheckAuth(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
fmt.Fprintln(os.Stderr, "local files were written successfully, but upload was skipped")
|
||||
return err
|
||||
}
|
||||
|
||||
for _, path := range generated {
|
||||
if err := drive.Upload(path, "/my-files/md/"+filepath.Base(path)); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error uploading %s: %v\n", path, err)
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "uploaded %s to Proton Drive\n", path)
|
||||
}
|
||||
if err := drive.UploadDir(absOutput, remoteDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
fmt.Fprintln(os.Stderr, "local files were written successfully, but upload failed")
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "uploaded %s to %s on Proton Drive\n", absOutput, remoteDir)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := renderer.Write(result, outputPath, appendMode, cfg.SingleFile); err != nil {
|
||||
indexPath := filepath.Join(absOutput, "index.md")
|
||||
|
||||
if err := renderer.Write(result, indexPath, appendMode, cfg.SingleFile); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "wrote %d entries to %s\n", len(result.Entries), outputPath)
|
||||
fmt.Fprintf(os.Stderr, "wrote %d entries to %s\n", len(result.Entries), indexPath)
|
||||
|
||||
if protonDrive {
|
||||
remotePath := drivePath
|
||||
if remotePath == "" {
|
||||
remotePath = "/my-files/md/" + filepath.Base(outputPath)
|
||||
remoteDir := drivePath
|
||||
if remoteDir == "" {
|
||||
remoteDir = "/my-files/md/" + filepath.Base(absOutput)
|
||||
}
|
||||
|
||||
if err := drive.CheckAuth(); err != nil {
|
||||
@@ -195,13 +217,13 @@ func run(cmd *cobra.Command, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := drive.Upload(outputPath, remotePath); err != nil {
|
||||
if err := drive.UploadDir(absOutput, remoteDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
fmt.Fprintln(os.Stderr, "local file was written successfully, but upload failed")
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "uploaded %s to %s on Proton Drive\n", outputPath, remotePath)
|
||||
fmt.Fprintf(os.Stderr, "uploaded %s to %s on Proton Drive\n", absOutput, remoteDir)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
+51
-7
@@ -10,12 +10,10 @@ import (
|
||||
|
||||
const binaryName = "proton-drive"
|
||||
|
||||
// Lookup returns the full path to the proton-drive binary in PATH.
|
||||
func Lookup() (string, error) {
|
||||
return exec.LookPath(binaryName)
|
||||
}
|
||||
|
||||
// CheckAuth verifies that the user is authenticated by listing /my-files.
|
||||
func CheckAuth() error {
|
||||
binary, err := Lookup()
|
||||
if err != nil {
|
||||
@@ -32,11 +30,57 @@ func CheckAuth() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Upload uploads localPath to remotePath on Proton Drive.
|
||||
// remotePath is a full path including filename (e.g. /my-files/md/report.md).
|
||||
// The CLI uploads to a parent directory and preserves the local filename,
|
||||
// so if the remote filename differs from the local one, a temp copy is staged.
|
||||
// Existing remote files are overwritten (--conflict-strategy replace).
|
||||
// EnsureRemoteParent creates the parent remote directory if it doesn't exist.
|
||||
func EnsureRemoteParent(remoteDir string) error {
|
||||
binary, err := Lookup()
|
||||
if err != nil {
|
||||
return fmt.Errorf("proton-drive not found in PATH: %w", err)
|
||||
}
|
||||
|
||||
parentDir := filepath.Dir(remoteDir)
|
||||
|
||||
// Try create-folder on the grandparent for the parent
|
||||
grandparent := filepath.Dir(parentDir)
|
||||
parentName := filepath.Base(parentDir)
|
||||
|
||||
cmd := exec.Command(binary, "filesystem", "create-folder", grandparent, parentName)
|
||||
cmd.Stdout = nil
|
||||
cmd.Stderr = nil
|
||||
cmd.Run() // Ignore errors — folder may already exist
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UploadDir uploads an entire local directory to Proton Drive.
|
||||
// The local directory's basename becomes the remote directory name.
|
||||
func UploadDir(localDir, remoteDir string) error {
|
||||
binary, err := Lookup()
|
||||
if err != nil {
|
||||
return fmt.Errorf("proton-drive not found in PATH: %w", err)
|
||||
}
|
||||
|
||||
// Ensure parent of remote dir exists
|
||||
if err := EnsureRemoteParent(remoteDir); err != nil {
|
||||
return fmt.Errorf("cannot prepare remote directory: %w", err)
|
||||
}
|
||||
|
||||
// Upload to parent — the local dir's basename creates the remote dir
|
||||
remoteParent := filepath.Dir(remoteDir)
|
||||
|
||||
cmd := exec.Command(binary, "filesystem", "upload", localDir, remoteParent,
|
||||
"--conflict-strategy", "replace")
|
||||
|
||||
var stderr strings.Builder
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("upload to proton-drive failed: %w: %s",
|
||||
err, strings.TrimSpace(stderr.String()))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Upload uploads a single local file to a remote path on Proton Drive.
|
||||
func Upload(localPath, remotePath string) error {
|
||||
binary, err := Lookup()
|
||||
if err != nil {
|
||||
|
||||
@@ -84,7 +84,7 @@ func Write(result *walker.Result, outputPath string, appendMode bool, singleFile
|
||||
|
||||
// WriteSlices generates a root markdown file plus one markdown per top-level directory.
|
||||
// Returns the list of generated file paths.
|
||||
func WriteSlices(result *walker.Result, outputPath string) ([]string, error) {
|
||||
func WriteSlices(result *walker.Result, outputDir string) ([]string, error) {
|
||||
entries := result.Entries
|
||||
if len(entries) == 0 {
|
||||
return nil, fmt.Errorf("no entries to write")
|
||||
@@ -107,15 +107,13 @@ func WriteSlices(result *walker.Result, outputPath string) ([]string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
outDir := filepath.Dir(outputPath)
|
||||
|
||||
var generated []string
|
||||
|
||||
// Write root file
|
||||
rootPath := outputPath
|
||||
// Write root file as index.md
|
||||
rootPath := filepath.Join(outputDir, "index.md")
|
||||
generated = append(generated, rootPath)
|
||||
|
||||
if err := writeRootSlice(rootPath, absRoot, rootName, rootFiles, topLevelDirs, outDir); err != nil {
|
||||
if err := writeRootSlice(rootPath, absRoot, rootName, rootFiles, topLevelDirs); err != nil {
|
||||
return nil, fmt.Errorf("failed to write root slice: %w", err)
|
||||
}
|
||||
|
||||
@@ -128,7 +126,7 @@ func WriteSlices(result *walker.Result, outputPath string) ([]string, error) {
|
||||
|
||||
for _, dirName := range dirNames {
|
||||
sliceName := strings.TrimSuffix(dirName, "/") + ".md"
|
||||
sliceDir := filepath.Join(outDir, dirName)
|
||||
sliceDir := filepath.Join(outputDir, dirName)
|
||||
if err := os.MkdirAll(sliceDir, 0755); err != nil {
|
||||
return nil, fmt.Errorf("cannot create slice directory %s: %w", sliceDir, err)
|
||||
}
|
||||
@@ -141,13 +139,18 @@ func WriteSlices(result *walker.Result, outputPath string) ([]string, error) {
|
||||
// Strip dirName prefix for tree building only
|
||||
treeEntries := make([]walker.Entry, len(dirEntries))
|
||||
for i, e := range dirEntries {
|
||||
treeEntries[i] = walker.Entry{
|
||||
RelPath: strings.TrimPrefix(e.RelPath, dirName+string(filepath.Separator)),
|
||||
Size: e.Size,
|
||||
sepIdx := strings.Index(e.RelPath, string(filepath.Separator))
|
||||
if sepIdx >= 0 {
|
||||
treeEntries[i] = walker.Entry{
|
||||
RelPath: e.RelPath[sepIdx+1:],
|
||||
Size: e.Size,
|
||||
}
|
||||
} else {
|
||||
treeEntries[i] = e
|
||||
}
|
||||
}
|
||||
|
||||
if err := writeDirSlice(slicePath, absDir, absRoot, dirName, treeEntries, dirEntries, filepath.Base(outputPath)); err != nil {
|
||||
if err := writeDirSlice(slicePath, absDir, absRoot, dirName, treeEntries, dirEntries, "index.md"); err != nil {
|
||||
return nil, fmt.Errorf("failed to write slice %s: %w", sliceName, err)
|
||||
}
|
||||
}
|
||||
@@ -155,7 +158,7 @@ func WriteSlices(result *walker.Result, outputPath string) ([]string, error) {
|
||||
return generated, nil
|
||||
}
|
||||
|
||||
func writeRootSlice(path, absRoot, rootName string, rootFiles []walker.Entry, topLevelDirs map[string][]walker.Entry, outDir string) error {
|
||||
func writeRootSlice(path, absRoot, rootName string, rootFiles []walker.Entry, topLevelDirs map[string][]walker.Entry) error {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create root file: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user