GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

identity_cmd.go 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/spf13/cobra"
  6. )
  7. type identityOpts struct {
  8. *rootOpts
  9. regenerate bool
  10. fingerprint bool
  11. visual bool
  12. }
  13. func newIdentity(parent *rootOpts) *identityOpts {
  14. return &identityOpts{rootOpts: parent}
  15. }
  16. func (opts *identityOpts) Command() *cobra.Command {
  17. cmd := &cobra.Command{
  18. Use: "identity",
  19. Short: "Display SSH public key",
  20. RunE: opts.RunE,
  21. }
  22. cmd.Flags().BoolVarP(&opts.regenerate, "regenerate", "r", false, `Generate a new identity`)
  23. cmd.Flags().BoolVarP(&opts.fingerprint, "fingerprint", "l", false, `Show fingerprint of public key`)
  24. cmd.Flags().BoolVarP(&opts.visual, "visual", "v", false, `Show ASCII art representation with fingerprint (implies -l)`)
  25. return cmd
  26. }
  27. func (opts *identityOpts) RunE(_ *cobra.Command, args []string) error {
  28. if len(args) > 0 {
  29. return errorWantedNoArgs
  30. }
  31. ctx := context.Background()
  32. repoConfig, err := opts.API.GitRepoConfig(ctx, opts.regenerate)
  33. if err != nil {
  34. return err
  35. }
  36. publicSSHKey := repoConfig.PublicSSHKey
  37. if opts.visual {
  38. opts.fingerprint = true
  39. }
  40. if opts.fingerprint {
  41. fmt.Println(publicSSHKey.Fingerprints["md5"].Hash)
  42. if opts.visual {
  43. fmt.Print(publicSSHKey.Fingerprints["md5"].Randomart)
  44. }
  45. } else {
  46. fmt.Print(publicSSHKey.Key)
  47. }
  48. return nil
  49. }