all

package
v1.64.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 23 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 ListCmd = base.Cmd{
	BaseCobraCommand: func(hcapi2.Client) *cobra.Command {

		var resources []string
		for _, cmd := range allCmds {
			resources = append(resources, " - "+cmd.GetResourceNamePlural())
		}

		cmd := &cobra.Command{
			Use:   "list [options]",
			Short: "List all resources in the project",
			Long: `List all resources in the project. This does not include static/public resources like Locations, public ISOs, etc.

Listed resources are:
` + strings.Join(resources, "\n"),
		}

		cmd.Flags().StringP("selector", "l", "", "Selector to filter by labels")

		cmd.Flags().Bool("paid", false, "Only list resources that cost money (true, false)")

		output.AddFlag(cmd, output.OptionJSON(), output.OptionYAML())

		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, _ []string) error {

		paid, _ := cmd.Flags().GetBool("paid")
		labelSelector, _ := cmd.Flags().GetString("selector")

		outOpts := output.FlagsForCommand(cmd)

		cmds := allCmds
		if paid {
			cmds = []base.Listable{
				server.ListCmd,
				image.ListCmd,
				primaryip.ListCmd,
				volume.ListCmd,
				loadbalancer.ListCmd,
				floatingip.ListCmd,
				storagebox.ListCmd,
			}
		}

		type response struct {
			result []any
			err    error
		}
		responseChs := make([]chan response, len(cmds))

		for i, lc := range cmds {
			i, lc := i, lc
			ch := make(chan response)
			responseChs[i] = ch

			go func() {
				defer close(ch)

				listOpts := hcloud.ListOpts{
					LabelSelector: labelSelector,
				}

				flagSet := pflag.NewFlagSet(lc.GetJSONKeyGetByName(), pflag.ExitOnError)

				switch lc.GetJSONKeyGetByName() {
				case image.ListCmd.JSONKeyGetByName:
					flagSet.StringSlice("type", []string{"backup", "snapshot"}, "")
				case iso.ListCmd.JSONKeyGetByName:
					flagSet.StringSlice("type", []string{"private"}, "")
				}

				_ = flagSet.Parse([]string{})

				result, err := lc.FetchAny(s, flagSet, listOpts, []string{})
				ch <- response{result, err}
			}()
		}

		resources := make([][]any, len(cmds))
		for i, responseCh := range responseChs {
			response := <-responseCh
			if err := response.err; err != nil {
				return err
			}
			resources[i] = response.result
		}

		if outOpts.IsSet("json") || outOpts.IsSet("yaml") {
			schema := make(map[string]any)
			for i, lc := range cmds {
				subSchema := make([]any, 0, len(resources[i]))
				for _, resource := range resources[i] {
					subSchema = append(subSchema, lc.SchemaAny(resource))
				}
				schema[lc.GetJSONKeyGetByName()] = subSchema
			}
			if outOpts.IsSet("json") {
				return util.DescribeJSON(cmd.OutOrStdout(), schema)
			}
			return util.DescribeYAML(cmd.OutOrStdout(), schema)
		}

		for i, lc := range cmds {
			cols := lc.GetDefaultColumns()
			table := lc.NewOutputTable(cmd.OutOrStdout(), s.Client())
			table.WriteHeader(cols)

			if len(resources[i]) == 0 {
				continue
			}

			cmd.Print(strings.ToUpper(lc.GetResourceNamePlural()) + "\n---\n")
			for _, resource := range resources[i] {
				table.Write(cols, resource)
			}
			if err := table.Flush(); err != nil {
				return err
			}
			cmd.Println()
		}

		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