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

add Get command to Kanx #3313

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions pkg/kando/process_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func newProcessClientCommand() *cobra.Command {
Short: "Send commands to the process server",
}
cmd.AddCommand(newProcessClientCreateCommand())
cmd.AddCommand(newProcessClientGetCommand())
cmd.AddCommand(newProcessClientListCommand())
cmd.AddCommand(newProcessClientOutputCommand())
cmd.PersistentFlags().BoolP(processAsJSONFlagName, "j", false, "Display output as json")
Expand Down
64 changes: 64 additions & 0 deletions pkg/kando/process_client_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2020 The Kanister 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 kando

import (
"os"

"context"
"fmt"
"github.com/kanisterio/kanister/pkg/kanx"
"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/protojson"
"strconv"
)

func newProcessClientGetCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: "get",
RunE: runProcessClientGet,
}
return cmd
}

func runProcessClientGet(cmd *cobra.Command, args []string) error {
cmd.SetContext(context.Background())
pid, err := strconv.Atoi(args[0])
if err != nil {
return err
}
addr, err := processAddressFlagValue(cmd)
if err != nil {
return err
}
asJSON := processAsJSONFlagValue(cmd)
cmd.SilenceUsage = true
p, err := kanx.GetProcess(cmd.Context(), addr, int64(pid))
if err != nil {
return err
}
out := os.Stdout
if asJSON {
buf, err := protojson.Marshal(p)
if err != nil {
return err
}
fmt.Fprintln(out, string(buf))
} else {
fmt.Fprintln(out, "Process: ", p.String())
}
return nil
}
22 changes: 19 additions & 3 deletions pkg/kanx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,23 @@ func CreateProcess(ctx context.Context, addr string, name string, args []string)
Name: name,
Args: args,
}
return c.CreateProcesses(ctx, in)
return c.CreateProcess(ctx, in)
}

func GetProcess(ctx context.Context, addr string, pid int64) (*Process, error) {
conn, err := newGRPCConnection(addr)
if err != nil {
return nil, err
}
defer conn.Close() //nolint:errcheck
c := NewProcessServiceClient(conn)
wpc, err := c.GetProcess(ctx, &ProcessPidRequest{
Pid: pid,
})
if err != nil {
return nil, err
}
return wpc, nil
}

func ListProcesses(ctx context.Context, addr string) ([]*Process, error) {
Expand Down Expand Up @@ -71,7 +87,7 @@ func Stdout(ctx context.Context, addr string, pid int64, out io.Writer) error {
}
defer conn.Close() //nolint:errcheck
c := NewProcessServiceClient(conn)
in := &ProcessOutputRequest{
in := &ProcessPidRequest{
Pid: pid,
}
sc, err := c.Stdout(ctx, in)
Expand All @@ -88,7 +104,7 @@ func Stderr(ctx context.Context, addr string, pid int64, out io.Writer) error {
}
defer conn.Close() //nolint:errcheck
c := NewProcessServiceClient(conn)
in := &ProcessOutputRequest{
in := &ProcessPidRequest{
Pid: pid,
}
sc, err := c.Stderr(ctx, in)
Expand Down
Loading
Loading