sshkey

package
v1.63.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CreateCmd = base.CreateCmd[*hcloud.SSHKey]{
	BaseCobraCommand: func(hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:   "create [options] --name <name> (--public-key <key> | --public-key-from-file <file>)",
			Short: "Create an SSH Key",
		}
		cmd.Flags().String("name", "", "Key name (required)")
		_ = cmd.MarkFlagRequired("name")

		cmd.Flags().String("public-key", "", "Public key")
		cmd.Flags().String("public-key-from-file", "", "Path to file containing public key")
		cmd.MarkFlagsMutuallyExclusive("public-key", "public-key-from-file")

		cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)")
		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, _ []string) (*hcloud.SSHKey, any, error) {
		name, _ := cmd.Flags().GetString("name")
		publicKey, _ := cmd.Flags().GetString("public-key")
		publicKeyFile, _ := cmd.Flags().GetString("public-key-from-file")
		labels, _ := cmd.Flags().GetStringToString("label")

		if publicKeyFile != "" {
			var (
				data []byte
				err  error
			)
			if publicKeyFile == "-" {
				data, err = io.ReadAll(os.Stdin)
			} else {
				data, err = os.ReadFile(publicKeyFile)
			}
			if err != nil {
				return nil, nil, err
			}
			publicKey = string(data)
		}

		opts := hcloud.SSHKeyCreateOpts{
			Name:      name,
			PublicKey: publicKey,
			Labels:    labels,
		}
		sshKey, _, err := s.Client().SSHKey().Create(s, opts)
		if err != nil {
			return nil, nil, err
		}

		cmd.Printf("SSH Key %d created\n", sshKey.ID)

		return sshKey, util.Wrap("ssh_key", hcloud.SchemaFromSSHKey(sshKey)), nil
	},
}
View Source
var DeleteCmd = base.DeleteCmd[*hcloud.SSHKey]{
	ResourceNameSingular: "SSH Key",
	ResourceNamePlural:   "SSH Keys",
	ShortDescription:     "Delete an SSH Key",
	NameSuggestions:      func(c hcapi2.Client) func() []string { return c.SSHKey().Names },
	Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.SSHKey, *hcloud.Response, error) {
		return s.Client().SSHKey().Get(s, idOrName)
	},
	Delete: func(s state.State, _ *cobra.Command, sshKey *hcloud.SSHKey) (*hcloud.Action, error) {
		_, err := s.Client().SSHKey().Delete(s, sshKey)
		return nil, err
	},
}
View Source
var DescribeCmd = base.DescribeCmd[*hcloud.SSHKey]{
	ResourceNameSingular: "SSH Key",
	ShortDescription:     "Describe an SSH Key",
	NameSuggestions:      func(c hcapi2.Client) func() []string { return c.SSHKey().Names },
	Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.SSHKey, any, error) {
		key, _, err := s.Client().SSHKey().Get(s, idOrName)
		if err != nil {
			return nil, nil, err
		}
		return key, hcloud.SchemaFromSSHKey(key), nil
	},
	PrintText: func(_ state.State, _ *cobra.Command, out io.Writer, sshKey *hcloud.SSHKey) error {
		fmt.Fprintf(out, "ID:\t%d\n", sshKey.ID)
		fmt.Fprintf(out, "Name:\t%s\n", sshKey.Name)
		fmt.Fprintf(out, "Created:\t%s (%s)\n", util.Datetime(sshKey.Created), humanize.Time(sshKey.Created))
		fmt.Fprintf(out, "Fingerprint:\t%s\n", sshKey.Fingerprint)

		fmt.Fprintln(out)
		fmt.Fprintf(out, "Public Key:\n%s\n", strings.TrimSpace(sshKey.PublicKey))

		fmt.Fprintln(out)
		util.DescribeLabels(out, sshKey.Labels, "")

		return nil
	},
}
View Source
var LabelCmds = base.LabelCmds[*hcloud.SSHKey]{
	ResourceNameSingular:   "SSH Key",
	ShortDescriptionAdd:    "Add a label to an SSH Key",
	ShortDescriptionRemove: "Remove a label from an SSH Key",
	NameSuggestions:        func(c hcapi2.Client) func() []string { return c.SSHKey().Names },
	LabelKeySuggestions:    func(c hcapi2.Client) func(idOrName string) []string { return c.SSHKey().LabelKeys },
	Fetch: func(s state.State, idOrName string) (*hcloud.SSHKey, error) {
		sshKey, _, err := s.Client().SSHKey().Get(s, idOrName)
		if err != nil {
			return nil, err
		}
		if sshKey == nil {
			return nil, fmt.Errorf("SSH Key not found: %s", idOrName)
		}
		return sshKey, nil
	},
	SetLabels: func(s state.State, sshKey *hcloud.SSHKey, labels map[string]string) error {
		opts := hcloud.SSHKeyUpdateOpts{
			Labels: labels,
		}
		_, _, err := s.Client().SSHKey().Update(s, sshKey, opts)
		return err
	},
	GetLabels: func(sshKey *hcloud.SSHKey) map[string]string {
		return sshKey.Labels
	},
	GetIDOrName: func(sshKey *hcloud.SSHKey) string {
		return strconv.FormatInt(sshKey.ID, 10)
	},
}
View Source
var ListCmd = &base.ListCmd[*hcloud.SSHKey, schema.SSHKey]{
	ResourceNamePlural: "SSH Keys",
	JSONKeyGetByName:   "ssh_keys",
	DefaultColumns:     []string{"id", "name", "fingerprint", "age"},
	SortOption:         config.OptionSortSSHKey,

	Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]*hcloud.SSHKey, error) {
		opts := hcloud.SSHKeyListOpts{ListOpts: listOpts}
		if len(sorts) > 0 {
			opts.Sort = sorts
		}
		return s.Client().SSHKey().AllWithOpts(s, opts)
	},

	OutputTable: func(t *output.Table[*hcloud.SSHKey], _ hcapi2.Client) {
		t.
			AddAllowedFields(&hcloud.SSHKey{}).
			AddFieldFn("labels", func(sshKey *hcloud.SSHKey) string {
				return util.LabelsToString(sshKey.Labels)
			}).
			AddFieldFn("created", func(sshKey *hcloud.SSHKey) string {
				return util.Datetime(sshKey.Created)
			}).
			AddFieldFn("age", func(sshKey *hcloud.SSHKey) string {
				return util.Age(sshKey.Created, time.Now())
			})
	},

	Schema: hcloud.SchemaFromSSHKey,
}
View Source
var UpdateCmd = base.UpdateCmd[*hcloud.SSHKey]{
	ResourceNameSingular: "SSH Key",
	ShortDescription:     "Update an SSH Key",
	NameSuggestions:      func(c hcapi2.Client) func() []string { return c.SSHKey().Names },
	Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.SSHKey, *hcloud.Response, error) {
		return s.Client().SSHKey().Get(s, idOrName)
	},
	DefineFlags: func(cmd *cobra.Command) {
		cmd.Flags().String("name", "", "SSH Key name")
	},
	Update: func(s state.State, _ *cobra.Command, sshKey *hcloud.SSHKey, flags map[string]pflag.Value) error {
		updOpts := hcloud.SSHKeyUpdateOpts{
			Name: flags["name"].String(),
		}
		_, _, err := s.Client().SSHKey().Update(s, sshKey, updOpts)
		if err != nil {
			return err
		}
		return nil
	},
}

Functions

func NewCommand

func NewCommand(s state.State) *cobra.Command

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL