update

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var UpdateCmd = &cobra.Command{
	Use:   "update",
	Short: "updates dependencies to highest version based on constraints",
	Long: `Updates can be done for development and runtime dependencies based on constraints. Following constraints are supported:
		~ : latest patch version
		^ : latest minor version
		# : date based updates. Useful, when a package doesn't follow semver and we want to update to the latest published version
		Currently, only packages following semver versioning are supported.

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

		conf, err := configure.PreCheckConf()
		if err != nil {
			fmt.Println(styles.ErrorStyle.Render("error:", err.Error()))
			os.Exit(1)
		}

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

		var dstErr bytes.Buffer
		hconf, err := hcl2nix.ReadConfig(data, &dstErr)
		if err != nil {
			fmt.Println(styles.ErrorStyle.Render(dstErr.String()))
			os.Exit(1)
		}

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

		devVersionMap := fetchPackageVersions(hconf.Packages.Development, sc)
		runtimeVersionMap := fetchPackageVersions(hconf.Packages.Runtime, sc)

		devVersions := parsePackagesForUpdates(devVersionMap)
		runtimeVersions := parsePackagesForUpdates(runtimeVersionMap)

		sort.Strings(devVersions)
		sort.Strings(runtimeVersions)

		newPackages := hcl2nix.Packages{
			Development: devVersions,
			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()))
			os.Exit(1)
		}

		fh.ModFile, err = os.Create("bsf.hcl")
		if err != nil {
			fmt.Println(styles.ErrorStyle.Render("Error creating bsf.hcl: %s", err.Error()))
			os.Exit(1)
		}

		err = hcl2nix.SetPackages(data, newPackages, fh.ModFile)
		if err != nil {
			fmt.Println(styles.ErrorStyle.Render("Error updating bsf.hcl: %s", err.Error()))
			os.Exit(1)
		}

		err = generate.Generate(fh, sc)
		if err != nil {
			fmt.Println(styles.ErrorStyle.Render("Error generating files: %s", err.Error()))
			os.Exit(1)
		}

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

UpdateCmd represents the update command

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL