Skip to content

Commit

Permalink
feat ✨: cleanup tmp dir after jobs
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Hang <[email protected]>
  • Loading branch information
Banh-Canh committed Feb 5, 2025
1 parent 6aeada5 commit 3a9786c
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,37 @@ Ginx is a cli tool that watch a remote repository and run an arbitrary command o
r, err = utils.CloneRepo(source, branch, dir)
if err != nil {
utils.Logger.Fatal("Failed to clone repository.", zap.Error(err))
err := os.RemoveAll(dir)
if err != nil {
utils.Logger.Fatal("error removing directory.", zap.Error(err))
}
}
} else {
r, err = git.PlainOpen(dir)
utils.Logger.Info("Repository already exist, open directory repository.", zap.String("directory", dir))
if err != nil {
utils.Logger.Fatal("Failed to open existing directory repository.", zap.Error(err))
err := os.RemoveAll(dir)
if err != nil {
utils.Logger.Fatal("error removing directory.", zap.Error(err))
}
}
}
if nowFlag {
if len(args) > 0 {
utils.Logger.Info("Running command.", zap.String("command", args[0]), zap.Any("args", args[1:]))
if err := utils.RunCommand(dir, args[0], args[1:]...); err != nil {
utils.Logger.Error("Failed to run command.", zap.Error(err))
err := os.RemoveAll(dir)
if err != nil {
utils.Logger.Fatal("error removing directory.", zap.Error(err))
}
}
}
err := os.RemoveAll(dir)
if err != nil {
utils.Logger.Fatal("error removing directory.", zap.Error(err))
}
os.Exit(0)
}

Expand All @@ -82,13 +98,21 @@ Ginx is a cli tool that watch a remote repository and run an arbitrary command o
utils.Logger.Debug("Fetched remote commit.", zap.String("remoteCommit", remoteCommit))
if err != nil {
utils.Logger.Fatal("error fetching local commit.", zap.Error(err))
err := os.RemoveAll(dir)
if err != nil {
utils.Logger.Fatal("error removing directory.", zap.Error(err))
}
}

// Get the latest commit hash from the local repository
localCommit, err := utils.GetLatestLocalCommit(dir)
utils.Logger.Debug("Fetched local commit.", zap.String("localCommit", localCommit))
if err != nil {
utils.Logger.Fatal("error fetching local commit.", zap.Error(err))
err := os.RemoveAll(dir)
if err != nil {
utils.Logger.Fatal("error removing directory.", zap.Error(err))
}
}

if remoteCommit != localCommit {
Expand All @@ -102,15 +126,27 @@ Ginx is a cli tool that watch a remote repository and run an arbitrary command o
_, err = utils.CloneRepo(source, branch, dir)
if err != nil {
utils.Logger.Fatal("Failed to clone repository.", zap.Error(err))
err := os.RemoveAll(dir)
if err != nil {
utils.Logger.Fatal("error removing directory.", zap.Error(err))
}
}
}
if len(args) > 0 {
utils.Logger.Info("Running command.", zap.String("command", args[0]), zap.Any("args", args[1:]))
if err := utils.RunCommand(dir, args[0], args[1:]...); err != nil {
if exitFailFlag {
utils.Logger.Fatal("Failed to run command.", zap.Error(err))
err := os.RemoveAll(dir)
if err != nil {
utils.Logger.Fatal("error removing directory.", zap.Error(err))
}
}
utils.Logger.Error("Failed to run command.", zap.Error(err))
err := os.RemoveAll(dir)
if err != nil {
utils.Logger.Fatal("error removing directory.", zap.Error(err))
}
}
}
} else {
Expand Down

0 comments on commit 3a9786c

Please sign in to comment.