search

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SearchCmd = &cobra.Command{
	Use:   "search",
	Short: "search for a package",
	Long: `
The "search" subcommand starts an interactive window to find and display info about a package`,
	Example: `webman search`,
	RunE: func(cmd *cobra.Command, args []string) error {
		if len(args) != 0 {
			return cmd.Help()
		}
		cfg, err := config.Load()
		if err != nil {
			return err
		}

		if utils.RecipeDirFlag == "" {

			for _, pkgRepo := range cfg.PkgRepos {
				shouldRefresh, err := pkgRepo.ShouldRefreshRecipes(cfg.RefreshInterval)
				if err != nil {
					return err
				}
				if shouldRefresh || doRefresh {
					color.HiBlue("Refreshing package recipes for %q...", pkgRepo.Name)
					if err = pkgRepo.RefreshRecipes(); err != nil {
						color.Red("%v", err)
					}
				}
			}
		}
		pkgInfos := make([]*pkgparse.PkgConfig, 0)
		for _, pkgRepo := range cfg.PkgRepos {
			files, err := os.ReadDir(pkgRepo.PackagePath())
			if err != nil {
				return err
			}
			for _, file := range files {
				pkg := strings.Split(file.Name(), utils.PkgRecipeExt)[0]
				pkgInfo, err := pkgparse.ParsePkgConfigPath(pkgRepo.Path(), pkg)
				if err != nil {
					return err
				}
				pkgInfos = append(pkgInfos, pkgInfo)
			}
		}
		sort.Slice(pkgInfos, func(i, j int) bool {
			return pkgInfos[i].Title < pkgInfos[j].Title
		})

		installed := utils.InstalledPackages()
		installedSet := make(map[string]struct{})
		for _, i := range installed {
			installedSet[i] = struct{}{}
		}
		idx, err := fuzzyfinder.Find(
			pkgInfos,
			func(i int) string {
				pre := "   "
				if _, ok := installedSet[pkgInfos[i].Title]; ok {
					pre = "✅ "
				}
				return pre + pkgInfos[i].Title + " - " + pkgInfos[i].Tagline
			},
			fuzzyfinder.WithPreviewWindow(func(i, w, h int) string {
				if i == -1 {
					return ""
				}
				preview := fmt.Sprintf("%s: %s\n\n%s:\n %s\n\n%s:\n%s",
					"📦 Title",
					pkgInfos[i].Title,
					"💾 Tagline",
					pkgInfos[i].Tagline,
					"📄 About",
					pkgInfos[i].About,
				)
				notes := pkgInfos[i].InstallNotes()
				if notes != "" {
					preview += fmt.Sprintf("\n\n%s:\n %s",
						"📝 Notes",
						pkgInfos[i].InstallNotes(),
					)
				}
				return wrapText(preview, w)
			}))
		if err != nil {
			color.HiBlack("No package selected.")
			return nil
		}
		pkgName := pkgInfos[idx].Title
		prompt := &survey.Confirm{
			Message: "Would you like to install the latest version of " + color.CyanString(pkgName) + "?",
		}
		shouldInstall := false
		if err := survey.AskOne(prompt, &shouldInstall); err != nil || !shouldInstall {
			color.HiBlack("No package selected.")
			return nil
		}
		var wg sync.WaitGroup
		ml := multiline.New(1, os.Stdout)
		wg.Add(1)
		pkg := add.InstallPkg(cfg.PkgRepos, pkgName, 0, 1, &wg, &ml, false, false)
		if pkg == nil {
			return errors.New("failed to install pkg")
		}
		fmt.Print(pkg.PkgConf.InstallNotes())
		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL