Skip to content

Commit

Permalink
Add Download-indicator
Browse files Browse the repository at this point in the history
Fixes #7
  • Loading branch information
Tch1b0 committed Oct 15, 2021
1 parent f7df804 commit 7ee5308
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
11 changes: 8 additions & 3 deletions commands/init.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package commands

import (
"fmt"
"os"

"github.com/Glow-Project/ppm/pkg"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)

Expand All @@ -13,10 +15,13 @@ func initialize(ctx *cli.Context) error {
return err
}

innerErr := pkg.CreateNewPpmConfig(currentPath)
if innerErr != nil {
return err
err = pkg.CreateNewPpmConfig(currentPath)
if err != nil {
fmt.Println(color.RedString("ppm.json config-file already exists in this directory"))
return nil
}

fmt.Println(color.GreenString("New ppm.json config-file generated"))

return nil
}
40 changes: 26 additions & 14 deletions commands/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func install(ctx *cli.Context) error {

repo := ctx.Args().Get(0)
if len(repo) > 0 {
if config.HasDependency(repo) {
alreadyInstalled(repo)
return nil
}
installDependency(config, newPath, repo, false)
} else {
installAllDependencies(config, newPath)
Expand All @@ -52,37 +56,38 @@ func install(ctx *cli.Context) error {
}

func installAllDependencies(config pkg.PpmConfig, currentPath string) error {
loading := true
go pkg.PlayLoadingAnim(&loading)
for _, dependency := range config.Dependencies {
fmt.Printf("\rInstalling %s\n", color.YellowString(dependency))
dependency, version := pkg.GetVersionOrNot(dependency)
err := pkg.Clone(currentPath, dependency, version)
if err != nil {
installError(dependency)
return err
}
}
pkg.PrintDone()
loading = false
return nil
}

func installDependency(config pkg.PpmConfig, currentPath string, dependency string, isSubdependency bool) error {
dependency, version := pkg.GetVersionOrNot(dependency)

fmt.Printf("Installing %s\n", color.YellowString(dependency))
loading := true

go pkg.PlayLoadingAnim(&loading)
err := pkg.Clone(currentPath, dependency, version)
loading = false

var addDependency bool

if err != nil {

}

if err != nil {
switch err.Error() {
case "exit status 128":
color.GreenString("Plugin already installed")
addDependency = false

default:
return err
}
installError(dependency)
return err
} else {
pkg.PrintDone()
addDependency = true
}

Expand All @@ -102,11 +107,18 @@ func installDependency(config pkg.PpmConfig, currentPath string, dependency stri

// Iterate over dependencies and install them if needed
for _, dep := range subConfig.Dependencies {
fmt.Println(dep)
if !config.HasDependency(dep) && !config.HasSubDependency(dep) {
installDependency(config, currentPath, dep, true)
}
}

return nil
}

func alreadyInstalled(dependency string) {
fmt.Println(color.GreenString("The Plugin"), color.YellowString(dependency), color.GreenString("is already installed"))
}

func installError(dependency string) {
fmt.Printf(color.RedString("\rSome issues occured while trying to install %s"), color.YellowString(dependency))
}
5 changes: 5 additions & 0 deletions commands/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ func update(ctx *cli.Context) error {
return err
}

loading := true
go pkg.PlayLoadingAnim(&loading)
updateAllDependencies(config, newPath)
loading = false

pkg.PrintDone()

return nil
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/utility.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package pkg

import (
"fmt"
"os"
"strings"
"time"

"github.com/fatih/color"
)

// Check wether an absolute path exists
Expand Down Expand Up @@ -66,4 +70,24 @@ func GetVersionOrNot(dependency string) (string, string) {
dependencyName := splitDependency[0]

return dependencyName, version
}

func PlayLoadingAnim(loading *bool) {
animStates := []string{"|", "/", "-", "\\", "|"}
index := 0

for *loading {
fmt.Printf("\r%s", animStates[index])

if index == len(animStates)-1 {
index = 0
} else {
index++
}
time.Sleep(time.Second/10)
}
}

func PrintDone() {
fmt.Print(color.GreenString("\rDone"))
}

0 comments on commit 7ee5308

Please sign in to comment.