mirror of https://github.com/knative/client.git
65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
// Copyright © 2019 The Knative 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 service
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
"knative.dev/client/pkg/kn/commands"
|
|
knclient "knative.dev/client/pkg/serving/v1alpha1"
|
|
)
|
|
|
|
// Helper methods
|
|
var blankConfig clientcmd.ClientConfig
|
|
|
|
func init() {
|
|
var err error
|
|
blankConfig, err = clientcmd.NewClientConfigFromBytes([]byte(`kind: Config
|
|
version: v1
|
|
users:
|
|
- name: u
|
|
clusters:
|
|
- name: c
|
|
cluster:
|
|
server: example.com
|
|
contexts:
|
|
- name: x
|
|
context:
|
|
user: u
|
|
cluster: c
|
|
current-context: x
|
|
`))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func executeServiceCommand(client knclient.KnServingClient, args ...string) (string, error) {
|
|
knParams := &commands.KnParams{}
|
|
knParams.ClientConfig = blankConfig
|
|
|
|
output := new(bytes.Buffer)
|
|
knParams.Output = output
|
|
knParams.NewServingClient = func(namespace string) (knclient.KnServingClient, error) {
|
|
return client, nil
|
|
}
|
|
cmd := NewServiceCommand(knParams)
|
|
cmd.SetArgs(args)
|
|
cmd.SetOutput(output)
|
|
err := cmd.Execute()
|
|
return output.String(), err
|
|
}
|