selfupdate

package
v0.9.7 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2025 License: GPL-3.0 Imports: 11 Imported by: 0

README

Self Update

This package provides the ability to plug in another subcommand into your cobra app, that will expose the below subcommand.

Updates the binary to the specified or latest version.

Usage:
  eirctl update [flags]

Aliases:
  update, self-update

Flags:
      --baseUrl string   base url for the github release repository (default "https://github.com/Ensono/eirctl/releases")
  -h, --help             help for update
      --version string   specific version to update to. (default "latest")

Global Flags:
  -c, --config eirctl.yaml   config file to use - eirctl.yaml. For backwards compatibility it also accepts taskctl.yaml and tasks.yaml (default "eirctl.yaml")
  -d, --debug                enable debug level logging
      --dry-run              dry run
      --no-summary           show summary
  -q, --quiet                quite mode
      --verbose              enable trace level logging

The above example is from embedding in the eirctl utility

Github Releases

It supports GitHub releases OOTB, but custom functions for GetVersion can be provided, you can see ExampleUpdateCmd_withOwnGetFunc for more details.

Documentation

Overview

Package selfupdate provides the subcommand functionality for updating the binary itself.

It supports an OOTB GitHub releases URLs for fetching with some customisation, though if too rigid an entire fetch function can be provided.

Example:

WithGetVersionFunc()

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnrichFinalLink(link string) string

Types

type Opt

type Opt func(*UpdateCmd)

func WithDownloadSuffix

func WithDownloadSuffix(suffix string) Opt

func WithGetVersionFunc

func WithGetVersionFunc(fn func(ctx context.Context, flags UpdateCmdFlags, w io.WriteCloser) error) Opt

WithGetVersionFunc accepts a custom function that will encapsulate the entire fetch logic w io.WriteCloser will point to the current executable.

Ensure your custom function handles the `w.Write(resp.Body)`

func WithOsFsOps

func WithOsFsOps(osfs updateOsFSOpsIface) Opt

type UpdateCmd

type UpdateCmd struct {
	OsFsOps updateOsFSOpsIface
	// contains filtered or unexported fields
}
Example (WithOwnGetFunc)

Example with custom GetVersionFunc

package main

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"os"
	"testing"

	"github.com/Ensono/eirctl/output"
	"github.com/Ensono/eirctl/selfupdate"
	"github.com/spf13/cobra"
)

type mockCloser struct {
	io.Writer
}

func (m mockCloser) Close() error {
	return nil
}

type mockOsFsOps struct {
	exec   func() (string, error)
	rename func(oldpath string, newpath string) error
	create func(name string) (io.WriteCloser, error)
	chmod  func(name string, mode os.FileMode) error
}

func (o mockOsFsOps) Rename(oldpath string, newpath string) error {
	if o.rename != nil {
		return o.rename(oldpath, newpath)
	}
	return nil
}

func (o mockOsFsOps) Create(name string) (io.WriteCloser, error) {
	if o.create != nil {
		return o.create(name)
	}
	return mockCloser{&bytes.Buffer{}}, nil
}

func (o mockOsFsOps) Executable() (string, error) {
	if o.exec != nil {
		return o.exec()
	}
	return "/my/exec/binary", nil
}

func (o mockOsFsOps) Chmod(name string, mode os.FileMode) error {
	if o.chmod != nil {
		return o.chmod(name, mode)
	}
	return nil
}

func cmdHelper(t *testing.T, out, errOut io.Writer) *cobra.Command {
	t.Helper()
	rootCmd := &cobra.Command{}
	rootCmd.SetArgs([]string{"self-update"})
	rootCmd.SetErr(errOut)
	rootCmd.SetOut(out)
	return rootCmd
}

func main() {

	setOutput := ""
	getFunc := func(ctx context.Context, flags selfupdate.UpdateCmdFlags, w io.WriteCloser) error {
		// You can encapsulate the entire fetch logic in a custom function
		setOutput = "binary(contents....)"
		return nil
	}

	errOut := output.NewSafeWriter(&bytes.Buffer{})
	stdOut := output.NewSafeWriter(&bytes.Buffer{})
	rootCmd := cmdHelper(&testing.T{}, stdOut, errOut)

	// See cmd/eirctl/eirctl.go for a more complete example
	uc := selfupdate.New("my-binary", "http://ignored.com", selfupdate.WithGetVersionFunc(getFunc), selfupdate.WithOsFsOps(mockOsFsOps{}))
	uc.AddToRootCommand(rootCmd)

	if err := rootCmd.ExecuteContext(context.TODO()); err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(string(setOutput))
	fmt.Println(stdOut.String())

}
Output:

binary(contents....)
my-binary has been updated

func New

func New(name string, ghReleaseUrl string, opts ...Opt) *UpdateCmd

func (*UpdateCmd) AddToRootCommand

func (uc *UpdateCmd) AddToRootCommand(rootCmd *cobra.Command)

AddToRootCommand

func (*UpdateCmd) GetVersion

func (uc *UpdateCmd) GetVersion(ctx context.Context, flags UpdateCmdFlags, w io.WriteCloser) error

GetVersion downloads the binary stream from remote endpoint

NOTE: exposed as public for testing purposes

This can be overwritten completely to support any kind of fetcher

type UpdateCmdFlags

type UpdateCmdFlags struct {
	BaseUrl string
	Version string
}

Jump to

Keyboard shortcuts

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