Skip to content

Commit db57ded

Browse files
committed
refactor: update template synchronization logic and improve error handling
1 parent 1ca52a2 commit db57ded

4 files changed

Lines changed: 20 additions & 10 deletions

File tree

cmd/vt/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ func main() {
1313
logger.Init()
1414
templates.Init()
1515

16-
// Print the banner
1716
banner.Print()
1817

1918
// Run the CLI

internal/cli/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var templateCmd = &cobra.Command{
5151
}
5252

5353
if update {
54-
templates.Update()
54+
templates.SyncTemplates()
5555
return
5656
}
5757
},

pkg/templates/downloader.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package templates
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67

@@ -20,10 +21,8 @@ func cloneTemplatesRepo(repoPath string, force bool) error {
2021
return err
2122
}
2223

23-
if !force {
24-
if !status.IsClean() {
25-
return fmt.Errorf("detected uncomitted changes in %s", repoPath)
26-
}
24+
if !force && !status.IsClean() {
25+
return fmt.Errorf("detected uncommitted changes in %s", repoPath)
2726
}
2827

2928
err = worktree.Pull(&git.PullOptions{
@@ -38,16 +37,25 @@ func cloneTemplatesRepo(repoPath string, force bool) error {
3837
return nil
3938
}
4039

40+
err = os.RemoveAll(repoPath)
41+
if err != nil {
42+
return err
43+
}
44+
4145
if err := os.MkdirAll(repoPath, 0750); err != nil {
4246
return err
4347
}
4448

4549
_, err = git.PlainClone(repoPath, false, &git.CloneOptions{
46-
URL: "https://github.com/HappyHackingSpace/vt-templates",
50+
URL: TemplateRemoteRepoistory,
4751
Depth: 1,
4852
})
4953

5054
if err != nil {
55+
removeErr := os.RemoveAll(repoPath)
56+
if removeErr != nil {
57+
return errors.Join(err, fmt.Errorf("cleanup failed: %w", removeErr))
58+
}
5159
return err
5260
}
5361

pkg/templates/template.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"github.com/rs/zerolog/log"
1111
)
1212

13+
// TemplateRemoteRepoistory is a constant for repo url.
14+
const TemplateRemoteRepoistory string = "https://github.com/HappyHackingSpace/vt-templates"
15+
1316
// Template represents a vulnerable target environment configuration.
1417
type Template struct {
1518
ID string `yaml:"id"`
@@ -100,15 +103,15 @@ func Init() {
100103
}
101104
}
102105

103-
// Update loads all templates from remote repository (force).
104-
func Update() {
106+
// SyncTemplates downloads or updates all templates from the remote repository.
107+
func SyncTemplates() {
105108
homeDir, err := os.UserHomeDir()
106109
if err != nil {
107110
log.Fatal().Msgf("%v", err)
108111
}
109112

110113
repoPath := filepath.Join(homeDir, "vt-templates")
111-
log.Info().Msgf("cloning github.com/HappyHackingSpace/vt-templates")
114+
log.Info().Msgf("cloning %s", TemplateRemoteRepoistory)
112115
err = cloneTemplatesRepo(repoPath, true)
113116
if err != nil {
114117
log.Fatal().Msgf("%v", err)

0 commit comments

Comments
 (0)