Skip to content

Commit

Permalink
Fix double url preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
vyPal committed Feb 11, 2024
1 parent 80e4db3 commit 8116eb2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 46 deletions.
43 changes: 3 additions & 40 deletions src/lib/cache/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import (
"encoding/gob"
"fmt"
"log"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"

"github.com/fatih/color"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/vyPal/CaffeineC/lib/project"
Expand Down Expand Up @@ -229,37 +227,7 @@ func (p *PackageCache) ResolvePackage(ident string) (found bool, pkg Package, fp
return found, pkg, fp, objDir, nil
}

func PrepUrl(liburl string) (u, ver string, e error) {
version := "main"
if strings.Contains(liburl, "@") {
split := strings.Split(liburl, "@")
liburl = split[0]
version = split[1]
} else {
color.Yellow("Branch name not specified, defaulting to 'main'")
}

parsedUrl, err := url.Parse(liburl)
if err != nil {
return "", "", err
}

if parsedUrl.Hostname() == "" {
liburl = "https://github.com/" + liburl
}

if !strings.HasPrefix(liburl, "http://") && !strings.HasPrefix(liburl, "https://") {
liburl = "https://" + liburl
}
return liburl, version, nil
}

func UpdateLibrary(pcache PackageCache, liburl string) (conf project.CfConf, ident, ver string, e error) {
liburl, version, err := PrepUrl(liburl)
if err != nil {
return project.CfConf{}, "", "", err
}

func UpdateLibrary(pcache PackageCache, liburl string, version string) (conf project.CfConf, ident, ver string, e error) {
// Get the directory in the cache's BaseDir
updateDir := filepath.Join(pcache.BaseDir, strings.TrimPrefix(liburl, "https://"), version)

Expand Down Expand Up @@ -311,15 +279,10 @@ func UpdateLibrary(pcache PackageCache, liburl string) (conf project.CfConf, ide
return conf, strings.TrimPrefix(liburl, "https://"), version, nil
}

func InstallLibrary(pcache PackageCache, liburl string) (conf project.CfConf, ident, ver string, e error) {
liburl, version, err := PrepUrl(liburl)
if err != nil {
return project.CfConf{}, "", "", err
}

func InstallLibrary(pcache PackageCache, liburl string, version string) (conf project.CfConf, ident, ver string, e error) {
// Create a directory in the cache's BaseDir
installDir := filepath.Join(pcache.BaseDir, strings.TrimPrefix(liburl, "https://"), version)
err = os.MkdirAll(installDir, 0700)
err := os.MkdirAll(installDir, 0700)
if err != nil {
return project.CfConf{}, "", "", err
}
Expand Down
12 changes: 6 additions & 6 deletions src/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func install(c *cli.Context) error {

if pkg == (cache.Package{}) {
color.Green("Package not found locally, cloning...")
conf, _, _, err = cache.InstallLibrary(pcache, dep.Identifier)
conf, _, _, err = cache.InstallLibrary(pcache, dep.Identifier, dep.Version)
if err != nil {
return err
}
Expand Down Expand Up @@ -140,7 +140,7 @@ func install(c *cli.Context) error {
var cnf project.CfConf
if pkg == (cache.Package{}) {
color.Green("Package not found locally, cloning...")
cnf, ident, ver, err = cache.InstallLibrary(pcache, liburl)
cnf, ident, ver, err = cache.InstallLibrary(pcache, liburl, version)
if err != nil {
return err
}
Expand Down Expand Up @@ -307,13 +307,13 @@ func libUpdate(c *cli.Context) error {

if pkg == (cache.Package{}) {
color.Green("Package not found locally, cloning...")
conf, _, _, err = cache.InstallLibrary(pcache, dep.Identifier)
conf, _, _, err = cache.InstallLibrary(pcache, dep.Identifier, dep.Version)
if err != nil {
return err
}
} else {
color.Green("Package found locally, updating...")
conf, _, _, err = cache.UpdateLibrary(pcache, dep.Identifier)
conf, _, _, err = cache.UpdateLibrary(pcache, dep.Identifier, dep.Version)
if err != nil {
return err
}
Expand All @@ -339,13 +339,13 @@ func libUpdate(c *cli.Context) error {
var cnf project.CfConf
if pkg == (cache.Package{}) {
color.Green("Package not found locally, cloning...")
cnf, ident, ver, err = cache.InstallLibrary(pcache, liburl)
cnf, ident, ver, err = cache.InstallLibrary(pcache, liburl, version)
if err != nil {
return err
}
} else {
color.Green("Package found locally, updating...")
cnf, ident, ver, err = cache.UpdateLibrary(pcache, liburl)
cnf, ident, ver, err = cache.UpdateLibrary(pcache, liburl, version)
if err != nil {
return err
}
Expand Down

0 comments on commit 8116eb2

Please sign in to comment.