Skip to content

Commit

Permalink
Add notes to installation and removal (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
jolheiser authored Sep 29, 2022
1 parent b3c3df5 commit 0e79232
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 38 deletions.
6 changes: 5 additions & 1 deletion cmd/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ webman add [email protected] [email protected] [email protected]`,
}
}
}
if !InstallAllPkgs(cfg.PkgRepos, args) {
pkgs := InstallAllPkgs(cfg.PkgRepos, args)
for _, pkg := range pkgs {
fmt.Print(pkg.InstallNotes())
}
if len(args) != len(pkgs) {
return errors.New("Not all packages installed successfully")
}
color.Green("All %d packages are installed!", len(args))
Expand Down
49 changes: 26 additions & 23 deletions cmd/add/install_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"github.com/fatih/color"
)

func InstallAllPkgs(pkgRepos []*config.PkgRepo, args []string) bool {
func InstallAllPkgs(pkgRepos []*config.PkgRepo, args []string) []*pkgparse.PkgConfig {
var wg sync.WaitGroup
ml := multiline.New(len(args), os.Stdout)
wg.Add(len(args))
results := make(chan bool, len(args))
results := make(chan *pkgparse.PkgConfig, len(args))
for i, arg := range args {
i := i
arg := arg
Expand All @@ -32,19 +32,22 @@ func InstallAllPkgs(pkgRepos []*config.PkgRepo, args []string) bool {
}()
}
wg.Wait()
success := true
pkgs := make([]*pkgparse.PkgConfig, 0, len(args))
for i := 0; i < len(args); i++ {
success = success && <-results
res := <-results
if res != nil {
pkgs = append(pkgs, res)
}
}
return success
return pkgs
}

func InstallPkg(pkgRepos []*config.PkgRepo, arg string, argIndex int, argCount int, wg *sync.WaitGroup, ml *multiline.MultiLogger) bool {
func InstallPkg(pkgRepos []*config.PkgRepo, arg string, argIndex int, argCount int, wg *sync.WaitGroup, ml *multiline.MultiLogger) *pkgparse.PkgConfig {
defer wg.Done()
pkg, ver, err := utils.ParsePkgVer(arg)
if err != nil {
ml.Printf(argIndex, color.RedString(err.Error()))
return false
return nil
}
if len(ver) == 0 {
ml.SetPrefix(argIndex, color.CyanString(pkg)+": ")
Expand All @@ -61,13 +64,13 @@ func InstallPkg(pkgRepos []*config.PkgRepo, arg string, argIndex int, argCount i
foundRecipe <- true
if err != nil {
ml.Printf(argIndex, color.RedString("%v", err))
return false
return nil
}
pkgOS := pkgparse.GOOStoPkgOs[utils.GOOS]
for _, ignorePair := range pkgConf.Ignore {
if pkgOS == ignorePair.Os && utils.GOARCH == ignorePair.Arch {
ml.Printf(argIndex, color.RedString("unsupported OS + Arch for this package"))
return false
return nil
}
}
if len(ver) == 0 || pkgConf.ForceLatest {
Expand All @@ -81,20 +84,20 @@ func InstallPkg(pkgRepos []*config.PkgRepo, arg string, argIndex int, argCount i
foundLatest <- true
if err != nil {
ml.Printf(argIndex, color.RedString("unable to find latest version tag: %v", err))
return false
return nil
}
if pkgConf.ForceLatest && len(ver) != 0 && *verPtr != ver {
ml.Printf(argIndex, color.RedString("This package requires using the latest version, which is currently %s",
color.MagentaString(*verPtr)))
return false
return nil
}
ver = *verPtr
ml.Printf(argIndex, "Found %s version tag: %s", color.CyanString(pkg), color.MagentaString(ver))
}
stemPtr, extPtr, urlPtr, err := pkgConf.GetAssetStemExtUrl(ver)
if err != nil {
ml.Printf(argIndex, color.RedString("%v", err))
return false
return nil
}
stem := *stemPtr
ext := *extPtr
Expand All @@ -112,10 +115,10 @@ func InstallPkg(pkgRepos []*config.PkgRepo, arg string, argIndex int, argCount i
// If file exists
if _, err := os.Stat(extractPath); !os.IsNotExist(err) {
ml.Printf(argIndex, color.HiBlackString("Already installed!"))
return true
return pkgConf
}
if !DownloadUrl(url, downloadPath, pkg, ver, argIndex, argCount, ml) {
return false
return nil
}
var isRawBinary bool
if m, ok := pkgConf.OsMap[pkgOS]; ok {
Expand All @@ -124,19 +127,19 @@ func InstallPkg(pkgRepos []*config.PkgRepo, arg string, argIndex int, argCount i
if isRawBinary {
if err = os.Chmod(downloadPath, 0o755); err != nil {
ml.Printf(argIndex, color.RedString("Failed to make download executable!"))
return false
return nil
}
if err = os.MkdirAll(extractPath, os.ModePerm); err != nil {
ml.Printf(argIndex, color.RedString("Failed to create package-version path!"))
return false
return nil
}
binPath := filepath.Join(extractPath, pkgConf.Title)
if utils.GOOS == "windows" {
binPath += ".exe"
}
if err = os.Rename(downloadPath, binPath); err != nil {
ml.Printf(argIndex, color.RedString("Failed to rename temporary download to new path!"))
return false
return nil
}
} else {
hasUnpacked := make(chan bool)
Expand All @@ -154,7 +157,7 @@ func InstallPkg(pkgRepos []*config.PkgRepo, arg string, argIndex int, argCount i
if err != nil {
ml.Printf(argIndex, color.RedString("%v", err))
cleanUpFailedInstall(pkg, extractPath)
return false
return nil
}
ml.Printf(argIndex, "Completed unpacking %s@%s", color.CyanString(pkg), color.MagentaString(ver))
}
Expand All @@ -168,29 +171,29 @@ func InstallPkg(pkgRepos []*config.PkgRepo, arg string, argIndex int, argCount i
if err != nil {
cleanUpFailedInstall(pkg, extractPath)
ml.Printf(argIndex, color.RedString("%v", err))
return false
return nil
}
renames, err := pkgConf.GetRenames()
if err != nil {
ml.Printf(argIndex, color.RedString("Failed creating links: %v", err))
return false
return nil
}
madeLinks, err := link.CreateLinks(pkg, ver, binPaths, renames)
if err != nil {
cleanUpFailedInstall(pkg, extractPath)
ml.Printf(argIndex, color.RedString("Failed creating links: %v", err))
return false
return nil
}
if !madeLinks {
cleanUpFailedInstall(pkg, extractPath)
ml.Printf(argIndex, color.RedString("Failed creating links"))
return false
return nil
}
ml.Printf(argIndex, "Now using %s@%s", color.CyanString(pkg), color.MagentaString(ver))
}
ml.Printf(argIndex, color.GreenString("Successfully installed!"))
if p, err := exec.LookPath(pkg); err == nil && !strings.Contains(p, utils.WebmanBinDir) {
ml.Printf(argIndex, color.YellowString("Found another binary at %q that may interfere", p))
}
return true
return pkgConf
}
4 changes: 2 additions & 2 deletions cmd/dev/bintest/bintest.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ The "bintest" tests that binary paths given in a package recipe have valid binar
return err
}
pkg := args[0]
var pairResults map[string]bool = map[string]bool{}
pairResults := make(map[string]bool)
if err := check.CheckPkgConfig(pkg); err != nil {
color.Red("Pkg Config Error: %v", err)
}
Expand Down Expand Up @@ -86,7 +86,7 @@ The "bintest" tests that binary paths given in a package recipe have valid binar
var wg sync.WaitGroup
ml := multiline.New(len(args), os.Stdout)
wg.Add(1)
pairResults[osPairStr] = add.InstallPkg(cfg.PkgRepos, pkg+"@"+*latestVer, 0, 1, &wg, &ml)
pairResults[osPairStr] = add.InstallPkg(cfg.PkgRepos, pkg+"@"+*latestVer, 0, 1, &wg, &ml) != nil

relbinPaths, err := pkgConf.GetMyBinPaths()
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions cmd/group/add/group_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package add
import (
"errors"
"fmt"
"os"

"github.com/candrewlee14/webman/cmd/add"
"github.com/candrewlee14/webman/config"
Expand Down Expand Up @@ -84,9 +83,12 @@ The "group add" subcommand installs a group of packages.
if len(pkgsToInstall) == 0 {
color.HiBlack("No packages selected for installation.")
} else {
if !add.InstallAllPkgs(cfg.PkgRepos, pkgsToInstall) {
color.Magenta("Not all packages installed successfully")
os.Exit(1)
pkgs := add.InstallAllPkgs(cfg.PkgRepos, pkgsToInstall)
for _, pkg := range pkgs {
fmt.Print(pkg.InstallNotes())
}
if len(pkgs) != len(pkgsToInstall) {
return errors.New("Not all packages installed successfully")
}
color.Green("All %d selected packages from group %s are installed", len(pkgsToInstall), color.YellowString(group))
}
Expand Down
1 change: 1 addition & 0 deletions cmd/group/remove/group_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The "group remove" subcommand removes a group of packages.
return err
}
if removed {
fmt.Print(pkgConf.RemoveNotes())
fmt.Println("Removed", color.CyanString(pkg))
} else {
color.HiBlack("%s was not previously installed", pkg)
Expand Down
1 change: 1 addition & 0 deletions cmd/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ webman remove rg`,
}
}
}
fmt.Print(pkgConf.RemoveNotes())
fmt.Printf("All %d selected packages are uninstalled.\n", len(pkgVerStems))
return nil
},
Expand Down
8 changes: 5 additions & 3 deletions cmd/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ The "search" subcommand starts an interactive window to find and display info ab
color.HiBlack("No package selected.")
return nil
}
pkg := pkgInfos[idx].Title
pkgName := pkgInfos[idx].Title
prompt := &survey.Confirm{
Message: "Would you like to install the latest version of " + color.CyanString(pkg) + "?",
Message: "Would you like to install the latest version of " + color.CyanString(pkgName) + "?",
}
shouldInstall := false
if err := survey.AskOne(prompt, &shouldInstall); err != nil || !shouldInstall {
Expand All @@ -115,9 +115,11 @@ The "search" subcommand starts an interactive window to find and display info ab
var wg sync.WaitGroup
ml := multiline.New(1, os.Stdout)
wg.Add(1)
if !add.InstallPkg(cfg.PkgRepos, pkg, 0, 1, &wg, &ml) {
pkg := add.InstallPkg(cfg.PkgRepos, pkgName, 0, 1, &wg, &ml)
if pkg == nil {
return errors.New("failed to install pkg")
}
fmt.Print(pkg.InstallNotes())
return nil
},
}
Expand Down
51 changes: 46 additions & 5 deletions pkgparse/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"path/filepath"
"regexp"
"strings"

