forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
62 lines (53 loc) · 1.58 KB
/
project.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
package openshift
import (
"io"
"io/ioutil"
"k8s.io/apimachinery/pkg/api/errors"
kclientcmd "k8s.io/client-go/tools/clientcmd"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/openshift/origin/pkg/oc/cli/cmd"
"github.com/openshift/origin/pkg/oc/cli/config"
)
// CreateProject creates a project
func CreateProject(f *clientcmd.Factory, name, display, desc, basecmd string, out io.Writer) error {
projectClient, err := f.OpenshiftInternalProjectClient()
if err != nil {
return err
}
pathOptions := config.NewPathOptionsWithConfig("")
opt := &cmd.NewProjectOptions{
ProjectName: name,
DisplayName: display,
Description: desc,
Name: basecmd,
Client: projectClient.Project(),
ProjectOptions: &cmd.ProjectOptions{PathOptions: pathOptions},
Out: ioutil.Discard,
}
err = opt.ProjectOptions.Complete(f, []string{}, ioutil.Discard)
if err != nil {
return err
}
err = opt.Run()
if err != nil {
if errors.IsAlreadyExists(err) {
return setCurrentProject(f, name, out)
}
return err
}
return nil
}
func setCurrentProject(f *clientcmd.Factory, name string, out io.Writer) error {
pathOptions := config.NewPathOptionsWithConfig("")
opt := &cmd.ProjectOptions{PathOptions: pathOptions}
opt.Complete(f, []string{name}, out)
return opt.RunProject()
}
func LoggedInUserFactory() (*clientcmd.Factory, error) {
cfg, err := config.NewOpenShiftClientConfigLoadingRules().Load()
if err != nil {
return nil, err
}
defaultCfg := kclientcmd.NewDefaultClientConfig(*cfg, &kclientcmd.ConfigOverrides{})
return clientcmd.NewFactory(defaultCfg), nil
}