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 ¶
Types ¶
type Opt ¶
type Opt func(*UpdateCmd)
func WithDownloadSuffix ¶
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 (*UpdateCmd) AddToRootCommand ¶
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