forked from redhat-developer/odo
/
project.go
72 lines (65 loc) · 1.86 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
63
64
65
66
67
68
69
70
71
72
package project
import (
"github.com/pkg/errors"
"github.com/redhat-developer/odo/pkg/occlient"
)
// ApplicationInfo holds information about one project
type ProjectInfo struct {
// Name of the project
Name string
// is this project active?
Active bool
}
func GetCurrent(client *occlient.Client) string {
project := client.GetCurrentProjectName()
return project
}
func SetCurrent(client *occlient.Client, project string) error {
err := client.SetCurrentProject(project)
if err != nil {
return errors.Wrap(err, "unable to set current project to"+project)
}
return nil
}
func Create(client *occlient.Client, projectName string) error {
err := client.CreateNewProject(projectName)
if err != nil {
return errors.Wrap(err, "unable to create new project")
}
return nil
}
func List(client *occlient.Client) ([]ProjectInfo, error) {
currentProject := client.GetCurrentProjectName()
allProjects, err := client.GetProjectNames()
if err != nil {
return nil, errors.Wrap(err, "cannot get all the projects")
}
projects := []ProjectInfo{}
for _, project := range allProjects {
isActive := false
if project == currentProject {
isActive = true
}
projects = append(projects, ProjectInfo{
Name: project,
Active: isActive,
})
}
return projects, nil
}
// Checks whether a project with the given name exists or not
// projectName is the project name to perform check for
// The first returned parameter is a bool indicating if a project with the given name already exists or not
// The second returned parameter is the error that might occurs while execution
func Exists(client *occlient.Client, projectName string) (bool, error) {
projects, err := List(client)
if err != nil {
return false, errors.Wrap(err, "unable to get the project list")
}
for _, project := range projects {
if project.Name == projectName {
return true, nil
}
}
return false, nil
}