"github.com/candrewlee14/webman/config"
"github.com/candrewlee14/webman/utils"

"github.com/fatih/color"
"gopkg.in/yaml.v3"
)

Expand All @@ -32,6 +33,8 @@ type OsInfo struct {
IsRawBinary bool `yaml:"is_raw_binary"`
FilenameFormatOverride string `yaml:"filename_format_override"`
Renames []RenameItem `yaml:"renames"`
InstallNote string `yaml:"install_note"`
RemoveNote string `yaml:"remove_note"`
}

type OsArchPair struct {
Expand All @@ -40,9 +43,11 @@ type OsArchPair struct {
}

type PkgConfig struct {
Title string
Tagline string
About string
Title string `yaml:"-"`
Tagline string `yaml:"tagline"`
About string `yaml:"about"`
InstallNote string `yaml:"install_note"`
RemoveNote string `yaml:"remove_note"`

InfoUrl string `yaml:"info_url"`
ReleasesUrl string `yaml:"releases_url"`
Expand All @@ -64,6 +69,42 @@ type PkgConfig struct {
Ignore []OsArchPair `yaml:"ignore"`
}

func (pkgConf *PkgConfig) InstallNotes() string {
var installNotes string

pkgOS := GOOStoPkgOs[utils.GOOS]
note := pkgConf.InstallNote
osNote := pkgConf.OsMap[pkgOS].InstallNote
if note != "" || osNote != "" {
installNotes += color.BlueString("== %s\n", pkgConf.Title)
}
if note != "" {
installNotes += color.YellowString(note) + "\n"
}
if osNote != "" {
installNotes += color.YellowString(osNote) + "\n"
}
return installNotes
}

func (pkgConf *PkgConfig) RemoveNotes() string {
var removeNotes string

pkgOS := GOOStoPkgOs[utils.GOOS]
note := pkgConf.RemoveNote
osNote := pkgConf.OsMap[pkgOS].RemoveNote
if note != "" || osNote != "" {
removeNotes += color.BlueString("== %s\n", pkgConf.Title)
}
if note != "" {
removeNotes += color.YellowString(note) + "\n"
}
if osNote != "" {
removeNotes += color.YellowString(osNote) + "\n"
}
return removeNotes
}

var GOOStoPkgOs = map[string]string{
"darwin": "macos",
"windows": "win",
Expand Down Expand Up @@ -252,7 +293,7 @@ func ParseVersion(versionStr string, versionFmt string) (*string, error) {
return &matchedVer[1], nil
}

///
// /
func (pkgConf *PkgConfig) GetAssetStemExtUrl(version string) (*string, *string, *string, error) {
pkgOs, exists := GOOStoPkgOs[utils.GOOS]
if !exists {
Expand Down
16 changes: 16 additions & 0 deletions schema/pkg_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
"description": "Package description",
"type": "string"
},
"install_note": {
"description": "Installation notes for this package",
"type": "string"
},
"remove_note": {
"description": "Removal notes for this package",
"type": "string"
},
"info_url": {
"description": "URL for information/documentation",
"type": "string"
Expand Down Expand Up @@ -281,6 +289,14 @@
}

}
},
"install_note": {
"description": "Installation notes for this OS",
"type": "string"
},
"remove_note": {
"description": "Removal notes for this OS",
"type": "string"
}
}
}
Expand Down

0 comments on commit 0e79232

Please sign in to comment.