Skip to content

Commit 61d53dc

Browse files
valaparthvirm3l
andauthored
Add name detection to odo analyze (#6682)
* Add name detection to odo analyze Signed-off-by: Parthvi Vala <[email protected]> * Update tests/helper/helper_generic.go Co-authored-by: Armel Soro <[email protected]> * Update tests/integration/cmd_analyze_test.go Co-authored-by: Armel Soro <[email protected]> * Update odo analyze documentation Signed-off-by: Parthvi Vala <[email protected]> --------- Signed-off-by: Parthvi Vala <[email protected]> Co-authored-by: Armel Soro <[email protected]>
1 parent 163c164 commit 61d53dc

File tree

7 files changed

+23
-6
lines changed

7 files changed

+23
-6
lines changed

docs/website/docs/command-reference/json-output.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The structures used to return information using JSON output are defined in [the
2222
The `analyze` command analyzes the files in the current directory and returns the following information:
2323
- the best devfiles to use, from the devfiles in the registries defined in the list of preferred registries with the command `odo preference view`
2424
- the ports used in the application, if that was possible to determine.
25+
- the name of the application, if that was possible to determine; else it returns name of the current directory.
2526

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

@@ -35,7 +36,8 @@ odo analyze -o json
3536
"devfileRegistry": "DefaultDevfileRegistry",
3637
"ports": [
3738
3000
38-
]
39+
],
40+
"name": "node-echo"
3941
}
4042
]
4143
```

pkg/alizer/alizer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,12 @@ func (o *Alizer) DetectPorts(path string) ([]int, error) {
138138
return components[0].Ports, nil
139139
}
140140

141-
func NewDetectionResult(typ model.DevFileType, registry api.Registry, appPorts []int, devfileVersion string) *api.DetectionResult {
141+
func NewDetectionResult(typ model.DevFileType, registry api.Registry, appPorts []int, devfileVersion, name string) *api.DetectionResult {
142142
return &api.DetectionResult{
143143
Devfile: typ.Name,
144144
DevfileRegistry: registry.Name,
145145
ApplicationPorts: appPorts,
146146
DevfileVersion: devfileVersion,
147+
Name: name,
147148
}
148149
}

pkg/api/analyze.go

+2
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ type DetectionResult struct {
1717
// ApplicationPorts represents the list of ports detected
1818
ApplicationPorts []int `json:"ports,omitempty"`
1919
DevfileVersion string `json:"devfileVersion,omitempty"`
20+
// Name represents the project/application name as detected by alizer
21+
Name string `json:"name,omitempty"`
2022
}

pkg/init/backend/alizer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (o *AlizerBackend) SelectDevfile(ctx context.Context, flags map[string]stri
6363
if !confirm {
6464
return nil, nil
6565
}
66-
return alizer.NewDetectionResult(selected, registry, appPorts, defaultVersion), nil
66+
return alizer.NewDetectionResult(selected, registry, appPorts, defaultVersion, ""), nil
6767
}
6868

6969
func (o *AlizerBackend) SelectStarterProject(devfile parser.DevfileObj, flags map[string]string) (starter *v1alpha2.StarterProject, err error) {

pkg/odo/cli/alizer/alizer.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ func (o *AlizerOptions) RunForJsonOutput(ctx context.Context) (out interface{},
5858
if err != nil {
5959
return nil, err
6060
}
61-
result := alizer.NewDetectionResult(df, reg, appPorts, defaultVersion)
61+
name, err := o.clientset.AlizerClient.DetectName(workingDir)
62+
if err != nil {
63+
return nil, err
64+
}
65+
result := alizer.NewDetectionResult(df, reg, appPorts, defaultVersion, name)
6266
return []api.DetectionResult{*result}, nil
6367
}
6468

tests/helper/helper_generic.go

+6
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ func JsonPathDoesNotExist(json string, path string) {
319319
fmt.Sprintf("content should not contain %q but is %q", path, result.String()))
320320
}
321321

322+
// JsonPathExist expects that the content of the path exists in the JSON string
323+
func JsonPathExist(json string, path string) {
324+
result := gjson.Get(json, path)
325+
Expect(result.Exists()).To(BeTrue(),
326+
fmt.Sprintf("content should contain %q", path))
327+
}
322328
func JsonPathContentIsValidUserPort(json string, path string) {
323329
result := gjson.Get(json, path)
324330
intVal, err := strconv.Atoi(result.String())

tests/integration/cmd_analyze_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var _ = Describe("odo analyze command tests", func() {
3030

3131
When("source files are in the directory", func() {
3232
BeforeEach(func() {
33-
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context)
33+
helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context)
3434
})
3535

3636
It("analyze should return correct value", func() {
@@ -39,7 +39,9 @@ var _ = Describe("odo analyze command tests", func() {
3939
Expect(stderr).To(BeEmpty())
4040
Expect(helper.IsJSON(stdout)).To(BeTrue())
4141
helper.JsonPathContentIs(stdout, "0.devfile", "nodejs")
42-
helper.JsonPathContentIs(stdout, "0.devfileRegistry", "DefaultDevfileRegistry")
42+
helper.JsonPathExist(stdout, "0.devfileRegistry")
43+
helper.JsonPathContentIs(stdout, "0.name", "node-echo")
44+
helper.JsonPathExist(stdout, "0.ports")
4345
})
4446
})
4547

0 commit comments

Comments
 (0)