Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

odo init json output #5658

Merged
merged 12 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,70 @@ from the devfiles in the registries defined in the list of preferred registries

The output of this command contains a devfile name and a registry name:

```
```bash
$ odo alizer -o json
{
"devfile": "nodejs",
"registry": "DefaultDevfileRegistry"
"devfileRegistry": "DefaultDevfileRegistry"
}
$ echo $?
0
```

If the command is executed in an empty directory, it will return an error and terminate with a non-zero exit status:
If the command is executed in an empty directory, it will return an error in the standard error stream and terminate with a non-zero exit status:

```
```bash
$ odo alizer -o json
{
"message": "No valid devfile found for project in /home/user/my/empty/directory"
}
$ echo $?
1
```

## odo init -o json

The `init` command downloads a devfile and, optionally, a starter project. The usage for this command can be found in the [odo init command reference page](init.md).

The output of this command contains the path of the downloaded devfile and its content, in JSON format.

```bash
$ odo init -o json \
--name aname \
--devfile go \
--starter go-starter
{
"devfilePath": "/home/user/my-project/devfile.yaml",
"devfileData": {
"devfile": {
"schemaVersion": "2.1.0",
[...]
},
"supportedOdoFeatures": {
"dev": true,
"deploy": false,
"debug": false
}
},
"forwardedPorts": [],
"runningIn": [],
"managedBy": "odo"
}
$ echo $?
0
```

If the command fails, it will return an error in the standard error stream and terminate with a non-zero exit status:

```bash
# Executing the same command again will fail
$ odo init -o json \
--name aname \
--devfile go \
--starter go-starter
{
"message": "a devfile already exists in the current directory"
}
$ echo $?
1
```
5 changes: 3 additions & 2 deletions pkg/alizer/alizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"reflect"

"github.com/redhat-developer/alizer/go/pkg/apis/recognizer"
"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/registry"
)

Expand Down Expand Up @@ -50,8 +51,8 @@ func (o *Alizer) DetectFramework(path string) (recognizer.DevFileType, registry.
return typ, registry, nil
}

