Skip to content

Commit

Permalink
Merge pull request #26 from launchrctl/24-various_improvements
Browse files Browse the repository at this point in the history
#24: add git clone depth 1
  • Loading branch information
davidferlay authored Jan 16, 2024
2 parents 8f36791 + e7f07c9 commit 4cf4f9d
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 15 deletions.
3 changes: 3 additions & 0 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (p *Plugin) CobraAddCommands(rootCmd *cobra.Command) error {
var workingDir string
var skipNotVersioned bool
var conflictsVerbosity bool
var clean bool
var composeCmd = &cobra.Command{
Use: "compose",
Short: "Composes filesystem (files & dirs)",
Expand All @@ -45,6 +46,7 @@ func (p *Plugin) CobraAddCommands(rootCmd *cobra.Command) error {
c, err := compose.CreateComposer(
p.wd,
compose.ComposerOptions{
Clean: clean,
WorkingDir: workingDir,
SkipNotVersioned: skipNotVersioned,
ConflictsVerbosity: conflictsVerbosity,
Expand All @@ -62,6 +64,7 @@ func (p *Plugin) CobraAddCommands(rootCmd *cobra.Command) error {
composeCmd.Flags().StringVarP(&workingDir, "working-dir", "w", ".compose/packages", "Working directory for temp files")
composeCmd.Flags().BoolVarP(&skipNotVersioned, "skip-not-versioned", "s", false, "Skip not versioned files from source directory (git only)")
composeCmd.Flags().BoolVar(&conflictsVerbosity, "conflicts-verbosity", false, "Log files conflicts")
composeCmd.Flags().BoolVar(&clean, "clean", false, "Remove .compose dir on start")
rootCmd.AddCommand(composeCmd)
return nil
}
13 changes: 12 additions & 1 deletion compose/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func getVersionedMap(gitDir string) (map[string]bool, error) {
}

func (b *Builder) build() error {
fmt.Println("Creating composition...")
err := EnsureDirExists(b.targetDir)
if err != nil {
return err
Expand Down Expand Up @@ -202,6 +203,7 @@ func (b *Builder) build() error {

graph := buildDependenciesGraph(b.packages)
items, _ := graph.TopSort(DependencyRoot)
targetsMap := getTargetsMap(b.packages)

if b.logConflicts {
fmt.Print("Conflicting files:\n")
Expand All @@ -210,7 +212,7 @@ func (b *Builder) build() error {
for i := 0; i < len(items); i++ {
pkgName := items[i]
if pkgName != DependencyRoot {
pkgPath := filepath.Join(b.sourceDir, pkgName)
pkgPath := filepath.Join(b.sourceDir, pkgName, targetsMap[pkgName])
packageFs := os.DirFS(pkgPath)
strategies, ok := ps[pkgName]
err = fs.WalkDir(packageFs, ".", func(path string, d fs.DirEntry, err error) error {
Expand Down Expand Up @@ -281,6 +283,15 @@ func (b *Builder) build() error {
return nil
}

func getTargetsMap(packages []*Package) map[string]string {
targets := make(map[string]string)
for _, p := range packages {
targets[p.GetName()] = p.GetTarget()
}

return targets
}

func logConflictResolve(resolveto mergeConflictResolve, path, pkgName string, entry *fsEntry) {
if resolveto == noConflict {
return
Expand Down
50 changes: 40 additions & 10 deletions compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package compose

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
Expand All @@ -11,9 +12,10 @@ import (
)

const (
composeFile = "compose.yaml"
buildDir = ".compose/build"
dirPermissions = 0755
buildDir = ".compose/build"
composeDir = ".compose"
composeFile = "compose.yaml"
dirPermissions = 0755
)

var (
Expand All @@ -31,6 +33,7 @@ type Composer struct {

// ComposerOptions - list of possible composer options
type ComposerOptions struct {
Clean bool
WorkingDir string
SkipNotVersioned bool
ConflictsVerbosity bool
Expand All @@ -48,9 +51,12 @@ func CreateComposer(pwd string, opts ComposerOptions, k keyring.Keyring) (*Compo

// RunInstall on composr
func (c *Composer) RunInstall() error {
dm := CreateDownloadManager()
buildDir, packagesDir, err := c.prepareInstall()
if err != nil {
return err
}

packagesDir := c.getPackagesDirPath()
dm := CreateDownloadManager()
packages, err := dm.Download(c.getCompose(), packagesDir, c.getKeyring())
if err != nil {
return err
Expand All @@ -61,20 +67,44 @@ func (c *Composer) RunInstall() error {

builder := createBuilder(
c.pwd,
c.getBuildDirPath(),
buildDir,
packagesDir,
c.options.SkipNotVersioned,
c.options.ConflictsVerbosity,
packages)
return builder.build()
}

func (c *Composer) getBuildDirPath() string {
return filepath.Join(c.pwd, buildDir)
func (c *Composer) prepareInstall() (string, string, error) {
buildPath := c.getPath(buildDir)
composePath := c.getPath(composeDir)
packagesPath := c.getPath(c.options.WorkingDir)

if c.options.Clean {
fmt.Printf("Cleaning compose dir: %s\n", composeDir)
err := os.RemoveAll(composePath)
if err != nil {
return "", "", err
}

fmt.Printf("Cleaning packages dir: %s\n", c.options.WorkingDir)
err = os.RemoveAll(packagesPath)
if err != nil {
return "", "", err
}
} else {
fmt.Printf("Cleaning build dir: %s\n", buildDir)
err := os.RemoveAll(buildPath)
if err != nil {
return "", "", err
}
}

return buildPath, packagesPath, nil
}

func (c *Composer) getPackagesDirPath() string {
return filepath.Join(c.pwd, c.options.WorkingDir)
func (c *Composer) getPath(value string) string {
return filepath.Join(c.pwd, value)
}

// EnsureDirExists checks if directory exists, otherwise create it
Expand Down
12 changes: 9 additions & 3 deletions compose/downloadManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,17 @@ func (m DownloadManager) recursiveDownload(c *YamlCompose, packages []*Package,

func downloadPackage(pkg *Package, targetDir string, k keyring.Keyring) error {
downloader := getDownloaderForPackage(pkg.GetType())
var packagePath = filepath.Join(targetDir, pkg.GetName())
var downloadPath = packagePath
packagePath := filepath.Join(targetDir, pkg.GetName())
downloadPath := filepath.Join(packagePath, pkg.GetTarget())

if _, err := os.Stat(downloadPath); !os.IsNotExist(err) {
// Skip package download if folder exists in packages dir.
return nil
}

// temporary
if dtype := pkg.GetType(); dtype == httpType {
downloadPath = targetDir
downloadPath = packagePath
}

err := downloader.Download(pkg, downloadPath, k)
Expand Down
1 change: 1 addition & 0 deletions compose/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (g *gitDownloader) Download(pkg *Package, targetDir string, k keyring.Keyri
options := &git.CloneOptions{
URL: url,
Progress: os.Stdout,
Depth: 1,
}
if pkg.GetRef() != "" {
options.ReferenceName = plumbing.NewBranchReferenceName(pkg.GetRef())
Expand Down
3 changes: 2 additions & 1 deletion compose/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (h *httpDownloader) Download(pkg *Package, targetDir string, k keyring.Keyr

fmt.Println(fmt.Sprintf("http download: " + name))
fpath := filepath.Clean(filepath.Join(targetDir, name))
os.MkdirAll(targetDir, dirPermissions)

out, err := os.Create(fpath)
if err != nil {
Expand Down Expand Up @@ -102,7 +103,7 @@ func (h *httpDownloader) Download(pkg *Package, targetDir string, k keyring.Keyr
// rename root folder to package name
return os.Rename(
filepath.Join(targetDir, archiveRootDir),
filepath.Join(targetDir, pkg.Name),
filepath.Join(targetDir, pkg.GetTarget()),
)
}

Expand Down
12 changes: 12 additions & 0 deletions compose/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ func (p *Package) GetTag() string {
return p.Source.Tag
}

func (p *Package) GetTarget() string {
target := "latest"

if p.GetRef() != "" {
target = p.GetRef()
} else if p.GetTag() != "" {
target = p.GetTag()
}

return target
}

func parseComposeYaml(input []byte) (*YamlCompose, error) {
cfg := YamlCompose{}
err := yaml.Unmarshal(input, &cfg)
Expand Down

0 comments on commit 4cf4f9d

Please sign in to comment.