Skip to content

Commit

Permalink
Write APIs CLI commands (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitarPetrov authored Mar 19, 2020
1 parent 1099c82 commit 8fe7688
Show file tree
Hide file tree
Showing 73 changed files with 4,743 additions and 1,712 deletions.
2 changes: 1 addition & 1 deletion .gometalinter.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"Enable": ["deadcode", "golint", "vet", "gotype", "gocyclo", "maligned", "errcheck", "misspell", "staticcheck"],
"Enable": ["deadcode", "golint", "vet", "gotype", "gocyclo", "errcheck", "misspell", "staticcheck"],
"Exclude": ["test", "fake_*"],
"Format": "[{{.Severity}}]: {{.Path}}: {{.Line}}:{{if .Col}}{{.Col}}{{end}}: {{.Message}} ({{.Linter}})",
"Deadline": "120s",
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ before_script:

script:
# Execute static checks
- gometalinter --vendor ./...
- gometalinter --cyclo-over=13 --vendor ./...

# Execute tests and generate coverage for all the packages except fakes and tests
- go test ./... -coverpkg $(go list ./... | egrep -v "fakes|test" | paste -sd "," -) -coverprofile=profile.cov
Expand Down
48 changes: 24 additions & 24 deletions Gopkg.lock

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

118 changes: 118 additions & 0 deletions internal/cmd/binding/bind.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2018 The Service Manager 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 binding

import (
"github.com/Peripli/service-manager-cli/internal/cmd"
"github.com/Peripli/service-manager-cli/internal/output"
"github.com/Peripli/service-manager-cli/pkg/query"
"github.com/Peripli/service-manager-cli/pkg/types"

"fmt"
"github.com/spf13/cobra"
)

// BindCmd wraps the smctl bind command
type BindCmd struct {
*cmd.Context

binding types.ServiceBinding
instanceName string

outputFormat output.Format
}

// NewBindCmd returns new bind command with context
func NewBindCmd(context *cmd.Context) *BindCmd {
return &BindCmd{Context: context, binding: types.ServiceBinding{}}
}

// Prepare returns cobra command
func (bc *BindCmd) Prepare(prepare cmd.PrepareFunc) *cobra.Command {
result := &cobra.Command{
Use: "bind [instance-name] [binding-name]",
Short: "Creates binding in SM",
Long: `Creates binding in SM`,

PreRunE: prepare(bc, bc.Context),
RunE: cmd.RunE(bc),
}

result.Flags().StringVarP(&bc.binding.ServiceInstanceID, "id", "", "", "ID of the service instance. Required when name is ambiguous")
cmd.AddFormatFlag(result.Flags())
cmd.AddCommonQueryFlag(result.Flags(), &bc.Parameters)
cmd.AddModeFlag(result.Flags(), "async")

return result
}

// Validate validates command's arguments
func (bc *BindCmd) Validate(args []string) error {
if len(args) < 2 {
return fmt.Errorf("instance and binding names are required")
}

bc.instanceName = args[0]
bc.binding.Name = args[1]
return nil
}

// Run runs the command's logic
func (bc *BindCmd) Run() error {
if bc.binding.ServiceInstanceID == "" {
instanceToBind, err := bc.Client.ListInstances(&query.Parameters{
FieldQuery: []string{
fmt.Sprintf("name eq '%s'", bc.instanceName),
},
})
if err != nil {
return err
}
if len(instanceToBind.ServiceInstances) < 1 {
return fmt.Errorf("service instance with name %s not found", bc.instanceName)
}
if len(instanceToBind.ServiceInstances) > 1 {
return fmt.Errorf("more than one service instance with name %s found. Use --id flag to specify id of the instance to bind", bc.instanceName)
}
bc.binding.ServiceInstanceID = instanceToBind.ServiceInstances[0].ID
}

resultBinding, location, err := bc.Client.Bind(&bc.binding, &bc.Parameters)
if err != nil {
return err
}

if len(location) != 0 {
cmd.CommonHandleAsyncExecution(bc.Context, location, fmt.Sprintf("Service Binding %s successfully scheduled. To see status of the operation use:\n", bc.binding.Name))
return nil
}

resultBinding.ServiceInstanceName = bc.instanceName
output.PrintServiceManagerObject(bc.Output, bc.outputFormat, resultBinding)
output.Println(bc.Output)
return nil
}

// SetOutputFormat set output format
func (bc *BindCmd) SetOutputFormat(format output.Format) {
bc.outputFormat = format
}

// HideUsage hide command's usage
func (bc *BindCmd) HideUsage() bool {
return true
}
Loading

0 comments on commit 8fe7688

Please sign in to comment.