From 0a57fce6fa23d111aa397099a65d1b31142e1b05 Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Tue, 6 Sep 2022 13:37:55 +0200 Subject: [PATCH 01/17] init: add `.gitignore` mention --- pkg/commands/init.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/commands/init.go b/pkg/commands/init.go index 0deb3b5..98dde50 100644 --- a/pkg/commands/init.go +++ b/pkg/commands/init.go @@ -3,6 +3,7 @@ package commands import ( "errors" "fmt" + "path" "github.com/Glow-Project/ppm/pkg/utility" "github.com/fatih/color" @@ -22,5 +23,9 @@ func initialize(ctx *cli.Context) error { fmt.Println(color.GreenString("new ppm.json config-file generated")) + if ok, _ := utility.DoesPathExist(path.Join(paths.Root, ".git")); ok { + fmt.Println(color.YellowString("when using ppm it is recommended to add the addons directory to your .gitignore file")) + } + return nil } From dc18317093d873eb5e1f9e857f9e9280e6a9d0e8 Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Sun, 11 Sep 2022 17:53:32 +0200 Subject: [PATCH 02/17] rewrite dependency --- pkg/commands/install.go | 19 ++++++++--------- pkg/commands/tidy.go | 10 --------- pkg/commands/uninstall.go | 21 +++++++++---------- pkg/commands/update.go | 4 ++-- pkg/fetch/fetch.go | 16 ++++++++++++++ pkg/utility/dependency.go | 44 +++++++++++++++++++++++++++++++++++++++ pkg/utility/ppmconfig.go | 43 +++++++++++++++++++------------------- 7 files changed, 102 insertions(+), 55 deletions(-) create mode 100644 pkg/fetch/fetch.go create mode 100644 pkg/utility/dependency.go diff --git a/pkg/commands/install.go b/pkg/commands/install.go index 433bf78..0bcd4e2 100644 --- a/pkg/commands/install.go +++ b/pkg/commands/install.go @@ -21,8 +21,8 @@ func install(ctx *cli.Context) error { installAllDependencies(&config, paths) } - for _, repo := range dependencies.Slice() { - if err = installDependency(&config, paths, repo, false); err != nil { + for _, dep := range dependencies.Slice() { + if err = installDependency(&config, paths, utility.DependencyFromString(dep), false); err != nil { return err } } @@ -39,23 +39,22 @@ func installAllDependencies(config *utility.PpmConfig, paths utility.Paths) erro return nil } -func installDependency(config *utility.PpmConfig, paths utility.Paths, dependency string, isSubDependency bool) error { - dependency, version := utility.GetVersionOrNot(dependency) +func installDependency(config *utility.PpmConfig, paths utility.Paths, dependency *utility.Dependency, isSubDependency bool) error { if !isSubDependency { - fmt.Printf("\rinstalling %s\n", color.YellowString(utility.GetPluginIdentifier(dependency))) + fmt.Printf("\rinstalling %s\n", color.YellowString(utility.GetPluginIdentifier(dependency.Identifier))) } else { - fmt.Printf("\t -> installing %s\n", color.YellowString(utility.GetPluginIdentifier(dependency))) + fmt.Printf("\t -> installing %s\n", color.YellowString(utility.GetPluginIdentifier(dependency.Identifier))) } loadAnim := utility.StartLoading() - err := utility.Clone(paths.Addons, dependency, version) + err := utility.Clone(paths.Addons, dependency.Identifier, "") loadAnim.Stop() if err != nil { if err.Error() == "repository already exists" { - alreadyInstalled(dependency) + alreadyInstalled(dependency.Identifier) } else { - installError(dependency) + installError(dependency.Identifier) return err } } @@ -69,7 +68,7 @@ func installDependency(config *utility.PpmConfig, paths utility.Paths, dependenc config.AddDependency(dependency) } - subConfig, err := utility.GetPluginConfig(paths.Addons, dependency) + subConfig, err := utility.GetPluginConfig(paths.Addons, dependency.Identifier) if err != nil { if !isSubDependency { utility.PrintDone() diff --git a/pkg/commands/tidy.go b/pkg/commands/tidy.go index 5d63ccd..88a9d8f 100644 --- a/pkg/commands/tidy.go +++ b/pkg/commands/tidy.go @@ -1,9 +1,6 @@ package commands import ( - "fmt" - "strings" - "github.com/Glow-Project/ppm/pkg/utility" "github.com/urfave/cli/v2" ) @@ -14,12 +11,5 @@ func tidy(ctx *cli.Context) error { return err } - for i, dep := range config.Dependencies { - if utility.IsGithubRepoUrl(dep) { - tmp := strings.Split(dep, "/") - config.Dependencies[i] = fmt.Sprintf("%s/%s", tmp[len(tmp)-2], tmp[len(tmp)-1]) - } - } - return config.Write() } diff --git a/pkg/commands/uninstall.go b/pkg/commands/uninstall.go index 7698f7e..274c2a3 100644 --- a/pkg/commands/uninstall.go +++ b/pkg/commands/uninstall.go @@ -23,13 +23,13 @@ func uninstall(ctx *cli.Context) error { } for i := 0; i < dependencies.Len(); i++ { - dep := dependencies.Get(i) + dep := utility.DependencyFromString(dependencies.Get(i)) if config.HasDependency(dep) && !config.HasSubDependency(dep) { uninstallDependency(&config, paths, dep, false) } else if config.HasSubDependency(dep) { - fmt.Println(color.RedString("the plugin"), color.YellowString(dep), color.RedString("is a sub dependency and can only be uninstalled by uninstalling its parent")) + fmt.Println(color.RedString("the plugin"), color.YellowString(dep.Identifier), color.RedString("is a sub dependency and can only be uninstalled by uninstalling its parent")) } else { - fmt.Println(color.RedString("the plugin"), color.YellowString(dep), color.RedString("is not installed")) + fmt.Println(color.RedString("the plugin"), color.YellowString(dep.Identifier), color.RedString("is not installed")) } } @@ -53,16 +53,15 @@ func uninstallAllDependencies(config *utility.PpmConfig, paths utility.Paths, ha return nil } -func uninstallDependency(config *utility.PpmConfig, paths utility.Paths, dependency string, isSubDependency bool) error { - dep := utility.GetPluginIdentifier(dependency) +func uninstallDependency(config *utility.PpmConfig, paths utility.Paths, dependency *utility.Dependency, isSubDependency bool) error { if !isSubDependency { - fmt.Println("\runinstalling", color.YellowString(dep)) + fmt.Println("\runinstalling", color.YellowString(dependency.Identifier)) } else { - fmt.Println("\t -> uninstalling", color.YellowString(dep)) + fmt.Println("\t -> uninstalling", color.YellowString(dependency.Identifier)) } loadAnim := utility.StartLoading() - subConfig, err := utility.GetPluginConfig(paths.Addons, dep) + subConfig, err := utility.GetPluginConfig(paths.Addons, dependency.Identifier) if err == nil { for i := 0; i < len(subConfig.Dependencies); i++ { subDep := subConfig.Dependencies[i] @@ -73,16 +72,16 @@ func uninstallDependency(config *utility.PpmConfig, paths utility.Paths, depende } // path: root/addons/dependency - err = os.RemoveAll(path.Join(paths.Addons, utility.GetPluginName(dep))) + err = os.RemoveAll(path.Join(paths.Addons, dependency.Identifier)) loadAnim.Stop() if err != nil { return err } if !isSubDependency { - config.RemoveDependency(dep) + config.RemoveDependency(dependency) } else { - config.RemoveSubDependency(dep) + config.RemoveSubDependency(dependency) } if !isSubDependency { diff --git a/pkg/commands/update.go b/pkg/commands/update.go index a0ef7fc..40fdff8 100644 --- a/pkg/commands/update.go +++ b/pkg/commands/update.go @@ -28,12 +28,12 @@ func update(ctx *cli.Context) error { func updateAllDependencies(config utility.PpmConfig, paths utility.Paths) error { for _, dependency := range config.Dependencies { - _, version := utility.GetVersionOrNot(dependency) + _, version := utility.GetVersionOrNot(dependency.Identifier) if len(version) > 0 { continue } - err := utility.Update(filepath.Join(paths.Addons, dependency)) + err := utility.Update(filepath.Join(paths.Addons, dependency.Identifier)) if err != nil { return err } diff --git a/pkg/fetch/fetch.go b/pkg/fetch/fetch.go new file mode 100644 index 0000000..8185a1c --- /dev/null +++ b/pkg/fetch/fetch.go @@ -0,0 +1,16 @@ +package fetch + +import ( + "github.com/Glow-Project/ppm/pkg/utility" + "github.com/go-git/go-git/v5" +) + +func InstallDependency(dep *utility.Dependency, paths *utility.Paths) { + if dep.Type == utility.GithubAsset { + git.PlainClone(paths.Addons, false, &git.CloneOptions{ + URL: dep.Url, + }) + } else { + // TODO: Install Godot Assets Addon + } +} diff --git a/pkg/utility/dependency.go b/pkg/utility/dependency.go new file mode 100644 index 0000000..59ae219 --- /dev/null +++ b/pkg/utility/dependency.go @@ -0,0 +1,44 @@ +package utility + +import ( + "fmt" + "strings" +) + +const ( + GithubAsset = "GITHUB_ASSET" + GDAsset = "GODOT_ASSET" +) + +type Dependency struct { + Identifier string `json:"identifier"` + Url string `json:"url"` + Type string `json:"type"` +} + +func DependencyFromString(str string) *Dependency { + var identifier string + var url string + var t string + + if isGithubUrl := IsGithubRepoUrl(str); isGithubUrl || IsUserAndRepo(str) { + if isGithubUrl { + url = str + } else { + url = fmt.Sprint("https://github.com/%s", str) + } + tmp := strings.Split(str, "/") + identifier = fmt.Sprintf("%s/%s", tmp[len(tmp)-2], tmp[len(tmp)-1]) + t = GithubAsset + } else { + identifier = strings.Replace(str, " ", "-", -1) + url = fmt.Sprintf("https://godotengine.org/asset-library/api/asset?filter=%s", str) + t = GithubAsset + } + + return &Dependency{ + Identifier: identifier, + Url: url, + Type: t, + } +} diff --git a/pkg/utility/ppmconfig.go b/pkg/utility/ppmconfig.go index 704a791..e9c9661 100644 --- a/pkg/utility/ppmconfig.go +++ b/pkg/utility/ppmconfig.go @@ -13,56 +13,54 @@ import ( // representing a ppm.json configuration file type PpmConfig struct { - IsPlugin bool `json:"plugin"` - Dependencies []string `json:"dependencies"` - SubDependencies []string `json:"sub-dependencies"` + IsPlugin bool `json:"plugin"` + Dependencies []*Dependency `json:"dependencies"` + SubDependencies []*Dependency `json:"sub-dependencies"` filePath string } // add an item safely to the Dependencies property -func (ppm *PpmConfig) AddDependency(dependency string) { +func (ppm *PpmConfig) AddDependency(dependency *Dependency) { ppm.Dependencies = append(ppm.Dependencies, dependency) ppm.Write() } // add an item safely to the sub-dependencies property -func (ppm *PpmConfig) AddSubDependency(dependency string) { +func (ppm *PpmConfig) AddSubDependency(dependency *Dependency) { ppm.SubDependencies = append(ppm.SubDependencies, dependency) ppm.Write() } // remove ALL (sub)dependencies func (ppm *PpmConfig) RemoveAllDependencies() { - ppm.Dependencies = []string{} - ppm.SubDependencies = []string{} + ppm.Dependencies = []*Dependency{} + ppm.SubDependencies = []*Dependency{} ppm.Write() } // remove an item safely from the Dependencies property by its name -func (ppm *PpmConfig) RemoveSubDependency(dependency string) { - dependency = GetPluginIdentifier(dependency) - ppm.SubDependencies = Filter(ppm.SubDependencies, func(item string, _ int) bool { - return item != dependency +func (ppm *PpmConfig) RemoveSubDependency(dependency *Dependency) { + ppm.SubDependencies = Filter(ppm.SubDependencies, func(item *Dependency, _ int) bool { + return item.Identifier != dependency.Identifier }) ppm.Write() } // remove an item safely from the sub-dependencies property by its name -func (ppm *PpmConfig) RemoveDependency(dependency string) { - dependency = GetPluginIdentifier(dependency) - ppm.Dependencies = Filter(ppm.Dependencies, func(item string, _ int) bool { - return item != dependency +func (ppm *PpmConfig) RemoveDependency(dependency *Dependency) { + ppm.Dependencies = Filter(ppm.Dependencies, func(item *Dependency, _ int) bool { + return item.Identifier != dependency.Identifier }) ppm.Write() } // check wether the config file has a certain dependency -func (ppm PpmConfig) HasDependency(dependency string) bool { +func (ppm PpmConfig) HasDependency(dependency *Dependency) bool { return SliceContains(dependency, ppm.Dependencies) } // check wether the config file has a certain sub-dependency -func (ppm PpmConfig) HasSubDependency(dependency string) bool { +func (ppm PpmConfig) HasSubDependency(dependency *Dependency) bool { return SliceContains(dependency, ppm.SubDependencies) } @@ -72,10 +70,11 @@ func (ppm PpmConfig) PrettyPrint() { ppmType = "plugin" } - dependencies := SliceToString(ppm.Dependencies, ", ") - subDependencies := SliceToString(ppm.SubDependencies, ", ") + // TODO: Print dependencies + // dependencies := SliceToString(ppm.Dependencies, ", ") + // subDependencies := SliceToString(ppm.SubDependencies, ", ") - fmt.Printf("this project is a %s\ndependencies: %v\nsubdependencies: %v\n", ppmType, dependencies, subDependencies) + fmt.Printf("this project is a %s\ndependencies: %v\nsubdependencies: %v\n", ppmType) //, dependencies, subDependencies) } // write the current state of the configuartion to the config file @@ -133,8 +132,8 @@ func CreateNewPpmConfig(path string) error { config := PpmConfig{ IsPlugin: isPlugin, - Dependencies: []string{}, - SubDependencies: []string{}, + Dependencies: []*Dependency{}, + SubDependencies: []*Dependency{}, } content, err := json.MarshalIndent(config, "", " ") From 906ac0e8c3ed010912ee68735a27b7e77422d6bf Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Mon, 12 Sep 2022 10:27:06 +0200 Subject: [PATCH 03/17] abstract the install process --- pkg/commands/install.go | 19 +++---- pkg/commands/reinstall.go | 2 +- pkg/fetch/fetch.go | 106 ++++++++++++++++++++++++++++++++++++-- pkg/fetch/requester.go | 43 ++++++++++++++++ pkg/utility/dependency.go | 2 +- pkg/utility/git.go | 33 ------------ 6 files changed, 156 insertions(+), 49 deletions(-) create mode 100644 pkg/fetch/requester.go diff --git a/pkg/commands/install.go b/pkg/commands/install.go index 0bcd4e2..8d167ef 100644 --- a/pkg/commands/install.go +++ b/pkg/commands/install.go @@ -3,6 +3,7 @@ package commands import ( "fmt" + "github.com/Glow-Project/ppm/pkg/fetch" "github.com/Glow-Project/ppm/pkg/utility" "github.com/fatih/color" "github.com/urfave/cli/v2" @@ -18,11 +19,11 @@ func install(ctx *cli.Context) error { dependencies := ctx.Args() if dependencies.Len() == 0 { - installAllDependencies(&config, paths) + installAllDependencies(&config, &paths) } for _, dep := range dependencies.Slice() { - if err = installDependency(&config, paths, utility.DependencyFromString(dep), false); err != nil { + if err = installDependency(&config, &paths, utility.DependencyFromString(dep), false); err != nil { return err } } @@ -30,7 +31,7 @@ func install(ctx *cli.Context) error { return nil } -func installAllDependencies(config *utility.PpmConfig, paths utility.Paths) error { +func installAllDependencies(config *utility.PpmConfig, paths *utility.Paths) error { for _, dependency := range config.Dependencies { if err := installDependency(config, paths, dependency, false); err != nil { return err @@ -39,7 +40,7 @@ func installAllDependencies(config *utility.PpmConfig, paths utility.Paths) erro return nil } -func installDependency(config *utility.PpmConfig, paths utility.Paths, dependency *utility.Dependency, isSubDependency bool) error { +func installDependency(config *utility.PpmConfig, paths *utility.Paths, dependency *utility.Dependency, isSubDependency bool) error { if !isSubDependency { fmt.Printf("\rinstalling %s\n", color.YellowString(utility.GetPluginIdentifier(dependency.Identifier))) } else { @@ -47,16 +48,12 @@ func installDependency(config *utility.PpmConfig, paths utility.Paths, dependenc } loadAnim := utility.StartLoading() - err := utility.Clone(paths.Addons, dependency.Identifier, "") + err := fetch.InstallDependency(dependency, paths) loadAnim.Stop() if err != nil { - if err.Error() == "repository already exists" { - alreadyInstalled(dependency.Identifier) - } else { - installError(dependency.Identifier) - return err - } + installError(dependency.Identifier) + return err } shouldAddDep := (!isSubDependency && !config.HasDependency(dependency)) || diff --git a/pkg/commands/reinstall.go b/pkg/commands/reinstall.go index 08e01f0..472eb45 100644 --- a/pkg/commands/reinstall.go +++ b/pkg/commands/reinstall.go @@ -16,7 +16,7 @@ func reinstall(ctx *cli.Context) error { return err } - err = installAllDependencies(&config, paths) + err = installAllDependencies(&config, &paths) if err != nil { return err } diff --git a/pkg/fetch/fetch.go b/pkg/fetch/fetch.go index 8185a1c..7cc44e5 100644 --- a/pkg/fetch/fetch.go +++ b/pkg/fetch/fetch.go @@ -1,16 +1,116 @@ package fetch import ( + "archive/zip" + "fmt" + "io" + "os" + "path" + "path/filepath" + "strings" + "github.com/Glow-Project/ppm/pkg/utility" "github.com/go-git/go-git/v5" ) -func InstallDependency(dep *utility.Dependency, paths *utility.Paths) { +func InstallDependency(dep *utility.Dependency, paths *utility.Paths) error { if dep.Type == utility.GithubAsset { - git.PlainClone(paths.Addons, false, &git.CloneOptions{ + _, err := git.PlainClone(paths.Addons, false, &git.CloneOptions{ URL: dep.Url, }) + if err != nil { + return err + } } else { - // TODO: Install Godot Assets Addon + r := Requester{} + data, err := r.Get(dep.Url) + if err != nil { + return err + } + id := data["result"].([]map[string]string)[0]["asset_id"] + + data, err = r.Get(fmt.Sprintf("https://godotengine.org/asset-library/api/asset/%s", id)) + if err != nil { + return err + } + + var dwdUrl string = data["download_url"].(string) + f, err := os.CreateTemp("", "tempfile") + if err != nil { + return err + } + defer os.Remove(f.Name()) + + r.Download(dwdUrl, f) + f.Close() + err = unzip(f.Name(), path.Join(paths.Addons, dep.Identifier)) + if err != nil { + return err + } + } + + return nil +} + +func unzip(src, dest string) error { + r, err := zip.OpenReader(src) + if err != nil { + return err + } + defer func() { + if err := r.Close(); err != nil { + panic(err) + } + }() + + os.MkdirAll(dest, 0755) + + writeFile := func(f *zip.File) error { + rc, err := f.Open() + if err != nil { + return err + } + defer func() { + if err := rc.Close(); err != nil { + panic(err) + } + }() + + path := filepath.Join(dest, f.Name) + + // Check for ZipSlip (Directory traversal) + if !strings.HasPrefix(path, filepath.Clean(dest)+string(os.PathSeparator)) { + return fmt.Errorf("illegal file path: %s", path) + } + + if f.FileInfo().IsDir() { + os.MkdirAll(path, f.Mode()) + } else { + os.MkdirAll(filepath.Dir(path), f.Mode()) + f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) + if err != nil { + return err + } + defer func() { + if err := f.Close(); err != nil { + panic(err) + } + }() + + _, err = io.Copy(f, rc) + if err != nil { + return err + } + } + return nil + } + + for _, f := range r.File { + err := writeFile(f) + if err != nil { + return err + } } + + return nil } diff --git a/pkg/fetch/requester.go b/pkg/fetch/requester.go new file mode 100644 index 0000000..a916116 --- /dev/null +++ b/pkg/fetch/requester.go @@ -0,0 +1,43 @@ +package fetch + +import ( + "encoding/json" + "fmt" + "io" + "net/http" +) + +type Requester struct { + BaseUrl string +} + +func (r *Requester) Get(url string) (map[string]any, error) { + rawResp, err := http.Get(r.BaseUrl + url) + if err != nil { + return map[string]any{}, err + } + + var result map[string]any + json.NewDecoder(rawResp.Body).Decode(&result) + + return result, err +} + +func (r *Requester) Download(url string, w io.Writer) error { + response, err := http.Get(r.BaseUrl + url) + if err != nil { + return err + } + defer response.Body.Close() + + if response.StatusCode != 200 { + return fmt.Errorf("request to %s failed", url) + } + + _, err = io.Copy(w, response.Body) + if err != nil { + return err + } + + return nil +} diff --git a/pkg/utility/dependency.go b/pkg/utility/dependency.go index 59ae219..2a4910e 100644 --- a/pkg/utility/dependency.go +++ b/pkg/utility/dependency.go @@ -25,7 +25,7 @@ func DependencyFromString(str string) *Dependency { if isGithubUrl { url = str } else { - url = fmt.Sprint("https://github.com/%s", str) + url = fmt.Sprintf("https://github.com/%s", str) } tmp := strings.Split(str, "/") identifier = fmt.Sprintf("%s/%s", tmp[len(tmp)-2], tmp[len(tmp)-1]) diff --git a/pkg/utility/git.go b/pkg/utility/git.go index cdc8353..d95537c 100644 --- a/pkg/utility/git.go +++ b/pkg/utility/git.go @@ -1,42 +1,9 @@ package utility import ( - "errors" - "fmt" - "path/filepath" - "strings" - git "github.com/go-git/go-git/v5" ) -// clone a certain github repository into a certain folder -func Clone(path string, repoName string, version string) error { - // the clonable name of the repository - var repository string - // the name of the repository - var name string - - if IsUrl(repoName) { - repository = repoName - tmp := strings.Split(repository, "/") - name = tmp[len(tmp)-1] - } else if IsUserAndRepo(repoName) { - repository = fmt.Sprintf("https://github.com/%s", repoName) - tmp := strings.Split(repoName, "/") - name = tmp[1] - } else { - // TODO: add godot asset library support - // name = repoName - return errors.New("godot asset library not supported yet") - } - - _, err := git.PlainClone(filepath.Join(path, name), false, &git.CloneOptions{ - URL: repository, - }) - - return err -} - // update a certain repository func Update(path string) error { repo, err := git.PlainOpen(path) From 8aa4c29f4bd023a0435eca24f0d0aaf67dcdc848 Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Mon, 17 Oct 2022 17:46:01 +0200 Subject: [PATCH 04/17] make github plugins work again --- pkg/commands/update.go | 5 ++--- pkg/fetch/fetch.go | 9 +++++---- pkg/utility/dependency.go | 4 ++-- pkg/utility/git.go | 2 +- pkg/utility/ppmconfig.go | 12 ++++++++---- pkg/utility/utility.go | 9 +++++++++ 6 files changed, 27 insertions(+), 14 deletions(-) diff --git a/pkg/commands/update.go b/pkg/commands/update.go index 40fdff8..97a9d0a 100644 --- a/pkg/commands/update.go +++ b/pkg/commands/update.go @@ -28,12 +28,11 @@ func update(ctx *cli.Context) error { func updateAllDependencies(config utility.PpmConfig, paths utility.Paths) error { for _, dependency := range config.Dependencies { - _, version := utility.GetVersionOrNot(dependency.Identifier) - if len(version) > 0 { + if dependency.Type != utility.GithubAsset { continue } - err := utility.Update(filepath.Join(paths.Addons, dependency.Identifier)) + err := utility.UpdateGithubRepo(filepath.Join(paths.Addons, dependency.Identifier)) if err != nil { return err } diff --git a/pkg/fetch/fetch.go b/pkg/fetch/fetch.go index 7cc44e5..08f6f03 100644 --- a/pkg/fetch/fetch.go +++ b/pkg/fetch/fetch.go @@ -15,7 +15,8 @@ import ( func InstallDependency(dep *utility.Dependency, paths *utility.Paths) error { if dep.Type == utility.GithubAsset { - _, err := git.PlainClone(paths.Addons, false, &git.CloneOptions{ + fullPath := path.Join(paths.Addons, dep.Identifier) + _, err := git.PlainClone(fullPath, false, &git.CloneOptions{ URL: dep.Url, }) if err != nil { @@ -27,14 +28,14 @@ func InstallDependency(dep *utility.Dependency, paths *utility.Paths) error { if err != nil { return err } - id := data["result"].([]map[string]string)[0]["asset_id"] + id := data["result"].([]interface{})[0].(map[string]interface{})["asset_id"] data, err = r.Get(fmt.Sprintf("https://godotengine.org/asset-library/api/asset/%s", id)) if err != nil { return err } - var dwdUrl string = data["download_url"].(string) + dwdUrl := data["download_url"].(string) f, err := os.CreateTemp("", "tempfile") if err != nil { return err @@ -43,7 +44,7 @@ func InstallDependency(dep *utility.Dependency, paths *utility.Paths) error { r.Download(dwdUrl, f) f.Close() - err = unzip(f.Name(), path.Join(paths.Addons, dep.Identifier)) + err = unzip(f.Name(), paths.Addons) if err != nil { return err } diff --git a/pkg/utility/dependency.go b/pkg/utility/dependency.go index 2a4910e..b6a188d 100644 --- a/pkg/utility/dependency.go +++ b/pkg/utility/dependency.go @@ -28,12 +28,12 @@ func DependencyFromString(str string) *Dependency { url = fmt.Sprintf("https://github.com/%s", str) } tmp := strings.Split(str, "/") - identifier = fmt.Sprintf("%s/%s", tmp[len(tmp)-2], tmp[len(tmp)-1]) + identifier = tmp[len(tmp)-1] t = GithubAsset } else { identifier = strings.Replace(str, " ", "-", -1) url = fmt.Sprintf("https://godotengine.org/asset-library/api/asset?filter=%s", str) - t = GithubAsset + t = GDAsset } return &Dependency{ diff --git a/pkg/utility/git.go b/pkg/utility/git.go index d95537c..412f024 100644 --- a/pkg/utility/git.go +++ b/pkg/utility/git.go @@ -5,7 +5,7 @@ import ( ) // update a certain repository -func Update(path string) error { +func UpdateGithubRepo(path string) error { repo, err := git.PlainOpen(path) if err != nil { return err diff --git a/pkg/utility/ppmconfig.go b/pkg/utility/ppmconfig.go index e9c9661..3287234 100644 --- a/pkg/utility/ppmconfig.go +++ b/pkg/utility/ppmconfig.go @@ -70,11 +70,15 @@ func (ppm PpmConfig) PrettyPrint() { ppmType = "plugin" } - // TODO: Print dependencies - // dependencies := SliceToString(ppm.Dependencies, ", ") - // subDependencies := SliceToString(ppm.SubDependencies, ", ") + depsToIdentifiers := func(deps []*Dependency) []string { + return Map(deps, func(item *Dependency, _ int) string { + return item.Identifier + }) + } + dependencies := SliceToString(depsToIdentifiers(ppm.Dependencies), ", ") + subDependencies := SliceToString(depsToIdentifiers(ppm.SubDependencies), ", ") - fmt.Printf("this project is a %s\ndependencies: %v\nsubdependencies: %v\n", ppmType) //, dependencies, subDependencies) + fmt.Printf("this project is a %s\ndependencies: %v\nsubdependencies: %v\n", ppmType, dependencies, subDependencies) } // write the current state of the configuartion to the config file diff --git a/pkg/utility/utility.go b/pkg/utility/utility.go index ca4df27..ddafc62 100644 --- a/pkg/utility/utility.go +++ b/pkg/utility/utility.go @@ -151,6 +151,15 @@ func Filter[t any](slice []t, predicate func(t, int) bool) []t { return newSlice } +func Map[t any, n any](slice []t, predicate func(t, int) n) []n { + newSlice := []n{} + for i, item := range slice { + newSlice = append(newSlice, predicate(item, i)) + } + + return newSlice +} + func IsUrl(str string) bool { a, _ := regexp.Match(`https?:\/\/[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{1,5}([a-zA-Z0-9_\/\-\=\&\?\:]+)*`, []byte(str)) return a From 89814bf4c61a5c6015c9584f7cf938f340a38808 Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Tue, 18 Oct 2022 17:55:20 +0200 Subject: [PATCH 05/17] fetch: abstract `InstallDependency` if/else branches into functions --- pkg/fetch/fetch.go | 75 ++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/pkg/fetch/fetch.go b/pkg/fetch/fetch.go index 08f6f03..15d082c 100644 --- a/pkg/fetch/fetch.go +++ b/pkg/fetch/fetch.go @@ -14,45 +14,62 @@ import ( ) func InstallDependency(dep *utility.Dependency, paths *utility.Paths) error { + var err error if dep.Type == utility.GithubAsset { - fullPath := path.Join(paths.Addons, dep.Identifier) - _, err := git.PlainClone(fullPath, false, &git.CloneOptions{ - URL: dep.Url, - }) - if err != nil { - return err - } + err = installGithubRepo(dep, paths) } else { - r := Requester{} - data, err := r.Get(dep.Url) - if err != nil { - return err - } - id := data["result"].([]interface{})[0].(map[string]interface{})["asset_id"] + err = installGodotAsset(dep, paths) - data, err = r.Get(fmt.Sprintf("https://godotengine.org/asset-library/api/asset/%s", id)) - if err != nil { - return err - } + } - dwdUrl := data["download_url"].(string) - f, err := os.CreateTemp("", "tempfile") - if err != nil { - return err - } - defer os.Remove(f.Name()) + return err +} - r.Download(dwdUrl, f) - f.Close() - err = unzip(f.Name(), paths.Addons) - if err != nil { - return err - } +// install a plugin from github +func installGithubRepo(dep *utility.Dependency, paths *utility.Paths) error { + fullPath := path.Join(paths.Addons, dep.Identifier) + _, err := git.PlainClone(fullPath, false, &git.CloneOptions{ + URL: dep.Url, + }) + if err != nil { + return err + } + + return nil +} + +// install a plugin from the godot asset store +func installGodotAsset(dep *utility.Dependency, paths *utility.Paths) error { + r := Requester{} + data, err := r.Get(dep.Url) + if err != nil { + return err + } + id := data["result"].([]interface{})[0].(map[string]interface{})["asset_id"] + + data, err = r.Get(fmt.Sprintf("https://godotengine.org/asset-library/api/asset/%s", id)) + if err != nil { + return err + } + + dwdUrl := data["download_url"].(string) + f, err := os.CreateTemp("", "tempfile") + if err != nil { + return err + } + defer os.Remove(f.Name()) + + r.Download(dwdUrl, f) + f.Close() + err = unzip(f.Name(), paths.Addons) + if err != nil { + return err } return nil } +// unzip a .zip file from src into dest func unzip(src, dest string) error { r, err := zip.OpenReader(src) if err != nil { From 3d4d1ff02c9887f43c5b2706666874f81d5f053c Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Tue, 18 Oct 2022 18:09:34 +0200 Subject: [PATCH 06/17] fetch: enhance documentation --- pkg/fetch/fetch.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/fetch/fetch.go b/pkg/fetch/fetch.go index 15d082c..90b08ce 100644 --- a/pkg/fetch/fetch.go +++ b/pkg/fetch/fetch.go @@ -13,13 +13,13 @@ import ( "github.com/go-git/go-git/v5" ) +// install a dependency `dep` into its directory inside the `addons` directory func InstallDependency(dep *utility.Dependency, paths *utility.Paths) error { var err error if dep.Type == utility.GithubAsset { err = installGithubRepo(dep, paths) } else { err = installGodotAsset(dep, paths) - } return err @@ -45,6 +45,16 @@ func installGodotAsset(dep *utility.Dependency, paths *utility.Paths) error { if err != nil { return err } + + /* structure of data: + { + "result": [ + { + "asset_id": "" + } + ] + } + */ id := data["result"].([]interface{})[0].(map[string]interface{})["asset_id"] data, err = r.Get(fmt.Sprintf("https://godotengine.org/asset-library/api/asset/%s", id)) From e500077fe29c018747e39251d095664efe38522a Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Sat, 5 Nov 2022 10:00:04 +0100 Subject: [PATCH 07/17] move generic utility into own file --- pkg/utility/generics.go | 66 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 pkg/utility/generics.go diff --git a/pkg/utility/generics.go b/pkg/utility/generics.go new file mode 100644 index 0000000..5fb1c67 --- /dev/null +++ b/pkg/utility/generics.go @@ -0,0 +1,66 @@ +package utility + +// get the index of a certain item in a string slice +func IndexOf[T comparable](target T, data []T) int { + for index, value := range data { + if target == value { + return index + } + } + return -1 +} + +// check whether a certain item exists in a string slice +func SliceContains[T comparable](target T, data []T) bool { + for _, v := range data { + if v == target { + return true + } + } + + return false +} + +// get the first predicate match of the slice +// +// returns nil if none of the items match +func GetFirstMatch[T any](arr []T, predicate func(T, int) bool) *T { + for i, value := range arr { + if predicate(value, i) { + return &value + } + } + + return nil +} + +// returns a new slice containing all the items on which the predicate returns true +func Filter[T any](arr []T, predicate func(T, int) bool) []T { + newArr := []T{} + for i, item := range arr { + if predicate(item, i) { + newArr = append(newArr, item) + } + } + + return newArr +} + +func Map[T any, N any](arr []T, predicate func(T, int) N) []N { + newArr := []N{} + for i, item := range arr { + newArr = append(newArr, predicate(item, i)) + } + + return newArr +} + +func Some[T any](arr []T, predicate func(T, int) bool) bool { + for i, item := range arr { + if predicate(item, i) { + return true + } + } + + return false +} From b1a726f0ac951ed6c23561728734267d7c188bbd Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Sat, 5 Nov 2022 10:00:21 +0100 Subject: [PATCH 08/17] move generic utility --- pkg/utility/utility.go | 55 ------------------------------------------ 1 file changed, 55 deletions(-) diff --git a/pkg/utility/utility.go b/pkg/utility/utility.go index ddafc62..fb9da32 100644 --- a/pkg/utility/utility.go +++ b/pkg/utility/utility.go @@ -39,27 +39,6 @@ func CheckOrCreateDir(path string) error { return nil } -// get the index of a certain item in a string slice -func IndexOf[t comparable](target t, data []t) int { - for index, value := range data { - if target == value { - return index - } - } - return -1 -} - -// check whether a certain item exists in a string slice -func SliceContains[t comparable](target t, data []t) bool { - for _, v := range data { - if v == target { - return true - } - } - - return false -} - func GetVersionOrNot(dependency string) (string, string) { version := "" @@ -126,40 +105,6 @@ func SliceToString(slice []string, seperator string) string { return str } -// get the first predicate match of the slice -// -// returns nil if none of the items match -func GetFirstMatch[t any](slice []t, predicate func(t, int) bool) *t { - for i, value := range slice { - if predicate(value, i) { - return &value - } - } - - return nil -} - -// returns a new slice containing all the items on which the predicate returns true -func Filter[t any](slice []t, predicate func(t, int) bool) []t { - newSlice := []t{} - for i, item := range slice { - if predicate(item, i) { - newSlice = append(newSlice, item) - } - } - - return newSlice -} - -func Map[t any, n any](slice []t, predicate func(t, int) n) []n { - newSlice := []n{} - for i, item := range slice { - newSlice = append(newSlice, predicate(item, i)) - } - - return newSlice -} - func IsUrl(str string) bool { a, _ := regexp.Match(`https?:\/\/[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{1,5}([a-zA-Z0-9_\/\-\=\&\?\:]+)*`, []byte(str)) return a From 750a9390968668f3c2e9252448b8b994b02678e0 Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Sat, 5 Nov 2022 10:00:41 +0100 Subject: [PATCH 09/17] config: enhace dependency search --- pkg/utility/ppmconfig.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/utility/ppmconfig.go b/pkg/utility/ppmconfig.go index 3287234..0101dfb 100644 --- a/pkg/utility/ppmconfig.go +++ b/pkg/utility/ppmconfig.go @@ -56,12 +56,16 @@ func (ppm *PpmConfig) RemoveDependency(dependency *Dependency) { // check wether the config file has a certain dependency func (ppm PpmConfig) HasDependency(dependency *Dependency) bool { - return SliceContains(dependency, ppm.Dependencies) + return Some(ppm.Dependencies, func(dep *Dependency, _ int) bool { + return dep.Identifier == dependency.Identifier + }) } // check wether the config file has a certain sub-dependency func (ppm PpmConfig) HasSubDependency(dependency *Dependency) bool { - return SliceContains(dependency, ppm.SubDependencies) + return Some(ppm.SubDependencies, func(dep *Dependency, _ int) bool { + return dep.Identifier == dependency.Identifier + }) } func (ppm PpmConfig) PrettyPrint() { From eb51e506983559e559f75300d79ea80d1c4f4b1d Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Sun, 27 Nov 2022 10:27:50 +0100 Subject: [PATCH 10/17] Taskfile: fix --- taskfile.yml => Taskfile.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename taskfile.yml => Taskfile.yml (100%) diff --git a/taskfile.yml b/Taskfile.yml similarity index 100% rename from taskfile.yml rename to Taskfile.yml From bede140afb3f02c97f7a02eb9d1278d28fe06708 Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Sun, 27 Nov 2022 10:28:07 +0100 Subject: [PATCH 11/17] fetch: cast `interface{}` id to string --- pkg/fetch/fetch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/fetch/fetch.go b/pkg/fetch/fetch.go index 90b08ce..a6fd0b3 100644 --- a/pkg/fetch/fetch.go +++ b/pkg/fetch/fetch.go @@ -55,7 +55,7 @@ func installGodotAsset(dep *utility.Dependency, paths *utility.Paths) error { ] } */ - id := data["result"].([]interface{})[0].(map[string]interface{})["asset_id"] + id := data["result"].([]interface{})[0].(map[string]interface{})["asset_id"].(string) data, err = r.Get(fmt.Sprintf("https://godotengine.org/asset-library/api/asset/%s", id)) if err != nil { From fc8560dde4ee6488a805fc6be5a60616abb5d92d Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Sun, 27 Nov 2022 10:43:33 +0100 Subject: [PATCH 12/17] utility: abstract regex into own file --- pkg/utility/regex.go | 21 +++++++++++++++++++++ pkg/utility/utility.go | 16 ---------------- 2 files changed, 21 insertions(+), 16 deletions(-) create mode 100644 pkg/utility/regex.go diff --git a/pkg/utility/regex.go b/pkg/utility/regex.go new file mode 100644 index 0000000..4a59713 --- /dev/null +++ b/pkg/utility/regex.go @@ -0,0 +1,21 @@ +package utility + +import "regexp" + +var ( + urlRegex = regexp.MustCompile(`https?:\/\/[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{1,5}([a-zA-Z0-9_\/\-\=\&\?\:]+)*`) + githubUrlRegex = regexp.MustCompile(`https?:\/\/github\.com(\/[a-zA-Z0-9_\-\=\&\?\:]+){2}`) + userAndRepoRegex = regexp.MustCompile(`[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+`) +) + +func IsUrl(str string) bool { + return urlRegex.MatchString(str) +} + +func IsGithubRepoUrl(str string) bool { + return githubUrlRegex.MatchString(str) +} + +func IsUserAndRepo(str string) bool { + return userAndRepoRegex.MatchString(str) +} diff --git a/pkg/utility/utility.go b/pkg/utility/utility.go index fb9da32..3857c80 100644 --- a/pkg/utility/utility.go +++ b/pkg/utility/utility.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "os" - "regexp" "strings" "github.com/fatih/color" @@ -104,18 +103,3 @@ func SliceToString(slice []string, seperator string) string { return str } - -func IsUrl(str string) bool { - a, _ := regexp.Match(`https?:\/\/[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{1,5}([a-zA-Z0-9_\/\-\=\&\?\:]+)*`, []byte(str)) - return a -} - -func IsGithubRepoUrl(str string) bool { - a, _ := regexp.Match(`https?:\/\/github\.com(\/[a-zA-Z0-9_\-\=\&\?\:]+){2}`, []byte(str)) - return a -} - -func IsUserAndRepo(str string) bool { - a, _ := regexp.Match(`[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+`, []byte(str)) - return a -} From 9d14ced0570b297fbd10df1604d0b3ac013f0e86 Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Sun, 27 Nov 2022 10:43:51 +0100 Subject: [PATCH 13/17] fetch: remove sha when unzipping --- pkg/fetch/fetch.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/fetch/fetch.go b/pkg/fetch/fetch.go index a6fd0b3..60a305b 100644 --- a/pkg/fetch/fetch.go +++ b/pkg/fetch/fetch.go @@ -7,6 +7,7 @@ import ( "os" "path" "path/filepath" + "regexp" "strings" "github.com/Glow-Project/ppm/pkg/utility" @@ -55,7 +56,12 @@ func installGodotAsset(dep *utility.Dependency, paths *utility.Paths) error { ] } */ - id := data["result"].([]interface{})[0].(map[string]interface{})["asset_id"].(string) + results := data["result"].([]interface{}) + if len(results) == 0 { + return fmt.Errorf("No results for dependency \"%s\"", dep.Identifier) + } + + id := results[0].(map[string]interface{})["asset_id"].(string) data, err = r.Get(fmt.Sprintf("https://godotengine.org/asset-library/api/asset/%s", id)) if err != nil { @@ -93,6 +99,8 @@ func unzip(src, dest string) error { os.MkdirAll(dest, 0755) + shaRegex := regexp.MustCompile("-[0-9a-f]{40}") + writeFile := func(f *zip.File) error { rc, err := f.Open() if err != nil { @@ -104,7 +112,9 @@ func unzip(src, dest string) error { } }() - path := filepath.Join(dest, f.Name) + fileName := string(shaRegex.ReplaceAll([]byte(f.Name), []byte(""))) + path := filepath.Join(dest, fileName) + fmt.Println(path) // Check for ZipSlip (Directory traversal) if !strings.HasPrefix(path, filepath.Clean(dest)+string(os.PathSeparator)) { From 1a47a9110213d1c67a051297df8bc2501113c224 Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Sun, 27 Nov 2022 11:40:34 +0100 Subject: [PATCH 14/17] regex: add `IsAssetDependency` check --- pkg/utility/regex.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/utility/regex.go b/pkg/utility/regex.go index 4a59713..e9ecee6 100644 --- a/pkg/utility/regex.go +++ b/pkg/utility/regex.go @@ -3,9 +3,10 @@ package utility import "regexp" var ( - urlRegex = regexp.MustCompile(`https?:\/\/[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{1,5}([a-zA-Z0-9_\/\-\=\&\?\:]+)*`) - githubUrlRegex = regexp.MustCompile(`https?:\/\/github\.com(\/[a-zA-Z0-9_\-\=\&\?\:]+){2}`) - userAndRepoRegex = regexp.MustCompile(`[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+`) + urlRegex = regexp.MustCompile(`^https?:\/\/[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{1,5}([a-zA-Z0-9_\/\-\=\&\?\:]+)*$`) + githubUrlRegex = regexp.MustCompile(`^https?:\/\/github\.com(\/[a-zA-Z0-9_\-\=\&\?\:]+){2}$`) + userAndRepoRegex = regexp.MustCompile(`^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+$`) + assetDependencyRegex = regexp.MustCompile(`^[a-zA-Z0-9\-\_]+$`) ) func IsUrl(str string) bool { @@ -19,3 +20,7 @@ func IsGithubRepoUrl(str string) bool { func IsUserAndRepo(str string) bool { return userAndRepoRegex.MatchString(str) } + +func IsAssetDependency(str string) bool { + return assetDependencyRegex.MatchString(str) +} From 9e89fc7bf8b9d681f759f63a92cde9c1979b5517 Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Sun, 27 Nov 2022 11:41:01 +0100 Subject: [PATCH 15/17] tidy: enhance for backwards compability --- pkg/commands/tidy.go | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/pkg/commands/tidy.go b/pkg/commands/tidy.go index 88a9d8f..9d0f210 100644 --- a/pkg/commands/tidy.go +++ b/pkg/commands/tidy.go @@ -1,12 +1,49 @@ package commands import ( + "encoding/json" + "os" + "github.com/Glow-Project/ppm/pkg/utility" "github.com/urfave/cli/v2" ) func tidy(ctx *cli.Context) error { - _, config, err := utility.GetPathsAndConfig() + paths, err := utility.CreatePathsFromCwd() + if err != nil { + return err + } + + content, err := os.ReadFile(paths.ConfigFile) + if err != nil { + return err + } + + var jsonContent map[string]interface{} + err = json.Unmarshal(content, &jsonContent) + if err != nil { + return err + } + + strDeps, ok := jsonContent["dependencies"].([]interface{}) + if !ok { + return nil + } + + deps := []*utility.Dependency{} + for _, dep := range strDeps { + str, ok := dep.(string) + if !ok { + return nil + } + deps = append(deps, utility.DependencyFromString(str)) + } + + jsonContent["dependencies"] = deps + jsonData, err := json.MarshalIndent(jsonContent, "", "\t") + os.WriteFile(paths.ConfigFile, jsonData, 0644) + + config, err := utility.ParsePpmConfig(paths.ConfigFile) if err != nil { return err } From f1fb18575f3ff9315e1bcfd9f3235988062b1e03 Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Sun, 27 Nov 2022 11:41:19 +0100 Subject: [PATCH 16/17] remove debug code --- pkg/fetch/fetch.go | 1 - tests/utility_test.go | 2 -- 2 files changed, 3 deletions(-) diff --git a/pkg/fetch/fetch.go b/pkg/fetch/fetch.go index 60a305b..54ff4ba 100644 --- a/pkg/fetch/fetch.go +++ b/pkg/fetch/fetch.go @@ -114,7 +114,6 @@ func unzip(src, dest string) error { fileName := string(shaRegex.ReplaceAll([]byte(f.Name), []byte(""))) path := filepath.Join(dest, fileName) - fmt.Println(path) // Check for ZipSlip (Directory traversal) if !strings.HasPrefix(path, filepath.Clean(dest)+string(os.PathSeparator)) { diff --git a/tests/utility_test.go b/tests/utility_test.go index 99740d9..c28f1fd 100644 --- a/tests/utility_test.go +++ b/tests/utility_test.go @@ -1,7 +1,6 @@ package tests import ( - "fmt" "path/filepath" "testing" @@ -17,7 +16,6 @@ func TestPaths(t *testing.T) { path := "/this/is/a/test" addonsPath := filepath.Join(path + "/addons") paths := utility.CreatePaths(path) - fmt.Println(path) if paths.Root != path { t.Errorf(pathNotEqualMessage, paths.Root, path) } From 162e013880055225daab68e1e391b4b5765ab25e Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Sun, 27 Nov 2022 11:41:36 +0100 Subject: [PATCH 17/17] ppmconfig: enhance error checking --- pkg/utility/ppmconfig.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/utility/ppmconfig.go b/pkg/utility/ppmconfig.go index 0101dfb..5beafb3 100644 --- a/pkg/utility/ppmconfig.go +++ b/pkg/utility/ppmconfig.go @@ -114,7 +114,10 @@ func ParsePpmConfig(filePath string) (PpmConfig, error) { } config := PpmConfig{} - json.Unmarshal([]byte(content), &config) + err = json.Unmarshal([]byte(content), &config) + if err != nil { + return PpmConfig{}, err + } config.filePath = filePath return config, nil @@ -144,7 +147,7 @@ func CreateNewPpmConfig(path string) error { SubDependencies: []*Dependency{}, } - content, err := json.MarshalIndent(config, "", " ") + content, err := json.MarshalIndent(config, "", "\t") if err != nil { return err }