summaryrefslogtreecommitdiff
path: root/pkg/bindings/generate/generate.go
blob: daca8046fb0e2a2e7e6b0e1692480a4e48001df6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package generate

import (
	"context"
	"errors"
	"net/http"

	"github.com/containers/podman/v4/pkg/bindings"
	"github.com/containers/podman/v4/pkg/domain/entities"
)

func Systemd(ctx context.Context, nameOrID string, options *SystemdOptions) (*entities.GenerateSystemdReport, error) {
	if options == nil {
		options = new(SystemdOptions)
	}
	conn, err := bindings.GetClient(ctx)
	if err != nil {
		return nil, err
	}
	params, err := options.ToParams()
	if err != nil {
		return nil, err
	}

	response, err := conn.DoRequest(ctx, nil, http.MethodGet, "/generate/%s/systemd", params, nil, nameOrID)
	if err != nil {
		return nil, err
	}
	defer response.Body.Close()

	report := &entities.GenerateSystemdReport{}
	return report, response.Process(&report.Units)
}

// Kube generate Kubernetes YAML (v1 specification)
//
// Note: Caller is responsible for closing returned reader
func Kube(ctx context.Context, nameOrIDs []string, options *KubeOptions) (*entities.GenerateKubeReport, error) {
	if options == nil {
		options = new(KubeOptions)
	}
	conn, err := bindings.GetClient(ctx)
	if err != nil {
		return nil, err
	}
	if len(nameOrIDs) < 1 {
		return nil, errors.New("must provide the name or ID of one container or pod")
	}

	params, err := options.ToParams()
	if err != nil {
		return nil, err
	}
	for _, name := range nameOrIDs {
		params.Add("names", name)
	}
	response, err := conn.DoRequest(ctx, nil, http.MethodGet, "/generate/kube", params, nil)
	if err != nil {
		return nil, err
	}

	if response.StatusCode == http.StatusOK {
		return &entities.GenerateKubeReport{Reader: response.Body}, nil
	}

	// Unpack the error.
	return nil, response.Process(nil)
}