-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add commands for installing and updating dependencies (#26)
- Added `install-deps` command to install all required dependencies. - Added `update-deps` command to update all dependencies or specific ones individually. - Refactored to use constants (e.g., `go`, `weaver`) for better maintainability and easier implementation of future unit tests. - Enhanced project initialization by ensuring a `go.mod` file is created automatically. These changes streamline dependency management and improve code maintainability.
- Loading branch information
Showing
9 changed files
with
170 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
module github.com/renanbastos93/boneless | ||
|
||
go 1.20 | ||
go 1.22.0 | ||
|
||
require github.com/pelletier/go-toml/v2 v2.0.8 | ||
toolchain go1.22.4 | ||
|
||
require ( | ||
github.com/pelletier/go-toml/v2 v2.0.8 | ||
golang.org/x/mod v0.22.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package internal | ||
|
||
const ( | ||
goCLI = "go" | ||
sqlcCLI = "sqlc" | ||
weaverCLI = "weaver" | ||
migrateCLI = "migrate" | ||
installCmd = "install" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package internal | ||
|
||
import ( | ||
"os/exec" | ||
) | ||
|
||
type packages struct { | ||
name, pkg string | ||
} | ||
|
||
var PackagesForInstall = []packages{ | ||
{"golang-migrate", "github.com/golang-migrate/migrate/v4/cmd/migrate@latest"}, | ||
{"sqlc", "github.com/sqlc-dev/sqlc/cmd/sqlc@latest"}, | ||
{"weaver", "github.com/ServiceWeaver/weaver/cmd/weaver@latest"}, | ||
} | ||
|
||
func InstallDeps(name string) { | ||
if name != "" { | ||
for _, p := range PackagesForInstall { | ||
if p.name == name { | ||
installDeps(p) | ||
} | ||
} | ||
} else { | ||
installDeps(PackagesForInstall...) | ||
} | ||
} | ||
|
||
func installDeps(packages ...packages) { | ||
for _, p := range packages { | ||
if IsInstalled(p.pkg) { | ||
println(p.name, "already installed!") | ||
continue | ||
} | ||
GoInstall(p.name, p.pkg) | ||
} | ||
} | ||
|
||
func IsInstalled(packageName string) bool { | ||
pathBin, err := exec.LookPath(packageName) | ||
return err != nil || pathBin == "" | ||
} | ||
|
||
func GoInstall(packageName string, args ...string) { | ||
cmdArgs := append([]string{installCmd}, args...) | ||
err := runCmd(goCLI, cmdArgs...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
println(packageName, "installed!") | ||
} | ||
|
||
func UpdateDeps(name string) { | ||
if name != "" { | ||
for _, p := range PackagesForInstall { | ||
if p.name == name { | ||
updateDeps(p) | ||
} | ||
} | ||
} else { | ||
updateDeps(PackagesForInstall...) | ||
} | ||
} | ||
|
||
func updateDeps(packages ...packages) { | ||
for _, p := range packages { | ||
GoInstall(p.name, p.pkg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package internal | ||
|
||
import "fmt" | ||
|
||
func ModInit() { | ||
moduleName, err := getModuleNameFromUserInput() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
args := []string{"mod", "init"} | ||
if moduleName != "" { | ||
args = append(args, moduleName) | ||
} | ||
|
||
err = runCmd(goCLI, args...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
} | ||
|
||
func getModuleNameFromUserInput() (string, error) { | ||
print("What is the module name for your project? (e.g., github.com/renanbastos93/boneless) ") | ||
|
||
var moduleName string | ||
_, err := fmt.Scanf("%s\n", &moduleName) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return moduleName, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters