mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
go.mod still declared `module github.com/hanzoai/deploy` after the repo was renamed. A Go module path is not cosmetic — it is the resolvable location. So `go get github.com/hanzoai/cd` could not work, and anything importing this module had to spell a name that no longer exists at that address. The build log of v3.6.0 is where it surfaced: the compiler was still emitting github.com/hanzoai/deploy/cmd/cd/commands. 3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from the module path, replaced separately so neither could clobber the other. go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean. Co-authored-by: Hanzo Dev <[email protected]>
202 lines
6.4 KiB
Go
202 lines
6.4 KiB
Go
package commands
|
|
|
|
import (
|
|
stderrors "errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"text/tabwriter"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/hanzoai/cd/cmd/cd/commands/headless"
|
|
"github.com/hanzoai/cd/cmd/cd/commands/utils"
|
|
cdclient "github.com/hanzoai/cd/pkg/apiclient"
|
|
gpgkeypkg "github.com/hanzoai/cd/pkg/apiclient/gpgkey"
|
|
appsv1 "github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
|
|
"github.com/hanzoai/cd/util/errors"
|
|
utilio "github.com/hanzoai/cd/util/io"
|
|
"github.com/hanzoai/cd/util/templates"
|
|
)
|
|
|
|
// NewGPGCommand returns a new instance of an `cd repo` command
|
|
func NewGPGCommand(clientOpts *cdclient.ClientOptions) *cobra.Command {
|
|
command := &cobra.Command{
|
|
Use: "gpg",
|
|
Short: "Manage GPG keys used for signature verification",
|
|
Run: func(c *cobra.Command, args []string) {
|
|
c.HelpFunc()(c, args)
|
|
os.Exit(1)
|
|
},
|
|
Example: ``,
|
|
}
|
|
command.AddCommand(NewGPGListCommand(clientOpts))
|
|
command.AddCommand(NewGPGGetCommand(clientOpts))
|
|
command.AddCommand(NewGPGAddCommand(clientOpts))
|
|
command.AddCommand(NewGPGDeleteCommand(clientOpts))
|
|
return command
|
|
}
|
|
|
|
// NewGPGListCommand lists all configured public keys from the server
|
|
func NewGPGListCommand(clientOpts *cdclient.ClientOptions) *cobra.Command {
|
|
var output string
|
|
command := &cobra.Command{
|
|
Use: "list",
|
|
Short: "List configured GPG public keys",
|
|
Example: templates.Examples(`
|
|
# List all configured GPG public keys in wide format (default).
|
|
cd gpg list
|
|
|
|
# List all configured GPG public keys in JSON format.
|
|
cd gpg list -o json
|
|
|
|
# List all configured GPG public keys in YAML format.
|
|
cd gpg list -o yaml
|
|
`),
|
|
|
|
Run: func(c *cobra.Command, _ []string) {
|
|
ctx := c.Context()
|
|
|
|
conn, gpgIf := headless.NewClientOrDie(clientOpts, c).NewGPGKeyClientOrDie()
|
|
defer utilio.Close(conn)
|
|
keys, err := gpgIf.List(ctx, &gpgkeypkg.GnuPGPublicKeyQuery{})
|
|
errors.CheckError(err)
|
|
switch output {
|
|
case "yaml", "json":
|
|
err := PrintResourceList(keys.Items, output, false)
|
|
errors.CheckError(err)
|
|
case "wide", "":
|
|
printKeyTable(keys.Items)
|
|
default:
|
|
errors.CheckError(fmt.Errorf("unknown output format: %s", output))
|
|
}
|
|
},
|
|
}
|
|
command.Flags().StringVarP(&output, "output", "o", "wide", "Output format. One of: json|yaml|wide")
|
|
return command
|
|
}
|
|
|
|
// NewGPGGetCommand retrieves a single public key from the server
|
|
func NewGPGGetCommand(clientOpts *cdclient.ClientOptions) *cobra.Command {
|
|
var output string
|
|
command := &cobra.Command{
|
|
Use: "get KEYID",
|
|
Short: "Get the GPG public key with ID <KEYID> from the server",
|
|
Example: templates.Examples(`
|
|
# Get a GPG public key with the specified KEYID in wide format (default).
|
|
cd gpg get KEYID
|
|
|
|
# Get a GPG public key with the specified KEYID in JSON format.
|
|
cd gpg get KEYID -o json
|
|
|
|
# Get a GPG public key with the specified KEYID in YAML format.
|
|
cd gpg get KEYID -o yaml
|
|
`),
|
|
|
|
Run: func(c *cobra.Command, args []string) {
|
|
ctx := c.Context()
|
|
|
|
if len(args) != 1 {
|
|
errors.Fatal(errors.ErrorGeneric, "Missing KEYID argument")
|
|
}
|
|
conn, gpgIf := headless.NewClientOrDie(clientOpts, c).NewGPGKeyClientOrDie()
|
|
defer utilio.Close(conn)
|
|
key, err := gpgIf.Get(ctx, &gpgkeypkg.GnuPGPublicKeyQuery{KeyID: args[0]})
|
|
errors.CheckError(err)
|
|
switch output {
|
|
case "yaml", "json":
|
|
err := PrintResourceList(key, output, false)
|
|
errors.CheckError(err)
|
|
case "wide", "":
|
|
fmt.Printf("Key ID: %s\n", key.KeyID)
|
|
fmt.Printf("Key fingerprint: %s\n", key.Fingerprint)
|
|
fmt.Printf("Key subtype: %s\n", strings.ToUpper(key.SubType))
|
|
fmt.Printf("Key owner: %s\n", key.Owner)
|
|
fmt.Printf("Key data follows until EOF:\n%s\n", key.KeyData)
|
|
default:
|
|
errors.CheckError(fmt.Errorf("unknown output format: %s", output))
|
|
}
|
|
},
|
|
}
|
|
command.Flags().StringVarP(&output, "output", "o", "wide", "Output format. One of: json|yaml|wide")
|
|
return command
|
|
}
|
|
|
|
// NewGPGAddCommand adds a public key to the server's configuration
|
|
func NewGPGAddCommand(clientOpts *cdclient.ClientOptions) *cobra.Command {
|
|
var fromFile string
|
|
command := &cobra.Command{
|
|
Use: "add",
|
|
Short: "Adds a GPG public key to the server's keyring",
|
|
Example: templates.Examples(`
|
|
# Add a GPG public key to the server's keyring from a file.
|
|
cd gpg add --from /path/to/keyfile
|
|
`),
|
|
|
|
Run: func(c *cobra.Command, _ []string) {
|
|
ctx := c.Context()
|
|
|
|
if fromFile == "" {
|
|
errors.CheckError(stderrors.New("--from is mandatory"))
|
|
}
|
|
keyData, err := os.ReadFile(fromFile)
|
|
if err != nil {
|
|
errors.CheckError(err)
|
|
}
|
|
conn, gpgIf := headless.NewClientOrDie(clientOpts, c).NewGPGKeyClientOrDie()
|
|
defer utilio.Close(conn)
|
|
resp, err := gpgIf.Create(ctx, &gpgkeypkg.GnuPGPublicKeyCreateRequest{Publickey: &appsv1.GnuPGPublicKey{KeyData: string(keyData)}})
|
|
errors.CheckError(err)
|
|
fmt.Printf("Created %d key(s) from input file", len(resp.Created.Items))
|
|
if len(resp.Skipped) > 0 {
|
|
fmt.Printf(", and %d key(s) were skipped because they exist already", len(resp.Skipped))
|
|
}
|
|
fmt.Print(".\n")
|
|
},
|
|
}
|
|
command.Flags().StringVarP(&fromFile, "from", "f", "", "Path to the file that contains the GPG public key to import")
|
|
return command
|
|
}
|
|
|
|
// NewGPGDeleteCommand removes a key from the server's keyring
|
|
func NewGPGDeleteCommand(clientOpts *cdclient.ClientOptions) *cobra.Command {
|
|
command := &cobra.Command{
|
|
Use: "rm KEYID",
|
|
Short: "Removes a GPG public key from the server's keyring",
|
|
Run: func(c *cobra.Command, args []string) {
|
|
ctx := c.Context()
|
|
|
|
if len(args) != 1 {
|
|
errors.Fatal(errors.ErrorGeneric, "Missing KEYID argument")
|
|
}
|
|
|
|
keyId := args[0]
|
|
|
|
conn, gpgIf := headless.NewClientOrDie(clientOpts, c).NewGPGKeyClientOrDie()
|
|
defer utilio.Close(conn)
|
|
|
|
promptUtil := utils.NewPrompt(clientOpts.PromptsEnabled)
|
|
canDelete := promptUtil.Confirm(fmt.Sprintf("Are you sure you want to remove '%s'? [y/n] ", keyId))
|
|
if canDelete {
|
|
_, err := gpgIf.Delete(ctx, &gpgkeypkg.GnuPGPublicKeyQuery{KeyID: keyId})
|
|
errors.CheckError(err)
|
|
fmt.Printf("Deleted key with key ID %s\n", keyId)
|
|
} else {
|
|
fmt.Printf("The command to delete key with key ID '%s' was cancelled.\n", keyId)
|
|
}
|
|
},
|
|
}
|
|
return command
|
|
}
|
|
|
|
// Print table of certificate info
|
|
func printKeyTable(keys []appsv1.GnuPGPublicKey) {
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
|
fmt.Fprint(w, "KEYID\tTYPE\tIDENTITY\n")
|
|
|
|
for _, k := range keys {
|
|
fmt.Fprintf(w, "%s\t%s\t%s\n", k.KeyID, strings.ToUpper(k.SubType), k.Owner)
|
|
}
|
|
_ = w.Flush()
|
|
}
|