Borg/cmd/serve.go
google-labs-jules[bot] 52c0fa6b6d feat: Improve test coverage and refactor for testability
This commit introduces a significant refactoring of the `cmd` package to improve testability and increases test coverage across the application.

Key changes include:
- Refactored Cobra commands to use `RunE` for better error handling and testing.
- Extracted business logic from command handlers into separate, testable functions.
- Added comprehensive unit tests for the `cmd`, `compress`, `github`, `logger`, and `pwa` packages.
- Added tests for missing command-line arguments, as requested.
- Implemented the `borg all` command to clone all public repositories for a GitHub user or organization.
- Restored and improved the `collect pwa` functionality.
- Removed duplicate code and fixed various bugs.
- Addressed a resource leak in the `all` command.
- Improved error handling in the `pwa` package.
- Refactored `main.go` to remove duplicated logic.
- Fixed several other minor bugs and inconsistencies.
2025-11-03 18:25:04 +00:00

68 lines
1.5 KiB
Go

package cmd
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/Snider/Borg/pkg/compress"
"github.com/Snider/Borg/pkg/datanode"
"github.com/Snider/Borg/pkg/tarfs"
"github.com/spf13/cobra"
)
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve [file]",
Short: "Serve a packaged PWA file",
Long: `Serves the contents of a packaged PWA file using a static file server.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
dataFile := args[0]
port, _ := cmd.Flags().GetString("port")
rawData, err := os.ReadFile(dataFile)
if err != nil {
fmt.Printf("Error reading data file: %v\n", err)
return
}
data, err := compress.Decompress(rawData)
if err != nil {
fmt.Printf("Error decompressing data: %v\n", err)
return
}
var fs http.FileSystem
if strings.HasSuffix(dataFile, ".matrix") {
fs, err = tarfs.New(data)
if err != nil {
fmt.Printf("Error creating TarFS from matrix tarball: %v\n", err)
return
}
} else {
dn, err := datanode.FromTar(data)
if err != nil {
fmt.Printf("Error creating DataNode from tarball: %v\n", err)
return
}
fs = http.FS(dn)
}
http.Handle("/", http.FileServer(fs))
fmt.Printf("Serving PWA on http://localhost:%s\n", port)
err = http.ListenAndServe(":"+port, nil)
if err != nil {
fmt.Printf("Error starting server: %v\n", err)
return
}
},
}
func init() {
RootCmd.AddCommand(serveCmd)
serveCmd.PersistentFlags().String("port", "8080", "Port to serve the PWA on")
}