Skip to content

Commit

Permalink
Added update option
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelonn committed Aug 23, 2024
1 parent d414945 commit 8f5080b
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions gitdeps.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ func PrintHelp() {
fmt.Println("Usage: gitdeps [options]")
fmt.Println("")
fmt.Println("Options:")
fmt.Println(" -f --force Re-clone existing modules")
fmt.Println(" -f --force Remove, then clone existing modules")
fmt.Println(" -u --update Update existing modules")
fmt.Println(" -n --no-recurse Do not update getdeps of the root modules")
fmt.Println("")
fmt.Println("gitdeps " + Version)
}

func Execute(args []string) {
force := false
update := false
noRecurse := false

if duplicate := CheckStrDuplicates(args); duplicate != "" {
Expand All @@ -54,6 +56,14 @@ func Execute(args []string) {
os.Exit(1)
}
force = true
} else if arg == "-u" || arg == "--update" {
if update {
fmt.Println("Duplicate arguments: update")
fmt.Println("")
PrintHelp()
os.Exit(1)
}
update = true
} else if arg == "-n" || arg == "--no-recurse" {
if noRecurse {
fmt.Println("Duplicate arguments: no-recurse")
Expand All @@ -76,7 +86,7 @@ func Execute(args []string) {
os.Exit(1)
}

err = UpdateDeps(workingDir, force, noRecurse)
err = UpdateDeps(workingDir, update, force, noRecurse)

if err != nil {
fmt.Println(err)
Expand All @@ -86,7 +96,7 @@ func Execute(args []string) {
}
}

func UpdateDeps(workingDir string, force bool, noRecurse bool) error {
func UpdateDeps(workingDir string, update bool, force bool, noRecurse bool) error {
depsFile := path.Join(workingDir, "gitdeps.json")

file, err := os.OpenFile(depsFile, os.O_RDONLY, 0644)
Expand Down Expand Up @@ -128,6 +138,8 @@ func UpdateDeps(workingDir string, force bool, noRecurse bool) error {

fullPath := filepath.Clean(filepath.Join(workingDir, modulePath))

repoInitialized := false

_, err := os.Lstat(fullPath)
if err != nil {
if os.IsNotExist(err) {
Expand All @@ -140,24 +152,32 @@ func UpdateDeps(workingDir string, force bool, noRecurse bool) error {
return errors.New(depsFile + ": '" + modulePath + "': " + err.Error())
}
} else {
if !force {
if force {
err := os.RemoveAll(fullPath)
if err != nil {
return errors.New(depsFile + ": '" + modulePath + "': " + err.Error())
}
err = os.MkdirAll(fullPath, 0777)
if err != nil {
return errors.New(depsFile + ": '" + modulePath + "': " + err.Error())
}
// Continue execution
} else if update {
repoInitialized = true
// Continue execution
} else {
fmt.Println("Skipped " + fullPath)
continue
}
err := os.RemoveAll(fullPath)
if err != nil {
return errors.New(depsFile + ": '" + modulePath + "': " + err.Error())
}
err = os.MkdirAll(fullPath, 0777)
}

if !repoInitialized {
err = RunCommand(fullPath, "git", "init")
if err != nil {
return errors.New(depsFile + ": '" + modulePath + "': " + err.Error())
}
// Continue execution
}

err = RunCommand(fullPath, "git", "init")
if err != nil {
return errors.New(depsFile + ": '" + modulePath + "': " + err.Error())
} else {
RunCommand(fullPath, "git", "remote", "remove", "origin")
}

err = RunCommand(fullPath, "git", "remote", "add", "origin", module.URL)
Expand Down Expand Up @@ -201,7 +221,7 @@ func UpdateDeps(workingDir string, force bool, noRecurse bool) error {
return errors.New(depsFile + ": '" + modulePath + "': " + err.Error())
}
} else {
err = UpdateDeps(fullPath, force, noRecurse)
err = UpdateDeps(fullPath, update, force, noRecurse)
if err != nil {
return err
}
Expand Down

0 comments on commit 8f5080b

Please sign in to comment.