Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var CreateCmd = base.CreateCmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "create [options] --name <name> (--public-key <key> | --public-key-from-file <file>)", Short: "Create a 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, args []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", ShortDescription: "Delete a SSH Key", NameSuggestions: func(c hcapi2.Client) func() []string { return c.SSHKey().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { return s.Client().SSHKey().Get(s, idOrName) }, Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { sshKey := resource.(*hcloud.SSHKey) if _, err := s.Client().SSHKey().Delete(s, sshKey); err != nil { return err } return nil }, }
View Source
var DescribeCmd = base.DescribeCmd{ ResourceNameSingular: "SSH Key", ShortDescription: "Describe a SSH Key", JSONKeyGetByID: "ssh_key", JSONKeyGetByName: "ssh_keys", NameSuggestions: func(c hcapi2.Client) func() []string { return c.SSHKey().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, 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, resource interface{}) error { sshKey := resource.(*hcloud.SSHKey) 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 sshKey.Labels { cmd.Printf(" %s: %s\n", key, value) } } return nil }, }
View Source
var LabelCmds = base.LabelCmds{ ResourceNameSingular: "SSH Key", ShortDescriptionAdd: "Add a label to a SSH Key", ShortDescriptionRemove: "Remove a label from a 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 }, FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { sshKey, _, err := s.Client().SSHKey().Get(s, idOrName) if err != nil { return nil, 0, err } if sshKey == nil { return nil, 0, fmt.Errorf("ssh key not found: %s", idOrName) } return sshKey.Labels, sshKey.ID, nil }, SetLabels: func(s state.State, id int64, labels map[string]string) error { opts := hcloud.SSHKeyUpdateOpts{ Labels: labels, } _, _, err := s.Client().SSHKey().Update(s, &hcloud.SSHKey{ID: id}, opts) return err }, }
View Source
var ListCmd = base.ListCmd{ ResourceNamePlural: "SSH keys", JSONKeyGetByName: "ssh_keys", DefaultColumns: []string{"id", "name", "fingerprint", "age"}, 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(_ hcapi2.Client) *output.Table { return output.NewTable(). 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: "SSHKey", ShortDescription: "Update a SSHKey", NameSuggestions: func(c hcapi2.Client) func() []string { return c.SSHKey().Names }, Fetch: func(s state.State, cmd *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, cmd *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 ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.