From d4432242d72cc085dc7f7b4b09ddc1f9f275bf3a Mon Sep 17 00:00:00 2001 From: Igor Ignatyev Date: Mon, 15 Jan 2024 17:45:28 +0300 Subject: [PATCH 1/5] #24: add git clone depth 1 --- compose/compose.go | 6 +++--- compose/git.go | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/compose/compose.go b/compose/compose.go index 90a6378..aefe91b 100644 --- a/compose/compose.go +++ b/compose/compose.go @@ -11,9 +11,9 @@ import ( ) const ( - composeFile = "compose.yaml" - buildDir = ".compose/build" - dirPermissions = 0755 + composeFile = "compose.yaml" + buildDir = ".compose/build" + dirPermissions = 0755 ) var ( diff --git a/compose/git.go b/compose/git.go index 1e59432..670286d 100644 --- a/compose/git.go +++ b/compose/git.go @@ -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()) From 46a5bda5a64b8e6b15e07433ffbc963817190c0e Mon Sep 17 00:00:00 2001 From: Igor Ignatyev Date: Mon, 15 Jan 2024 18:18:34 +0300 Subject: [PATCH 2/5] #24: add clean option --- compose.go | 3 +++ compose/compose.go | 39 +++++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/compose.go b/compose.go index 3890838..045949f 100644 --- a/compose.go +++ b/compose.go @@ -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)", @@ -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, @@ -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 } diff --git a/compose/compose.go b/compose/compose.go index aefe91b..19ba010 100644 --- a/compose/compose.go +++ b/compose/compose.go @@ -3,6 +3,7 @@ package compose import ( "errors" + "fmt" "io/fs" "os" "path/filepath" @@ -11,8 +12,9 @@ import ( ) const ( - composeFile = "compose.yaml" buildDir = ".compose/build" + composeDir = ".compose" + composeFile = "compose.yaml" dirPermissions = 0755 ) @@ -31,6 +33,7 @@ type Composer struct { // ComposerOptions - list of possible composer options type ComposerOptions struct { + Clean bool WorkingDir string SkipNotVersioned bool ConflictsVerbosity bool @@ -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 @@ -61,7 +67,7 @@ func (c *Composer) RunInstall() error { builder := createBuilder( c.pwd, - c.getBuildDirPath(), + buildDir, packagesDir, c.options.SkipNotVersioned, c.options.ConflictsVerbosity, @@ -69,12 +75,29 @@ func (c *Composer) RunInstall() error { 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) + packagesPath := c.getPath(c.options.WorkingDir) + + if c.options.Clean { + fmt.Printf("Cleaning compose dir: %s\n", composeDir) + err := os.RemoveAll(composeDir) + if err != nil { + return "", "", err + } + + fmt.Printf("Cleaning packages dir: %s\n", c.options.WorkingDir) + err = os.RemoveAll(packagesPath) + 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 From cbd3f85c5621dc1b63a5f9acc9c3edb3f6b0b3c5 Mon Sep 17 00:00:00 2001 From: Igor Ignatyev Date: Mon, 15 Jan 2024 18:25:36 +0300 Subject: [PATCH 3/5] #24: remove build dir before compose --- compose/compose.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/compose/compose.go b/compose/compose.go index 19ba010..07d4036 100644 --- a/compose/compose.go +++ b/compose/compose.go @@ -77,11 +77,12 @@ func (c *Composer) RunInstall() error { 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(composeDir) + err := os.RemoveAll(composePath) if err != nil { return "", "", err } @@ -91,6 +92,12 @@ func (c *Composer) prepareInstall() (string, string, error) { 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 From 31e9199dc03ff40bc87ba670dc2913a723e65626 Mon Sep 17 00:00:00 2001 From: Igor Ignatyev Date: Tue, 16 Jan 2024 14:38:57 +0300 Subject: [PATCH 4/5] #24: store packages inside version folder. skip download if package already exists --- compose/builder.go | 12 +++++++++++- compose/downloadManager.go | 12 +++++++++--- compose/http.go | 3 ++- compose/yaml.go | 12 ++++++++++++ 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/compose/builder.go b/compose/builder.go index ca55e35..f29652a 100644 --- a/compose/builder.go +++ b/compose/builder.go @@ -202,6 +202,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") @@ -210,7 +211,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 { @@ -281,6 +282,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 diff --git a/compose/downloadManager.go b/compose/downloadManager.go index f7c4c3f..61542c2 100644 --- a/compose/downloadManager.go +++ b/compose/downloadManager.go @@ -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) diff --git a/compose/http.go b/compose/http.go index d3286c9..65a8301 100644 --- a/compose/http.go +++ b/compose/http.go @@ -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 { @@ -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()), ) } diff --git a/compose/yaml.go b/compose/yaml.go index bee03fe..8f8def8 100644 --- a/compose/yaml.go +++ b/compose/yaml.go @@ -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) From e7f07c9e4f710302509c6f2fb82236de18ac74d7 Mon Sep 17 00:00:00 2001 From: Igor Ignatyev Date: Tue, 16 Jan 2024 15:31:54 +0300 Subject: [PATCH 5/5] #24: print build message --- compose/builder.go | 1 + 1 file changed, 1 insertion(+) diff --git a/compose/builder.go b/compose/builder.go index f29652a..226de5e 100644 --- a/compose/builder.go +++ b/compose/builder.go @@ -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