Skip to content

Commit

Permalink
Add file templating #318
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasdille committed Jul 11, 2024
1 parent 3e1badc commit 7ba2015
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 9 deletions.
57 changes: 55 additions & 2 deletions cmd/uniget/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"fmt"
"html/template"
"os"
"strings"
"syscall"

"github.com/pterm/pterm"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -340,10 +342,10 @@ func installTools(requestedTools tool.Tools, check bool, plan bool, reinstall bo
assertDirectory(viper.GetString("prefix") + "/" + viper.GetString("target"))
var err error
if usePathRewrite {
err = plannedTool.InstallWithPathRewrites(registryImagePrefix, viper.GetString("prefix"), pathRewriteRules)
err = plannedTool.InstallWithPathRewrites(registryImagePrefix, viper.GetString("prefix"), pathRewriteRules, createPatchFileCallback(plannedTool))

} else {
err = plannedTool.Install(registryImagePrefix, viper.GetString("prefix"), viper.GetString("target"), libDirectory, cacheDirectory)
err = plannedTool.Install(registryImagePrefix, viper.GetString("prefix"), viper.GetString("target"), libDirectory, cacheDirectory, createPatchFileCallback(plannedTool))
}
if err != nil {
if installSpinner != nil {
Expand Down Expand Up @@ -396,3 +398,54 @@ func installTools(requestedTools tool.Tools, check bool, plan bool, reinstall bo

return nil
}

func createPatchFileCallback(tool tool.Tool) func(path string) {
var patchFile = func(templatePath string) {
if strings.HasSuffix(templatePath, ".go-template") {
} else {
logging.Debugf("Skipping file %s. Will not patch.", templatePath)
return
}

values := make(map[string]interface{})
values["Target"] = viper.GetString("target")
values["Prefix"] = viper.GetString("prefix")
values["Name"] = tool.Name
values["Version"] = tool.Version

filePath := strings.TrimSuffix(templatePath, ".go-template")
logging.Info.Printfln("Patching file %s <- %s", filePath, templatePath)

templathPathInfo, err := os.Stat(templatePath)
if err != nil {
logging.Error.Printfln("Unable to get file info: %s", err)
return
}

file, err := os.Create(filePath)
if err != nil {
logging.Error.Printfln("Unable to create file: %s", err)
return
}
defer file.Close()
if stat, ok := templathPathInfo.Sys().(*syscall.Stat_t); ok {
file.Chown(int(stat.Uid), int(stat.Gid))
}
file.Chmod(templathPathInfo.Mode())

tmpl, err := template.ParseFiles(templatePath)
if err != nil {
logging.Error.Printfln("Unable to parse template file: %s", err)
return
}
tmpl.Execute(file, values)

err = os.Remove(templatePath)
if err != nil {
logging.Error.Printfln("Unable to remove template file: %s", err)
return
}
}

return patchFile
}
2 changes: 1 addition & 1 deletion cmd/uniget/self-upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var selfUpgradeCmd = &cobra.Command{
if err != nil {
return fmt.Errorf("failed to remove %s: %s", selfExe, err)
}
err = archive.ExtractTarGz(resp.Body, func(path string) string { return path })
err = archive.ExtractTarGz(resp.Body, func(path string) string { return path }, func(path string) {})
if err != nil {
return fmt.Errorf("failed to extract tar.gz: %s", err)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/uniget/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func uninstallTool(toolName string) error {
return fmt.Errorf("unable to read file %s: %s", filename, err)
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSuffix(line, ".go-template")
logging.Debugf("processing %s", line)
strippedLine := strings.TrimPrefix(line, "./")
strippedLine = strings.TrimPrefix(strippedLine, "usr/local/")
Expand Down
2 changes: 1 addition & 1 deletion cmd/uniget/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func downloadMetadata() error {
}

logging.Debugf("Extracting archive to %s", viper.GetString("prefix")+"/"+cacheDirectory)
err = archive.ExtractTarGz(blob, func(path string) string { return path })
err = archive.ExtractTarGz(blob, func(path string) string { return path }, func(path string) {})
if err != nil {
return fmt.Errorf("error extracting archive: %s", err)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/archive/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func pathIsInsideTarget(target string, candidate string) error {
return nil
}

func ExtractTarGz(gzipStream io.Reader, patchPath func(path string) string) error {
func ExtractTarGz(gzipStream io.Reader, patchPath func(path string) string, patchFile func(path string)) error {
target := "."

uncompressedStream, err := gzip.NewReader(gzipStream)
Expand Down Expand Up @@ -123,6 +123,9 @@ func ExtractTarGz(gzipStream io.Reader, patchPath func(path string) string) erro
return fmt.Errorf("ExtractTarGz: Chmod() failed: %s", err.Error())
}

// Callback for patching file
patchFile(fixedHeaderName)

// Unpack symlink
case tar.TypeSymlink:
logging.Tracef("Untarring symlink %s", fixedHeaderName)
Expand Down
8 changes: 4 additions & 4 deletions pkg/tool/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func applyPathRewrites(path string, rules []PathRewrite) string {
return newPath
}

func (tool *Tool) InstallWithPathRewrites(registryImagePrefix string, prefix string, rules []PathRewrite) error {
func (tool *Tool) InstallWithPathRewrites(registryImagePrefix string, prefix string, rules []PathRewrite, patchFile func(path string)) error {
// Fetch manifest for tool
err := containers.GetManifest(fmt.Sprintf(registryImagePrefix+"%s:%s", tool.Name, strings.Replace(tool.Version, "+", "-", -1)), func(blob blob.Reader) error {
logging.Debugf("Extracting with prefix=%s", prefix)
Expand All @@ -75,7 +75,7 @@ func (tool *Tool) InstallWithPathRewrites(registryImagePrefix string, prefix str
// Unpack tool
err = archive.ExtractTarGz(blob, func(path string) string {
return applyPathRewrites(path, rules)
})
}, patchFile)
if err != nil {
return fmt.Errorf("failed to extract layer: %s", err)
}
Expand All @@ -89,7 +89,7 @@ func (tool *Tool) InstallWithPathRewrites(registryImagePrefix string, prefix str
return nil
}

func (tool *Tool) Install(registryImagePrefix string, prefix string, target string, libDirectory string, cacheDirectory string) error {
func (tool *Tool) Install(registryImagePrefix string, prefix string, target string, libDirectory string, cacheDirectory string, patchFile func(path string)) error {
// Fetch manifest for tool
err := containers.GetManifest(fmt.Sprintf(registryImagePrefix+"%s:%s", tool.Name, strings.Replace(tool.Version, "+", "-", -1)), func(blob blob.Reader) error {
logging.Debugf("Extracting with prefix=%s and target=%s", prefix, target)
Expand Down Expand Up @@ -139,7 +139,7 @@ func (tool *Tool) Install(registryImagePrefix string, prefix string, target stri
}

return fixedPath
})
}, patchFile)
if err != nil {
return fmt.Errorf("failed to extract layer: %s", err)
}
Expand Down

0 comments on commit 7ba2015

Please sign in to comment.