File: targets.go

package info (click to toggle)
singularity-container 4.1.5%2Bds4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 43,876 kB
  • sloc: asm: 14,840; sh: 3,190; ansic: 1,751; awk: 414; makefile: 413; python: 99
file content (82 lines) | stat: -rw-r--r-- 1,911 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package targets

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"text/tabwriter"

	"github.com/moby/buildkit/frontend/gateway/client"
	"github.com/moby/buildkit/frontend/subrequests"
	"github.com/moby/buildkit/solver/pb"
)

const RequestTargets = "frontend.targets"

var SubrequestsTargetsDefinition = subrequests.Request{
	Name:        RequestTargets,
	Version:     "1.0.0",
	Type:        subrequests.TypeRPC,
	Description: "List all targets current build supports",
	Opts:        []subrequests.Named{},
	Metadata: []subrequests.Named{
		{Name: "result.json"},
		{Name: "result.txt"},
	},
}

type List struct {
	Targets []Target `json:"targets"`
	Sources [][]byte `json:"sources"`
}

func (l List) ToResult() (*client.Result, error) {
	res := client.NewResult()
	dt, err := json.MarshalIndent(l, "", "  ")
	if err != nil {
		return nil, err
	}
	res.AddMeta("result.json", dt)

	b := bytes.NewBuffer(nil)
	if err := PrintTargets(dt, b); err != nil {
		return nil, err
	}
	res.AddMeta("result.txt", b.Bytes())

	res.AddMeta("version", []byte(SubrequestsTargetsDefinition.Version))
	return res, nil
}

type Target struct {
	Name        string       `json:"name,omitempty"`
	Default     bool         `json:"default,omitempty"`
	Description string       `json:"description,omitempty"`
	Base        string       `json:"base,omitempty"`
	Platform    string       `json:"platform,omitempty"`
	Location    *pb.Location `json:"location,omitempty"`
}

func PrintTargets(dt []byte, w io.Writer) error {
	var l List

	if err := json.Unmarshal(dt, &l); err != nil {
		return err
	}

	tw := tabwriter.NewWriter(w, 0, 0, 1, ' ', 0)
	fmt.Fprintf(tw, "TARGET\tDESCRIPTION\n")

	for _, t := range l.Targets {
		name := t.Name
		if name == "" && t.Default {
			name = "(default)"
		} else if t.Default {
			name = fmt.Sprintf("%s (default)", name)
		}
		fmt.Fprintf(tw, "%s\t%s\n", name, t.Description)
	}

	return tw.Flush()
}