2021-04-09 14:20:26 +08:00
|
|
|
package buildflags
|
2019-03-24 12:30:29 +08:00
|
|
|
|
|
|
|
import (
|
2024-11-22 02:06:14 +08:00
|
|
|
"cmp"
|
|
|
|
"slices"
|
2019-03-24 12:30:29 +08:00
|
|
|
"strings"
|
|
|
|
|
2023-02-09 20:03:58 +08:00
|
|
|
controllerapi "github.com/docker/buildx/controller/pb"
|
2021-04-02 02:08:56 +08:00
|
|
|
"github.com/moby/buildkit/util/gitutil"
|
2019-03-24 12:30:29 +08:00
|
|
|
)
|
|
|
|
|
2024-11-22 02:06:14 +08:00
|
|
|
type SSH struct {
|
|
|
|
ID string `json:"id,omitempty" cty:"id"`
|
|
|
|
Paths []string `json:"paths,omitempty" cty:"paths"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SSH) Equal(other *SSH) bool {
|
|
|
|
return s.Less(other) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SSH) Less(other *SSH) int {
|
|
|
|
if s.ID != other.ID {
|
|
|
|
return cmp.Compare(s.ID, other.ID)
|
|
|
|
}
|
|
|
|
return slices.Compare(s.Paths, other.Paths)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SSH) String() string {
|
|
|
|
if len(s.Paths) == 0 {
|
|
|
|
return s.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
var b csvBuilder
|
|
|
|
paths := strings.Join(s.Paths, ",")
|
|
|
|
b.Write(s.ID, paths)
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SSH) ToPB() *controllerapi.SSH {
|
|
|
|
return &controllerapi.SSH{
|
|
|
|
ID: s.ID,
|
|
|
|
Paths: s.Paths,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SSH) UnmarshalText(text []byte) error {
|
|
|
|
parts := strings.SplitN(string(text), "=", 2)
|
|
|
|
|
|
|
|
s.ID = parts[0]
|
|
|
|
if len(parts) > 1 {
|
|
|
|
s.Paths = strings.Split(parts[1], ",")
|
|
|
|
} else {
|
|
|
|
s.Paths = nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-09 20:03:58 +08:00
|
|
|
func ParseSSHSpecs(sl []string) ([]*controllerapi.SSH, error) {
|
|
|
|
var outs []*controllerapi.SSH
|
|
|
|
if len(sl) == 0 {
|
|
|
|
return nil, nil
|
2019-03-24 12:30:29 +08:00
|
|
|
}
|
|
|
|
|
2023-02-09 20:03:58 +08:00
|
|
|
for _, s := range sl {
|
2024-11-22 02:06:14 +08:00
|
|
|
var out SSH
|
|
|
|
if err := out.UnmarshalText([]byte(s)); err != nil {
|
|
|
|
return nil, err
|
2023-02-09 20:03:58 +08:00
|
|
|
}
|
2024-11-22 02:06:14 +08:00
|
|
|
outs = append(outs, out.ToPB())
|
2019-03-24 12:30:29 +08:00
|
|
|
}
|
2023-02-09 20:03:58 +08:00
|
|
|
return outs, nil
|
2019-03-24 12:30:29 +08:00
|
|
|
}
|
2021-04-02 02:08:56 +08:00
|
|
|
|
|
|
|
// IsGitSSH returns true if the given repo URL is accessed over ssh
|
2023-09-07 19:13:54 +08:00
|
|
|
func IsGitSSH(repo string) bool {
|
|
|
|
url, err := gitutil.ParseURL(repo)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return url.Scheme == gitutil.SSHProtocol
|
2021-04-02 02:08:56 +08:00
|
|
|
}
|