Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the gokey cmd main to an importable package #70

Merged
merged 2 commits into from
Apr 29, 2024

Conversation

pdmccormick
Copy link
Contributor

Thank you for this great utility! It is so handy in fact that I would love to be able to bundle it into other programs. To this end, this PR lifts the contents of cmd/gokey (a non-importable main package with an unexported main function) into cmd/gokey/gokeycmd (as an importable gokeycmd package with an exported Main function). This enables the gokey cmd main to be imported into other single executable binary, multiple use Go programs.

How exactly these bundled programs are invoked will vary, but the Busybox-style approach uses multiple symlinks pointing to a common executable, with the symlink name indicating which subprogram should be run.

For example, consider multicmd.go:

package main

import (
        "fmt"
        "os"
        "path/filepath"

        "github.com/cloudflare/gokey/cmd/gokey/gokeycmd"
)

func main() {
        switch filepath.Base(os.Args[0]) {
        case "helloworld":
                fmt.Println("Hello World!")
        case "gokey":
                gokeycmd.Main()
        default:
                fmt.Println("run me as an executable named either `helloworld` or `gokey`")
        }
}

If this program is compiled, and either the executable is renamed to gokey or helloworld, or symlinks with those names are created, the different sub-commands will be invoked accordingly.

$ go build multicmd.go
$ ln -s multicmd helloworld
$ ln -s multicmd gokey

$ ./helloworld
Hello World!
$ ./gokey -p gokey -r gokeycmd
1'amOFD4yF

Thank you for the consideration.

@ignatk ignatk self-assigned this Apr 29, 2024
@ignatk ignatk self-requested a review April 29, 2024 09:58
Copy link
Contributor

@ignatk ignatk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution. FYI I have updated ci workflows and rebased the PR for the CI to run

@ignatk ignatk merged commit 19e52d5 into cloudflare:main Apr 29, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants