Skip to content

Instantly share code, notes, and snippets.

View sitaramshelke's full-sized avatar
👨‍💻
poking nose into others' code

Sitaram Shelke sitaramshelke

👨‍💻
poking nose into others' code
View GitHub Profile
@sitaramshelke
sitaramshelke / updatable-go-binary-snippet6.go
Created November 28, 2023 14:12
Snippet for self updatable go binaries
// Code snippet: Apply the self-update
bin, err := getExecutableFromAsset(assetId, format)
if err != nil {
// Handle error - can't get executable
}
updateOpts := selfupdate.Options{}
if err = selfupdate.Apply(bytes.NewBuffer(bin), updateOpts); err != nil {
// Handle error - update failed, roll back if necessary
}
@sitaramshelke
sitaramshelke / updatable-go-binary-snippet5.go
Created November 28, 2023 14:10
Snippet for self updatable go binaries
// Code snippet: To extract executable
func getExecutableFromAsset(assetId AssetId, format AssetFormat) ([]byte, error) {
buff, err := downloadAssetFromID(assetId)
if format == Zip {
zipReader, err := zip.NewReader(bytes.NewReader(buff.Bytes()), int64(buff.Len()))
// Zip reader logic
} else if format == Tar {
gzipReader, err := gzip.NewReader(buff)
@sitaramshelke
sitaramshelke / updatable-go-binary-snippet4.go
Created November 28, 2023 14:05
Snippet for self updatable go binaries
// Code snippet: Download the latest version
func downloadAssetFromID(assetId AssetId) (*bytes.Buffer, error) {
_, rdurl, err := githubClient.Repositories.DownloadReleaseAsset(context.Background(), "one2nc", "cloudlens", int64(assetId), nil)
resp, err := httpClient.Get(rdurl)
defer resp.Body.Close()
bin, err := io.ReadAll(resp.Body)
return bytes.NewBuffer(bin), nil
@sitaramshelke
sitaramshelke / updatable-go-binary-snippet3.go
Created November 28, 2023 13:54
Snippet for self updatable go binaries
// Code snippet: Lookup executable in the latest release information
var assetId AssetId
var format AssetFormat
loop:
for _, v := range latest.Assets {
asset := *v.Name
switch {
case strings.Contains(asset, ".zip"):
if strings.EqualFold(asset, executableName+".zip") {
assetId = AssetId(*v.ID)
@sitaramshelke
sitaramshelke / updatable-go-binary-snippet2.go
Created November 28, 2023 13:50
Snippet for self updatable go binaries
// Code snippet: Form an executable name based on assets in releases page
executableName := "cloudlens_" + strings.TrimPrefix(latest.GetTagName(), "v") + "_" + runtime.GOOS + "_" + runtime.GOARCH
@sitaramshelke
sitaramshelke / updatable-go-binary-snippet1.go
Created November 28, 2023 13:47
Snippet for self updatable go binaries
// Code snippet: Fetch the latest release information from GitHub
latest, _, err := githubClient.Repositories.GetLatestRelease(context.Background(), "<org>", "<repo>")
@sitaramshelke
sitaramshelke / shared_lib.groovy
Created February 4, 2021 02:36
Jenkins shared library input example
// src/com/example/Helper.groovy
#!/usr/bin/env groovy
package com.example
class Helper implements Serializable {
def context
Helper(context) {
this.context = context
}
@sitaramshelke
sitaramshelke / lambda_fib.rb
Created April 7, 2019 07:40
Fibanacci assignment from SICP Meetup 1
puts "=========== Iterative ============"
def fib(n)
i = 0
f = 0
s = 1
puts f
puts s
while i < n - 2
t = f
f = s

Keybase proof

I hereby claim:

  • I am sitaramshelke on github.
  • I am sitaramshelke (https://keybase.io/sitaramshelke) on keybase.
  • I have a public key whose fingerprint is 84E0 BFC7 D4B6 E61B 2DB2 D53D 3969 963E 0E35 C14F

To claim this, I am signing this object: