goself
Onboard the goself library into a Go CLI or service so it can install itself locally or on remote hosts over ssh, with cross-arch rebuilds. Use when adding an `install` or `deploy` command to a Go project.
Onboarding goself
goself (ubunatic.com/goself) installs a Go binary on the local host or on a remote host over
the system ssh client. It picks the binary with a Strategy, cross-compiles when the target
platform differs, and installs atomically (temp file + rename), so replacing a running binary is
safe.
1. Add the dependency
go get ubunatic.com/goself@latest
2. Wire an install command
Keep the cobra command thin; pass flags into goself.Options.
func newInstallCmd() *cobra.Command {
var strategy, dir, version string
cmd := &cobra.Command{
Use: "install [host]",
Short: "Install this binary locally or on a remote host over ssh",
Args: cobra.MaximumNArgs(1),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
s, err := goself.ParseStrategy(strategy)
if err != nil {
return err
}
t := goself.Target{Dir: dir}
if len(args) == 1 {
t.Runner = goself.SSH(args[0])
}
res, err := goself.New(goself.Options{
Strategy: s,
Version: version,
Release: goself.ReleaseSource{
BaseURL: "https://codeberg.org/<owner>/<repo>/releases/download/v{version}",
},
}).Deploy(cmd.Context(), t)
if err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "installed %s (%s, %s)\n", res.Path, res.Platform, res.Strategy)
return nil
},
}
cmd.Flags().StringVar(&strategy, "strategy", "auto", "auto, self, build, release or go")
cmd.Flags().StringVar(&dir, "dir", "~/.local/bin", "install directory on the target")
cmd.Flags().StringVar(&version, "version", "", "version for the release and go strategies")
return cmd
}
3. Strategies
| Strategy | Source | Notes |
|---|---|---|
| auto | checkout โ build; else same platform โ self; else โ go | default |
| self | the running executable | fails on platform mismatch |
| build | local checkout, CGO_ENABLED=0 cross build | refuses uncommitted changes for remote targets unless AllowDirty |
| release | <BaseURL>/<archive> + checksums.txt | GoReleaser names by default; override ReleaseSource.Archive/Checksums |
| go | go install pkg@version upstream | needs the binary's build info, or Options.Version; Direct sets GOPROXY=direct |
To ship the binary yourself (e.g. bundled with other runtime files), call
Deployer.Binary(ctx, platform, dir) instead of Deploy; goself.DetectPlatform(ctx, runner)
gives the target's platform. lmcoder's localInstallBinary is a working example.
4. Requirements and traps
- Static binaries. Build release and installed binaries with
CGO_ENABLED=0. A cgo binary linked against glibc fails with "not found" on musl hosts (Alpine), soselfreplication of it breaks there. - Reproducible builds.
auto/goacross platforms rebuild the exact version from build info. Binaries fromgo run, or withvcs.modified=true, cannot be reproduced; goself returns an error instead of guessing. - ssh. goself runs
ssh -o BatchMode=yes, so keys or an agent must work non-interactively. Pass extra flags withgoself.SSH(host, "-p", "2222"). - Targets. linux and darwin, amd64/arm64/arm/riscv64, with a POSIX
/bin/shanduname.
5. Verify
- Unit test your command with
goself.Target{Dir: t.TempDir()}andStrategy: goself.Self. - For remote installs, run your binary against a throwaway sshd container; see goself's
container_test.goandtestdata/sshd/Containerfilefor a ready harness.
- Content
- file-backed
- License:
- AGPL-3.0-or-later
- Origin
- goself
- Source:
- skills/goself/SKILL.md
- Source revision
737612b384e05c8bc003a41473fe767a791d4348- Requires
- Targets
- Invocation