Auto-update Zed code editor
![](https://habrastorage.org/getpro/habr/upload_files/591/21b/abf/59121babfe2dd598736cf1a81ae5bdad.png)
I recently discovered the Zed code editor and became so interested in it that I decided to switch to it. When the stable version for Linux was released the other day, I realized that it was time to act. Much to my disappointment, most Linux package managers have an older version of Zed available than the one presented on official websiteso I have to update the application manually. So I came up with the idea of creating a program that would automatically update Zed for me. For this, I decided to use the Golang programming language.
Checking the current version
You can check the editor version with the command zed --version
.
![Command output Command output](https://habrastorage.org/getpro/habr/upload_files/add/a33/a06/adda33a066f8e77061c92d8815ec80e9.png)
This command needs to be run during our program execution. I created a global variable availableVersion
which will store the current version of the program, and a function that takes a command and its arguments, then extracts the version from the output and writes it to a variable availableVersion
. After this you can use the variable availableVersion
to compare the current version of the program with the latest available version.
var availableVersion string = ""
func execute(cmd string, arg ...string) {
out, err := exec.Command(cmd, arg[0]).Output()
if err != nil {
panic(err)
}
output := string(out[:])
availableVersion = getVersionFromString(output, 4)
}
Function getVersionFromString
takes a string and the index of the element to start processing from, and returns a minor version.
func getVersionFromString(target string, start int) string {
var version string = ""
for i, c := start, 0; i < len(target); i++ {
if target[i] == '.' {
c++
}
if c < 2 {
version += string(target[i])
}
}
return version
}
Getting the latest version
In order to check the current released version, I check the release version on Github. You can check the latest release version using a link of this type https://api.github.com/repos/owner/repo/releases/latest
.
Then I created another global variable targetVersion
which should store the current version, and the getActualVersion function, which receives json with the current version, extracts it and writes it to a variable.
var targetVersion string = ""
func getActualVersion() {
resp, err := http.Get("https://api.github.com/repos/zed-industries/zed/releases/latest")
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
rm := make(map[string]json.RawMessage)
err = json.Unmarshal(body, &rm)
if err != nil {
panic(err)
}
targetVersion = getVersionFromString(string(rm["tag_name"]), 2)
}
Comparison of versions
All that remains is to compare the versions, and if they do not match, run the installation/update script with official website.
func main() {
execute("zed", "--version")
getActualVersion()
if availableVersion != targetVersion {
execute("curl https://zed.dev/install.sh | sh")
fmt.Println("\n\n\nSuccessfully update")
} else {
fmt.Println("The versions match")
}
}
Adding to crontab
In order for everything to be fully automated, the program needs to be compiled. go build main.go
and add the executable file to crontab.
Summary
I wrote a simple Go script that checks for updates to the Zed code editor once a week and installs them if there are any. You can see the full code on my Github. I hope this article was helpful. All the best!