func GetDevfileLocationFromDetection(typ recognizer.DevFileType, registry registry.Registry) *DevfileLocation {
return &DevfileLocation{
func GetDevfileLocationFromDetection(typ recognizer.DevFileType, registry registry.Registry) *api.DevfileLocation {
return &api.DevfileLocation{
Devfile: typ.Name,
DevfileRegistry: registry.Name,
}
Expand Down
14 changes: 0 additions & 14 deletions pkg/alizer/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions pkg/api/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package api

type RunningMode string

const (
RunninModeDev = "Dev"
RunninModeDeploy = "Deploy"
RunninModeUnknown = "Unknown"
rm3l marked this conversation as resolved.
Show resolved Hide resolved
)

// Component describes the state of a devfile component
type Component struct {
DevfilePath string `json:"devfilePath"`
DevfileData DevfileData `json:"devfileData"`
ForwardedPorts []ForwardedPort `json:"forwardedPorts"`
RunningIn []RunningMode `json:"runningIn"`
ManagedBy string `json:"managedBy"`
}

type ForwardedPort struct {
ContainerName string `json:"containerName"`
LocalPort int `json:"localPort"`
ContainerPort int `json:"containerPort"`
}
16 changes: 16 additions & 0 deletions pkg/api/devfile-data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package api

import "github.com/devfile/library/pkg/devfile/parser/data"

// DevfileData describes a devfile content
type DevfileData struct {
Devfile data.DevfileData `json:"devfile"`
SupportedOdoFeatures SupportedOdoFeatures `json:"supportedOdoFeatures"`
}

// SupportedOdoFeatures indicates the support of high-level (odo) features by a devfile component
type SupportedOdoFeatures struct {
Dev bool `json:"dev"`
Deploy bool `json:"deploy"`
Debug bool `json:"debug"`
}
7 changes: 4 additions & 3 deletions pkg/alizer/types.go → pkg/api/devfile-location.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package alizer
package api

// DevfileLocation indicates the location of a devfile, either in a devfile registry or using a path or an URI
type DevfileLocation struct {
// name of the Devfile in Devfile registry (required if DevfilePath is not defined)
Devfile string `json:"devfile,omitempty"`

// name of the devfile registry (as configured in odo registry). It can be used in combination with Devfile, but not with DevfilePath (optional)
DevfileRegistry string `json:"devfile-registry,omitempty"`
DevfileRegistry string `json:"devfileRegistry,omitempty"`

// path to a devfile. This is alternative to using devfile from Devfile registry. It can be local filesystem path or http(s) URL (required if Devfile is not defined)
DevfilePath string `json:"devfile-path,omitempty"`
DevfilePath string `json:"devfilePath,omitempty"`
}
3 changes: 3 additions & 0 deletions pkg/api/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// package api contains structures returned by commands as JSON
rm3l marked this conversation as resolved.
Show resolved Hide resolved
// and utilities to build values of these structures
package api
22 changes: 22 additions & 0 deletions pkg/api/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package api

import (
"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/parser/data"
"github.com/redhat-developer/odo/pkg/libdevfile"
)

func GetDevfileData(devfileObj parser.DevfileObj) DevfileData {
return DevfileData{
Devfile: devfileObj.Data,
SupportedOdoFeatures: getSupportedOdoFeatures(devfileObj.Data),
}
}

func getSupportedOdoFeatures(devfileData data.DevfileData) SupportedOdoFeatures {
return SupportedOdoFeatures{
Dev: libdevfile.HasRunCommand(devfileData),
Deploy: libdevfile.HasDeployCommand(devfileData),
Debug: libdevfile.HasDebugCommand(devfileData),
}
}
8 changes: 4 additions & 4 deletions pkg/component/delete/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/init/backend/alizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/redhat-developer/odo/pkg/alizer"
"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/init/asker"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
)
Expand All @@ -27,7 +28,7 @@ func (o *AlizerBackend) Validate(flags map[string]string, fs filesystem.Filesyst
}

// SelectDevfile calls thz Alizer to detect the devfile and asks for confirmation to the user
func (o *AlizerBackend) SelectDevfile(flags map[string]string, fs filesystem.Filesystem, dir string) (location *alizer.DevfileLocation, err error) {
func (o *AlizerBackend) SelectDevfile(flags map[string]string, fs filesystem.Filesystem, dir string) (location *api.DevfileLocation, err error) {
selected, registry, err := o.alizerClient.DetectFramework(dir)
if err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions pkg/init/backend/alizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/redhat-developer/alizer/go/pkg/apis/recognizer"
"github.com/redhat-developer/odo/pkg/alizer"
"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/init/asker"
"github.com/redhat-developer/odo/pkg/registry"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
Expand Down Expand Up @@ -36,7 +37,7 @@ func TestAlizerBackend_SelectDevfile(t *testing.T) {
name string
fields fields
args args
wantLocation *alizer.DevfileLocation
wantLocation *api.DevfileLocation
wantErr bool
}{
{
Expand All @@ -61,7 +62,7 @@ func TestAlizerBackend_SelectDevfile(t *testing.T) {
fs: filesystem.DefaultFs{},
dir: GetTestProjectPath("nodejs"),
},
wantLocation: &alizer.DevfileLocation{
wantLocation: &api.DevfileLocation{
Devfile: "a-devfile-name",
DevfileRegistry: "a-registry",
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/init/backend/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
dfutil "github.com/devfile/library/pkg/util"

"github.com/redhat-developer/odo/pkg/alizer"
"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/devfile/location"
"github.com/redhat-developer/odo/pkg/preference"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
Expand Down Expand Up @@ -69,8 +69,8 @@ func (o *FlagsBackend) Validate(flags map[string]string, fs filesystem.Filesyste
return nil
}

func (o *FlagsBackend) SelectDevfile(flags map[string]string, _ filesystem.Filesystem, _ string) (*alizer.DevfileLocation, error) {
return &alizer.DevfileLocation{
func (o *FlagsBackend) SelectDevfile(flags map[string]string, _ filesystem.Filesystem, _ string) (*api.DevfileLocation, error) {
return &api.DevfileLocation{
Devfile: flags[FLAG_DEVFILE],
DevfileRegistry: flags[FLAG_DEVFILE_REGISTRY],
DevfilePath: flags[FLAG_DEVFILE_PATH],
Expand Down
6 changes: 3 additions & 3 deletions pkg/init/backend/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/devfile/library/pkg/devfile/parser/data"
dffilesystem "github.com/devfile/library/pkg/testingutil/filesystem"

"github.com/redhat-developer/odo/pkg/alizer"
"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/preference"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
)
Expand All @@ -24,7 +24,7 @@ func TestFlagsBackend_SelectDevfile(t *testing.T) {
tests := []struct {
name string
fields fields
want *alizer.DevfileLocation
want *api.DevfileLocation
wantErr bool
}{
{
Expand All @@ -37,7 +37,7 @@ func TestFlagsBackend_SelectDevfile(t *testing.T) {
},
},
wantErr: false,
want: &alizer.DevfileLocation{
want: &api.DevfileLocation{
Devfile: "adevfile",
DevfilePath: "apath",
DevfileRegistry: "aregistry",
Expand Down
6 changes: 3 additions & 3 deletions pkg/init/backend/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
parsercommon "github.com/devfile/library/pkg/devfile/parser/data/v2/common"
"github.com/fatih/color"

"github.com/redhat-developer/odo/pkg/alizer"
"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/init/asker"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/registry"
Expand Down Expand Up @@ -40,8 +40,8 @@ func (o *InteractiveBackend) Validate(flags map[string]string, fs filesystem.Fil
return nil
}

func (o *InteractiveBackend) SelectDevfile(flags map[string]string, _ filesystem.Filesystem, _ string) (*alizer.DevfileLocation, error) {
result := &alizer.DevfileLocation{}
func (o *InteractiveBackend) SelectDevfile(flags map[string]string, _ filesystem.Filesystem, _ string) (*api.DevfileLocation, error) {
result := &api.DevfileLocation{}
devfileEntries, _ := o.registryClient.ListDevfileStacks("")

langs := devfileEntries.GetLanguages()
Expand Down
8 changes: 4 additions & 4 deletions pkg/init/backend/interactive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/golang/mock/gomock"

"github.com/redhat-developer/odo/pkg/alizer"
"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/init/asker"
"github.com/redhat-developer/odo/pkg/registry"
"github.com/redhat-developer/odo/pkg/testingutil"
Expand All @@ -26,7 +26,7 @@ func TestInteractiveBackend_SelectDevfile(t *testing.T) {
tests := []struct {
name string
fields fields
want *alizer.DevfileLocation
want *api.DevfileLocation
wantErr bool
}{
{
Expand All @@ -49,7 +49,7 @@ func TestInteractiveBackend_SelectDevfile(t *testing.T) {
return client
},
},
want: &alizer.DevfileLocation{
want: &api.DevfileLocation{
Devfile: "a-devfile-name",
DevfileRegistry: "MyRegistry1",
},
Expand All @@ -76,7 +76,7 @@ func TestInteractiveBackend_SelectDevfile(t *testing.T) {
return client
},
},
want: &alizer.DevfileLocation{
want: &api.DevfileLocation{
Devfile: "a-devfile-name",
DevfileRegistry: "MyRegistry1",
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/init/backend/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package backend
import (
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/redhat-developer/odo/pkg/alizer"
"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
)

Expand All @@ -16,7 +16,7 @@ type InitBackend interface {
Validate(flags map[string]string, fs filesystem.Filesystem, dir string) error

// SelectDevfile selects a devfile and returns its location information, depending on the flags
SelectDevfile(flags map[string]string, fs filesystem.Filesystem, dir string) (location *alizer.DevfileLocation, err error)
SelectDevfile(flags map[string]string, fs filesystem.Filesystem, dir string) (location *api.DevfileLocation, err error)

// SelectStarterProject selects a starter project from the devfile and returns information about the starter project,
// depending on the flags. If not starter project is selected, a nil starter is returned
Expand Down
Loading