This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
create.go
94 lines (86 loc) · 2.49 KB
/
create.go
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
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"github.com/openshift/library-go/pkg/git"
s2igit "github.com/openshift/source-to-image/pkg/scm/git"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
)
type createOption struct {
name string
image string
imageStream string
toDocker bool
}
func validateCreateOpts(opt createOption) error {
if opt.name == "" {
return errors.New("name is empty")
}
if opt.image == "" {
return errors.New("image is empty")
}
if opt.imageStream == "" {
return errors.New("image-stream is empty")
}
return nil
}
func createCommand(opts kobwOptions) *cobra.Command {
var opt createOption
cmd := &cobra.Command{
Use: "create",
Short: "create or update the build config",
RunE: func(cmd *cobra.Command, args []string) error {
// debug()
if err := validateCreateOpts(opt); err != nil {
return err
}
config, err := clientcmd.BuildConfigFromFlags(opts.masterURL, opts.kubeconfig)
if err != nil {
return errors.Wrap(err, "could not create kubernetes client config")
}
if err := createBuildConfig(config, args[0], opt); err != nil {
return errors.Wrap(err, "failed to create BuildConfig")
}
if !opt.toDocker {
if err := createImageStreamIfNeeded(config, opt.image); err != nil {
return errors.Wrapf(err, "failed to create imagestream %s", opt.image)
}
}
return nil
},
}
cmd.Flags().StringVar(&opt.name, "name", "", "build configuration name")
cmd.Flags().StringVar(&opt.image, "image", "", "image name to push")
cmd.Flags().StringVar(&opt.imageStream, "image-stream", "", "image stream to use as build input")
cmd.Flags().BoolVar(&opt.toDocker, "to-docker", true, "push the image to a docker registry")
return cmd
}
func detectSource(path string) (string, string, error) {
var source string
var revision string
url, err := s2igit.Parse(path)
if err != nil {
return "", "", err
}
gitRepo := git.NewRepository()
if url.IsLocal() {
remote, ok, err := gitRepo.GetOriginURL(path)
if err != nil && err != git.ErrGitNotAvailable {
return "", "", errors.Wrap(err, "could not detect source")
}
if !ok {
return "", "", fmt.Errorf("source is not supported %s (git: %s, %s)", path, url, remote)
}
info, errs := gitRepo.GetInfo(path)
if errs != nil && len(errs) > 0 {
return "", "", fmt.Errorf("could not get information for repository: %v", errs)
}
source = remote
revision = info.CommitID
} else {
source = path
revision = "master"
}
return source, revision, nil
}