Skip to content

Commit

Permalink
feat(build): rollback implementation (#30)
Browse files Browse the repository at this point in the history
Signed-off-by: Ruslan Semagin <[email protected]>
  • Loading branch information
pixel365 authored Feb 20, 2025
1 parent 1b2e155 commit 3449c1e
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 8 deletions.
62 changes: 54 additions & 8 deletions internal/module_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,32 @@ func (m *Module) Cleanup(log *zerolog.Logger) error {
}

func (m *Module) Rollback(log *zerolog.Logger) error {
//TODO: implementation
zipPath, err := makeZipFilePath(m)
if err != nil {
return err
}

if zipStat, err := os.Stat(zipPath); err == nil && !zipStat.IsDir() {
err := os.Remove(zipPath)
if err != nil {
return err
}

log.Info().Msgf("Removed zip file: %s", zipPath)
}

versionDir, err := makeVersionDirectory(m)
if err != nil {
return err
}

if err := os.RemoveAll(versionDir); err != nil {
return err
}

log.Info().Msgf("Removed version directory: %s", versionDir)
log.Info().Msg("Rollback complete")

return nil
}

Expand All @@ -142,14 +167,11 @@ func (m *Module) Push(log *zerolog.Logger) error {
}

func (m *Module) Collect(log *zerolog.Logger) error {
buildDirectory, err := filepath.Abs(fmt.Sprintf("%s/%s", m.BuildDirectory, m.Version))
versionDirectory, err := makeVersionDirectory(m)
if err != nil {
log.Error().Err(err).Msg("Failed to make build directory")
return err
}

buildDirectory = filepath.Clean(buildDirectory)

var wg sync.WaitGroup
errCh := make(chan error, len(m.Stages))

Expand All @@ -159,7 +181,7 @@ func (m *Module) Collect(log *zerolog.Logger) error {
}

wg.Add(1)
go handleStage(m.Ctx, &wg, errCh, log, &m.Ignore, item, buildDirectory)
go handleStage(m.Ctx, &wg, errCh, log, &m.Ignore, item, versionDirectory)
}

wg.Wait()
Expand All @@ -177,8 +199,12 @@ func (m *Module) Collect(log *zerolog.Logger) error {

log.Info().Msg("Collect complete")

zipPath := filepath.Join(m.BuildDirectory, fmt.Sprintf("%s.zip", m.Version))
if err := zipIt(buildDirectory, zipPath); err != nil {
zipPath, err := makeZipFilePath(m)
if err != nil {
return err
}

if err := zipIt(versionDirectory, zipPath); err != nil {
log.Error().Err(err).Msg("Failed to zip build")
return err
}
Expand Down Expand Up @@ -228,3 +254,23 @@ func handleStage(
go copyFromPath(ctx, wg, errCh, ignore, from, to, item.ActionIfFileExists)
}
}

func makeZipFilePath(module *Module) (string, error) {
path := filepath.Join(module.BuildDirectory, fmt.Sprintf("%s.zip", module.Version))
path, err := filepath.Abs(path)
if err != nil {
return "", err
}
path = filepath.Clean(path)
return path, nil
}

func makeVersionDirectory(module *Module) (string, error) {
path := filepath.Join(module.BuildDirectory, module.Version)
path, err := filepath.Abs(path)
if err != nil {
return "", err
}
path = filepath.Clean(path)
return path, nil
}
87 changes: 87 additions & 0 deletions internal/module_builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package internal

import (
"fmt"
"os"
"testing"
)

func Test_makeZipFilePath(t *testing.T) {
mod1 := &Module{
BuildDirectory: "testdata",
Version: "1.0.0",
}

mod2 := &Module{
BuildDirectory: "testdata/build",
Version: "1.0.1",
}

cur, _ := os.Getwd()

type args struct {
module *Module
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{"1", args{mod1}, fmt.Sprintf("%s/testdata/1.0.0.zip", cur), false},
{"2", args{mod2}, fmt.Sprintf("%s/testdata/build/1.0.1.zip", cur), false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := makeZipFilePath(tt.args.module)
if (err != nil) != tt.wantErr {
t.Errorf("makeZipFilePath() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("makeZipFilePath() got = %v, want %v", got, tt.want)
}
})
}
}

func Test_makeVersionDirectory(t *testing.T) {
mod1 := &Module{
BuildDirectory: "testdata",
Version: "1.0.0",
}

mod2 := &Module{
BuildDirectory: "testdata/build",
Version: "1.0.1",
}

cur, _ := os.Getwd()

type args struct {
module *Module
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{"1", args{mod1}, fmt.Sprintf("%s/testdata/1.0.0", cur), false},
{"2", args{mod2}, fmt.Sprintf("%s/testdata/build/1.0.1", cur), false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := makeVersionDirectory(tt.args.module)
if (err != nil) != tt.wantErr {
t.Errorf("makeVersionDirectory() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("makeVersionDirectory() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 3449c1e

Please sign in to comment.