Skip to content

Commit

Permalink
Add mirror and release flags
Browse files Browse the repository at this point in the history
  • Loading branch information
cedws committed Sep 17, 2023
1 parent 88d979b commit e08c085
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
38 changes: 37 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package cmd
import (
"fmt"
"os"
"path"
"regexp"

"github.com/cedws/man-get/internal/deb"
"github.com/cedws/man-get/internal/man"
"github.com/spf13/cobra"
)
Expand All @@ -14,6 +16,27 @@ man-get 1 haproxy`

var sectionPattern = regexp.MustCompile("^[0-9]$")

var (
mirror string
release string
)

func init() {
rootCmd.Flags().StringVarP(&mirror, "mirror", "m", "https://ftp.debian.org/debian", "Debian mirror to use")
rootCmd.Flags().StringVarP(&release, "release", "r", "bookworm", "Debian release to use")
}

func cacheDir() (string, error) {
if cacheHome, ok := os.LookupEnv("XDG_CACHE_HOME"); ok {
return cacheHome, nil
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return path.Join(home, ".cache", "man-get"), nil
}

var rootCmd = &cobra.Command{
Use: "man-get [section] <page>...",
Short: "CLI tool to grab Debian manpages",
Expand All @@ -28,7 +51,20 @@ var rootCmd = &cobra.Command{
sections = []string{args[0]}
}

if err := man.Fetch(sections, pages); err != nil {
cacheDir, err := cacheDir()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed: %v\n", err)
os.Exit(1)
}

client := deb.NewAptClient(
deb.WithMirror(mirror),
deb.WithDistribution(release),
deb.WithArch("amd64"),
deb.WithCacheDir(cacheDir),
)

if err := man.Fetch(client, sections, pages); err != nil {
fmt.Fprintf(os.Stderr, "Failed: %v\n", err)
os.Exit(1)
}
Expand Down
25 changes: 1 addition & 24 deletions internal/man/man.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,6 @@ func DefaultSections() []string {
}
}

func cacheDir() (string, error) {
if cacheHome, ok := os.LookupEnv("XDG_CACHE_HOME"); ok {
return cacheHome, nil
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return path.Join(home, ".cache", "man-get"), nil
}

func dataHomeDir() (string, error) {
if dataHome, ok := os.LookupEnv("XDG_DATA_HOME"); ok {
return dataHome, nil
Expand Down Expand Up @@ -67,19 +56,7 @@ func pagePath(page, section string) (string, error) {
return path.Join(sectionPath, fmt.Sprintf("%v.%v.gz", page, section)), nil
}

func Fetch(desiredSections, desiredPages []string) error {
cacheDir, err := cacheDir()
if err != nil {
return err
}

client := deb.NewAptClient(
deb.WithMirror("https://ftp.debian.org/debian"),
deb.WithDistribution("bullseye"),
deb.WithArch("amd64"),
deb.WithCacheDir(cacheDir),
)

func Fetch(client *deb.Client, desiredSections, desiredPages []string) error {
for _, page := range desiredPages {
sections, err := findSectionsForPage(client, page)
if err != nil {
Expand Down

0 comments on commit e08c085

Please sign in to comment.