forked from perses/perses
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement get and describe cmd (perses#310)
* implement get cmd Signed-off-by: Augustin Husson <husson.augustin@gmail.com> * implement describe command Signed-off-by: Augustin Husson <husson.augustin@gmail.com> * apply code review Signed-off-by: Augustin Husson <husson.augustin@gmail.com>
- Loading branch information
Showing
16 changed files
with
794 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2022 The Perses Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package describe | ||
|
||
import ( | ||
"fmt" | ||
|
||
cmdUtils "github.com/perses/perses/internal/cli/utils" | ||
cmdUtilsService "github.com/perses/perses/internal/cli/utils/service" | ||
modelV1 "github.com/perses/perses/pkg/model/api/v1" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type option struct { | ||
kind modelV1.Kind | ||
project string | ||
name string | ||
output string | ||
resourceService cmdUtilsService.Service | ||
} | ||
|
||
func (o *option) complete(args []string) error { | ||
if len(args) < 1 { | ||
return fmt.Errorf(cmdUtils.FormatAvailableResourcesMessage()) | ||
} else if len(args) < 2 { | ||
return fmt.Errorf("please specify the name of the resource you want to describe") | ||
} else if len(args) > 2 { | ||
return fmt.Errorf("you cannot have more than two arguments for the command 'describe'") | ||
} | ||
o.name = args[1] | ||
|
||
var err error | ||
o.kind, err = cmdUtils.GetKind(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Then, if no particular project has been specified through a flag, let's grab the one defined in the CLI config. | ||
if len(o.project) == 0 { | ||
o.project = cmdUtils.GlobalConfig.Project | ||
} | ||
|
||
// Finally, get the api client we will need later. | ||
apiClient, err := cmdUtils.GlobalConfig.GetAPIClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
svc, svcErr := cmdUtilsService.NewService(o.kind, o.project, apiClient) | ||
if svcErr != nil { | ||
return err | ||
} | ||
o.resourceService = svc | ||
return nil | ||
} | ||
|
||
func (o *option) validate() error { | ||
return cmdUtils.ValidateAndSetOutput(&o.output) | ||
} | ||
|
||
func (o *option) execute() error { | ||
entity, err := o.resourceService.GetResource(o.name) | ||
if err != nil { | ||
return err | ||
} | ||
return cmdUtils.HandleOutput(o.output, entity) | ||
} | ||
|
||
func NewCMD() *cobra.Command { | ||
o := &option{} | ||
cmd := &cobra.Command{ | ||
Use: "describe [RESOURCE_TYPE] [NAME]", | ||
Short: "Show details of a specific resource", | ||
Example: ` | ||
## Describe a particular dashboard. | ||
percli describe dashboard nodeExporter | ||
## Describe a particular dashboard as a JSON object. | ||
percli describe dashboard nodeExporter -ojson | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmdUtils.HandleError(o.complete(args)) | ||
cmdUtils.HandleError(o.validate()) | ||
cmdUtils.HandleError(o.execute()) | ||
}, | ||
} | ||
cmd.Flags().StringVarP(&o.project, "project", "p", o.project, "If present, the project scope for this CLI request.") | ||
cmd.Flags().StringVarP(&o.output, "output", "o", o.output, "One of 'yaml' or 'json'. Default is 'yaml'.") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2022 The Perses Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package get | ||
|
||
import ( | ||
"fmt" | ||
|
||
cmdUtils "github.com/perses/perses/internal/cli/utils" | ||
cmdUtilsService "github.com/perses/perses/internal/cli/utils/service" | ||
modelV1 "github.com/perses/perses/pkg/model/api/v1" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type option struct { | ||
kind modelV1.Kind | ||
allProject bool | ||
project string | ||
output string | ||
prefix string | ||
resourceService cmdUtilsService.Service | ||
} | ||
|
||
func (o *option) complete(args []string) error { | ||
// first let's analyze the args to get what kind of resource we should get and if there is a prefix to use for the filtering. | ||
if len(args) == 0 { | ||
return fmt.Errorf(cmdUtils.FormatAvailableResourcesMessage()) | ||
} else if len(args) == 2 { | ||
// In second position in the arguments, you can have a prefix that will be used to filter the resources. | ||
o.prefix = args[1] | ||
} else if len(args) > 2 { | ||
return fmt.Errorf("you cannot have more than two arguments for the command 'get'") | ||
} | ||
|
||
var err error | ||
o.kind, err = cmdUtils.GetKind(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Then, if no particular project has been specified through a flag, let's grab the one defined in the CLI config. | ||
if len(o.project) == 0 && !o.allProject { | ||
o.project = cmdUtils.GlobalConfig.Project | ||
} | ||
|
||
// Finally, get the api client we will need later. | ||
apiClient, err := cmdUtils.GlobalConfig.GetAPIClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
svc, svcErr := cmdUtilsService.NewService(o.kind, o.project, apiClient) | ||
if svcErr != nil { | ||
return err | ||
} | ||
o.resourceService = svc | ||
return nil | ||
} | ||
|
||
func (o *option) validate() error { | ||
// check if project should be defined (through the config or through the flag) for the given resource. | ||
if !o.allProject && len(o.project) == 0 && !cmdUtils.IsGlobalResource(o.kind) { | ||
return fmt.Errorf("no project has been defined for the scope of this command. If you intended to get all resources across projects, please use the flag --all") | ||
} | ||
if len(o.output) > 0 { | ||
// In this particular command, the default display is a matrix. | ||
return cmdUtils.ValidateAndSetOutput(&o.output) | ||
} | ||
return nil | ||
} | ||
|
||
func (o *option) execute() error { | ||
resourceList, err := o.resourceService.ListResource(o.prefix) | ||
if err != nil { | ||
return err | ||
} | ||
if len(o.output) > 0 { | ||
return cmdUtils.HandleOutput(o.output, resourceList) | ||
} | ||
entities, err := cmdUtils.ConvertToEntity(resourceList) | ||
if err != nil { | ||
return err | ||
} | ||
data := o.resourceService.BuildMatrix(entities) | ||
cmdUtils.HandlerTable(o.resourceService.GetColumHeader(), data) | ||
return nil | ||
} | ||
|
||
func NewCMD() *cobra.Command { | ||
o := &option{} | ||
cmd := &cobra.Command{ | ||
Use: "get [RESOURCE_TYPE] [PREFIX]", | ||
Short: "Retrieve any kind of resource from the API.", | ||
Example: ` | ||
# List all dashboards in the current project selected. | ||
percli get dashboards | ||
# List all dashboards that begin with a given name in the current project selected. | ||
percli get dashboards node | ||
# List all dashboards in a specific project. | ||
percli get dashboards -p my_project | ||
#List all dashboards as a JSON object. | ||
percli get dashboards -a -ojson | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmdUtils.HandleError(o.complete(args)) | ||
cmdUtils.HandleError(o.validate()) | ||
cmdUtils.HandleError(o.execute()) | ||
}, | ||
} | ||
cmd.Flags().BoolVarP(&o.allProject, "all", "a", o.allProject, "If present, list the requested object(s) across all projects. The project in the current context is ignored even if specified with --project.") | ||
cmd.Flags().StringVarP(&o.project, "project", "p", o.project, "If present, the project scope for this CLI request.") | ||
cmd.Flags().StringVarP(&o.output, "output", "o", o.output, "Kind of display: 'yaml' or 'json'.") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.