Skip to main content

rkubectl_app/cli/command/
get.rs

1use super::*;
2
3/// Display one or many resources
4///
5///  Prints a table of the most important information about the specified resources. You can filter the list using a label
6/// selector and the --selector flag. If the desired resource type is namespaced you will only see results in the current
7/// namespace if you don't specify any namespace.
8///
9///  By specifying the output as 'template' and providing a Go template as the value of the --template flag, you can filter
10/// the attributes of the fetched resources.
11///
12/// Use "kubectl api-resources" for a complete list of supported resources.
13///
14#[derive(Clone, Debug, Args)]
15
16pub struct Get {
17    #[command(flatten)]
18    params: ShowParams,
19
20    /// Raw URI to request from the server.  Uses the transport specified by the kubeconfig file.
21    #[arg(long, conflicts_with = "regular")]
22    raw: Option<String>,
23
24    /// If specified, gets the subresource of the requested object.
25    #[arg(group = "regular", long)]
26    subresource: Option<String>,
27
28    /// Resources to fetch
29    #[arg(group = "regular", required = true)]
30    resources: Option<Vec<String>>,
31}
32
33impl Get {
34    pub async fn exec(&self, context: &Context) -> RkResult<()> {
35        let kubeapi = context.kubeapi();
36        if let Some(raw) = self.raw.as_deref() {
37            let name = raw.strip_prefix("/").unwrap_or(raw);
38            let text = kubeapi.raw_get(name).await?;
39            println!("{text}");
40        } else {
41            let resources = self.resources(kubeapi)?;
42            let mut params = self.params;
43            params.show_kind |= resources.len() > 1;
44            let namespace = kubeapi.show_namespace();
45            for resource in resources {
46                let data = resource.get(kubeapi).await?;
47                let output = context.output_deprecated();
48                println!("{}", data.output(namespace, params, output));
49            }
50        }
51        Ok(())
52    }
53
54    fn resources(&self, kubeapi: &Kubeapi) -> kube::Result<Vec<ResourceArg>> {
55        let resources = self.resources.as_deref().unwrap_or_default();
56        ResourceArg::from_strings(resources, kubeapi)
57            .inspect(|resources| info!(args=?self.resources, ?resources))
58            .map_err(|_err| kube::Error::LinesCodecMaxLineLengthExceeded)
59    }
60}