Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --check flag to the update command #49

Merged
merged 11 commits into from
Jul 3, 2024
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ We contributors to BuildSafe:
* House (@dr-housemd)
* Rakshit(@rakshitgondwal)
* Praful(@Horiodino)
* Sanyam(@sanyamjain04)
* Sanyam(@sanyamjain04)
* Hanshal(@hanshal101)
25 changes: 18 additions & 7 deletions cmd/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"github.com/buildsafedev/bsf/pkg/update"
)

var check bool

// UpdateCmd represents the update command
var UpdateCmd = &cobra.Command{
Use: "update",
Expand All @@ -29,8 +31,7 @@ var UpdateCmd = &cobra.Command{

Currently, only packages following semver versioning are supported.


`,
`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(styles.TextStyle.Render("Updating..."))

Expand All @@ -42,7 +43,7 @@ var UpdateCmd = &cobra.Command{

data, err := os.ReadFile("bsf.hcl")
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error: ", err.Error()))
fmt.Println(styles.ErrorStyle.Render("error:", err.Error()))
os.Exit(1)
}

Expand All @@ -55,7 +56,7 @@ var UpdateCmd = &cobra.Command{

sc, err := search.NewClientWithAddr(conf.BuildSafeAPI, conf.BuildSafeAPITLS)
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error: ", err.Error()))
fmt.Println(styles.ErrorStyle.Render("error:", err.Error()))
os.Exit(1)
}

Expand All @@ -70,6 +71,15 @@ var UpdateCmd = &cobra.Command{
Runtime: runtimeVersions,
}

if check {
if !update.ComparePackages(hconf.Packages.Development, devVersions) || !update.ComparePackages(hconf.Packages.Runtime, runtimeVersions) {
fmt.Println(styles.WarnStyle.Render("Updates are available"))
os.Exit(1)
}
fmt.Println(styles.SucessStyle.Render("No updates available"))
os.Exit(0)
}

fh, err := hcl2nix.NewFileHandlers(true)
if err != nil {
fmt.Println(styles.ErrorStyle.Render("Error creating file handlers: %s", err.Error()))
Expand All @@ -95,7 +105,6 @@ var UpdateCmd = &cobra.Command{
}

fmt.Println(styles.SucessStyle.Render("Updated ran successfully"))

},
}

Expand Down Expand Up @@ -129,9 +138,7 @@ func parsePackagesForUpdates(versionMap map[string]*buildsafev1.FetchPackagesRes
case update.UpdateTypePinned:
newVersions = append(newVersions, k)
continue

}

}
return newVersions
}
Expand Down Expand Up @@ -165,3 +172,7 @@ func fetchPackageVersions(packages []string, sc buildsafev1.SearchServiceClient)

return versionsMap
}

func init() {
UpdateCmd.PersistentFlags().BoolVarP(&check, "check", "c", false, "Check for updates without applying them")
}
19 changes: 19 additions & 0 deletions pkg/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,22 @@ func TrimVersionInfo(pkg string) (string, string) {

return name, version
}

// ComparePackages compares the devVersions and the runtimeVersions
func ComparePackages(a, b []string) bool {
if len(a) != len(b) {
return false
}

counts := make(map[string]bool)
for _, item := range a {
counts[item] = true
}
for _, item := range b {
if !counts[item] {
return false
}
}

return true
}
36 changes: 36 additions & 0 deletions pkg/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,39 @@ func TestParseUpdateType(t *testing.T) {
})
}
}

func TestComparePackages(t *testing.T) {
tests := []struct {
name string
a []string
b []string
want bool
}{
{
name: "Test Case 1 - Equal slices",
a: []string{"pkg1@~v1.0.0", "pkg2@~v1.1.0", "pkg3@~v1.2.0"},
b: []string{"pkg3@~v1.2.0", "pkg2@~v1.1.0", "pkg1@~v1.0.0"},
want: true,
},
{
name: "Test Case 2 - Different slices",
a: []string{"pkg1@~v1.0.0", "pkg2@~v1.1.0", "pkg3@~v1.2.0"},
b: []string{"pkg1@~v1.2.0", "pkg2@~v1.1.0", "pkg3@~v1.3.0"},
want: false,
},
{
name: "Test Case 3 - Different lengths",
a: []string{"pkg1@~v1.0.0", "pkg2@~v1.1.0"},
b: []string{"pkg2@~v1.2.0", "pkg1@~v1.1.0", "pkg3@~v1.0.0"},
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ComparePackages(tt.a, tt.b); got != tt.want {
t.Errorf("CompareVersions() = %v, want %v", got, tt.want)
}
})
}
}
Loading