sshkey

package
v1.51.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2025 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{
	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) (any, 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{
	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) (interface{}, *hcloud.Response, error) {
		return s.Client().SSHKey().Get(s, idOrName)
	},
	Delete: func(s state.State, _ *cobra.Command, resource interface{}) (*hcloud.Action, error) {
		sshKey := resource.(*hcloud.SSHKey)
		_, 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, cmd *cobra.Command, sshKey *hcloud.SSHKey) error {
		cmd.Printf("ID:\t\t%d\n", sshKey.ID)
		cmd.Printf("Name:\t\t%s\n", sshKey.Name)
		cmd.Printf("Created:\t%s (%s)\n", util.Datetime(sshKey.Created), humanize.Time(sshKey.Created))
		cmd.Printf("Fingerprint:\t%s\n", sshKey.Fingerprint)
		cmd.Printf("Public Key:\n%s\n", strings.TrimSpace(sshKey.PublicKey))
		cmd.Print("Labels:\n")
		if len(sshKey.Labels) == 0 {
			cmd.Print("  No labels\n")
		} else {
			for key, value := range util.IterateInOrder(sshKey.Labels) {
				cmd.Printf("  %s: %s\n", key, value)
			}
		}

		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{
	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) ([]interface{}, error) {
		opts := hcloud.SSHKeyListOpts{ListOpts: listOpts}
		if len(sorts) > 0 {
			opts.Sort = sorts
		}
		sshKeys, err := s.Client().SSHKey().AllWithOpts(s, opts)

		var resources []interface{}
		for _, n := range sshKeys {
			resources = append(resources, n)
		}
		return resources, err
	},

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

	Schema: func(resources []interface{}) interface{} {
		sshKeySchemas := make([]schema.SSHKey, 0, len(resources))
		for _, resource := range resources {
			sshKey := resource.(*hcloud.SSHKey)
			sshKeySchemas = append(sshKeySchemas, hcloud.SchemaFromSSHKey(sshKey))
		}
		return sshKeySchemas
	},
}
View Source
var UpdateCmd = base.UpdateCmd{
	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) (interface{}, *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, resource interface{}, flags map[string]pflag.Value) error {
		floatingIP := resource.(*hcloud.SSHKey)
		updOpts := hcloud.SSHKeyUpdateOpts{
			Name: flags["name"].String(),
		}
		_, _, err := s.Client().SSHKey().Update(s, floatingIP, 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