Merge pull request #30 from containscafeine/tie-application-with-project

make applications tied to OpenShift projects
This commit is contained in:
Shubham
2018-02-05 18:12:37 +05:30
committed by GitHub
4 changed files with 40 additions and 54 deletions

View File

@@ -17,6 +17,7 @@ package cmd
import (
"fmt"
"github.com/redhat-developer/ocdev/pkg/application"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)
@@ -40,7 +41,22 @@ var createCmd = &cobra.Command{
},
}
var getCmd = &cobra.Command{
Use: "get",
Short: "get the active application",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
app, err := application.GetCurrent()
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
log.Infof("The current application is: %v", app)
},
}
func init() {
applicationCmd.AddCommand(getCmd)
applicationCmd.AddCommand(createCmd)
rootCmd.AddCommand(applicationCmd)
}

View File

@@ -1,15 +1,14 @@
package application
import (
"fmt"
"github.com/pkg/errors"
"github.com/redhat-developer/ocdev/pkg/config"
"github.com/redhat-developer/ocdev/pkg/occlient"
log "github.com/sirupsen/logrus"
)
const defaultApplication = "app"
// Create creates a new application and binds it to the current project.
// Create creates a new application and switches to it.
// If no name is provided, the application is named as in the constant
// "defaultApplication".
@@ -18,37 +17,21 @@ const defaultApplication = "app"
// If no project is set, this errors out.
func Create(name string) error {
// Get current project name
project, err := occlient.GetCurrentProjectName()
if err != nil {
return errors.Wrap(err, "unable to get current project's name")
}
// Set default application name if not set
if len(name) == 0 {
name = defaultApplication
}
app := config.Application{
Name: name,
Project: project,
if err := occlient.CreateNewProject(name); err != nil {
return errors.Wrapf(err, "unable to create application: %v", name)
}
ocdevConfig, err := config.New()
if err != nil {
return errors.Wrap(err, "error getting config")
}
// Check if application exists
if ocdevConfig.ApplicationExists(&app) {
return fmt.Errorf("application %v already exists in project %v", app.Name, app.Project)
}
// Add application to config
err = ocdevConfig.AddApplication(&app)
if err != nil {
return errors.Wrap(err, "unable to add application to config")
}
log.Infof("Switching to application: %v", name)
return nil
}
func GetCurrent() (string, error) {
app, err := occlient.GetCurrentProjectName()
if err != nil {
return "", errors.Wrap(err, "unable to get the active application")
}
return app, nil
}

View File

@@ -16,14 +16,8 @@ const (
configFileName = "ocdev"
)
type Application struct {
Name string `json:"name"`
Project string `json:"project"`
}
type Config struct {
Applications []Application `json:"applications"`
CurrentApplication string `json:"currentApplication"`
ActiveComponents map[string]string `json:"activeComponents"`
}
type ConfigInfo struct {
@@ -99,20 +93,3 @@ func (c *ConfigInfo) set() error {
return nil
}
func (c *ConfigInfo) ApplicationExists(inputApp *Application) bool {
for _, app := range c.Applications {
if reflect.DeepEqual(inputApp, &app) {
return true
}
}
return false
}
func (c *ConfigInfo) AddApplication(app *Application) error {
c.Applications = append(c.Applications, *app)
if err := c.set(); err != nil {
return errors.Wrap(err, "unable to set config data")
}
return nil
}

View File

@@ -146,6 +146,16 @@ func GetCurrentProjectName() (string, error) {
return string(output), nil
}
func CreateNewProject(name string) error {
_, err := runOcComamnd(&OcCommand{
args: []string{"new-project", name},
})
if err != nil {
return errors.Wrap(err, "unable to create new project")
}
return nil
}
// // GetDeploymentConfig returns information about DeploymentConfig
// func (occlient *OcClient) GetDeploymentConfig(name string) (*ov1.DeploymentConfig, error) {
// args := []string{