Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ Aliases:

Flags:
-c, --cluster string set the cluster
--decode-output decode the last base64-encoded line in the response and ignore logs
-e, --endpoint string endpoint of a non registered cluster
-f, --file-input string input file for the request
-h, --help help for run
Expand Down
99 changes: 82 additions & 17 deletions cmd/service_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io"
"io/ioutil"
"os"
"strings"

"github.com/grycap/oscar-cli/v2/pkg/config"
"github.com/grycap/oscar-cli/v2/pkg/service"
Expand Down Expand Up @@ -58,6 +59,7 @@ func serviceRunFunc(cmd *cobra.Command, args []string) error {
inputFile, _ := cmd.Flags().GetString("file-input")
textInput, _ := cmd.Flags().GetString("text-input")
outputFile, _ := cmd.Flags().GetString("output")
decodeOutput, _ := cmd.Flags().GetBool("decode-output")
if inputFile == "" && textInput == "" {
return errors.New("you must specify \"--file-input\" or \"--text-input\" flag")
}
Expand Down Expand Up @@ -110,28 +112,28 @@ func serviceRunFunc(cmd *cobra.Command, args []string) error {
return errors.New("unable to copy the response")
}

// Decode the result body
tmpfile.Seek(0, 0)
decoder := base64.NewDecoder(base64.StdEncoding, tmpfile)

// Parse output (store file if --output is set)
var out *os.File

if outputFile != "" {
// Create the file if --output is set
out, err = os.Create(outputFile)
if decodeOutput {
tmpfile.Seek(0, 0)
response, err := io.ReadAll(tmpfile)
if err != nil {
return fmt.Errorf("unable to create the file \"%s\"", outputFile)
return errors.New("unable to read the response")
}
} else {
// Create a temporary file
out, err = ioutil.TempFile("", "")
decoded, err := decodeLastBase64Line(response)
if err != nil {
return errors.New("unable to create a temporary file to decode the result")
return err
}
defer os.Remove(out.Name())
return writeServiceRunOutput(outputFile, bytes.NewReader(decoded))
}

// Decode the result body
tmpfile.Seek(0, 0)
decoder := base64.NewDecoder(base64.StdEncoding, tmpfile)

out, err := createServiceRunOutput(outputFile)
if err != nil {
return err
}
defer out.Close()
defer closeServiceRunOutput(out, outputFile)

// Copy the decoder stream into out
_, err = io.Copy(out, decoder)
Expand All @@ -158,6 +160,68 @@ func serviceRunFunc(cmd *cobra.Command, args []string) error {
return nil
}

func decodeLastBase64Line(response []byte) ([]byte, error) {
lines := strings.Split(string(response), "\n")
decoder := base64.StdEncoding.Strict()

for i := len(lines) - 1; i >= 0; i-- {
line := strings.TrimSpace(lines[i])
if line == "" {
continue
}
decoded, err := decoder.DecodeString(line)
if err == nil {
return decoded, nil
}
}

return nil, errors.New("unable to find base64-encoded output in the response")
}

func writeServiceRunOutput(outputFile string, input io.Reader) error {
out, err := createServiceRunOutput(outputFile)
if err != nil {
return err
}
defer closeServiceRunOutput(out, outputFile)

if _, err := io.Copy(out, input); err != nil {
return errors.New("unable to copy the response")
}

if outputFile == "" {
out.Seek(0, 0)
if _, err := io.Copy(os.Stdout, out); err != nil {
return errors.New("unable to print the result")
}
}

return nil
}

func createServiceRunOutput(outputFile string) (*os.File, error) {
if outputFile != "" {
out, err := os.Create(outputFile)
if err != nil {
return nil, fmt.Errorf("unable to create the file \"%s\"", outputFile)
}
return out, nil
}

out, err := ioutil.TempFile("", "")
if err != nil {
return nil, errors.New("unable to create a temporary file to decode the result")
}
return out, nil
}

func closeServiceRunOutput(out *os.File, outputFile string) {
if outputFile == "" {
os.Remove(out.Name())
}
out.Close()
}

func makeServiceRunCmd() *cobra.Command {
serviceRunCmd := &cobra.Command{
Use: "run SERVICE_NAME {--file-input | --text-input}",
Expand All @@ -173,6 +237,7 @@ func makeServiceRunCmd() *cobra.Command {
serviceRunCmd.Flags().StringP("file-input", "f", "", "input file for the request")
serviceRunCmd.Flags().StringP("text-input", "i", "", "text input string for the request")
serviceRunCmd.Flags().StringP("output", "o", "", "file path to store the output")
serviceRunCmd.Flags().Bool("decode-output", false, "decode the last base64-encoded line in the response and ignore logs")

return serviceRunCmd
}
114 changes: 114 additions & 0 deletions cmd/service_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,120 @@ func TestServiceRunCommandFileInput(t *testing.T) {
}
}

func TestServiceRunCommandDecodeOutputIgnoresLogs(t *testing.T) {
const (
clusterName = "run-decode-cluster"
serviceName = "decoder"
serviceToken = "decode-token"
payload = "ping"
expected = "decoded result\nwith multiple lines\n"
)

response := strings.Join([]string{
"2026-05-28 06:38:44,746 - supervisor - INFO - Reading storage configuration",
"2026-05-28 06:38:55,157 - supervisor - INFO - Creating response",
base64.StdEncoding.EncodeToString([]byte(expected)),
"",
}, "\n")

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && r.URL.Path == "/system/services/"+serviceName:
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(&types.Service{
Name: serviceName,
Token: serviceToken,
}); err != nil {
t.Fatalf("encoding service response: %v", err)
}
case r.Method == http.MethodPost && r.URL.Path == "/run/"+serviceName:
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, response)
default:
http.NotFound(w, r)
}
}))
defer server.Close()

configFile := writeConfigFile(t, clusterName, server.URL)
outputFile := filepath.Join(t.TempDir(), "result.txt")

stdout, stderr, err := runCommand(t,
"service", "--config", configFile,
"run", serviceName,
"--cluster", clusterName,
"--text-input", payload,
"--output", outputFile,
"--decode-output",
)
if err != nil {
t.Fatalf("service run command returned error: %v", err)
}
if stdout != "" {
t.Fatalf("expected empty stdout when output file is set, got %q", stdout)
}
if stderr != "" {
t.Fatalf("expected empty stderr, got %q", stderr)
}

content, err := os.ReadFile(outputFile)
if err != nil {
t.Fatalf("reading output file: %v", err)
}
if string(content) != expected {
t.Fatalf("expected decoded output %q, got %q", expected, content)
}
}

func TestServiceRunCommandWithoutDecodeOutputKeepsRawResponseWithLogs(t *testing.T) {
const (
clusterName = "run-raw-cluster"
serviceName = "raw"
serviceToken = "raw-token"
payload = "ping"
expected = "decoded result"
)

response := "log line\n" + base64.StdEncoding.EncodeToString([]byte(expected)) + "\n"

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && r.URL.Path == "/system/services/"+serviceName:
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(&types.Service{
Name: serviceName,
Token: serviceToken,
}); err != nil {
t.Fatalf("encoding service response: %v", err)
}
case r.Method == http.MethodPost && r.URL.Path == "/run/"+serviceName:
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, response)
default:
http.NotFound(w, r)
}
}))
defer server.Close()

configFile := writeConfigFile(t, clusterName, server.URL)

stdout, stderr, err := runCommand(t,
"service", "--config", configFile,
"run", serviceName,
"--cluster", clusterName,
"--text-input", payload,
)
if err != nil {
t.Fatalf("service run command returned error: %v", err)
}
if stderr != "" {
t.Fatalf("expected empty stderr, got %q", stderr)
}
if stdout != response {
t.Fatalf("expected raw response %q, got %q", response, stdout)
}
}

func TestServiceRunCommandInputValidation(t *testing.T) {
const clusterName = "run-validate-cluster"

Expand Down
45 changes: 22 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/schollz/progressbar/v3 v3.13.1
github.com/spf13/cobra v1.10.1
golang.org/x/net v0.48.0 // indirect
golang.org/x/sys v0.39.0 // indirect
golang.org/x/text v0.32.0 // indirect
k8s.io/api v0.34.2 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
github.com/spf13/cobra v1.10.2
golang.org/x/net v0.51.0 // indirect
golang.org/x/sys v0.42.0 // indirect
golang.org/x/text v0.34.0 // indirect
k8s.io/api v0.35.3 // indirect
k8s.io/klog/v2 v2.140.0 // indirect
)

require (
github.com/gdamore/tcell/v2 v2.8.1
github.com/golang-jwt/jwt/v5 v5.2.2
github.com/grycap/oscar/v4 v4.0.0
github.com/golang-jwt/jwt/v5 v5.3.0
github.com/grycap/oscar/v4 v4.1.0
github.com/indigo-dc/liboidcagent-go v0.3.0
github.com/rivo/tview v0.42.0
golang.org/x/term v0.38.0
golang.org/x/term v0.40.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -35,8 +35,8 @@ require (
github.com/emicklei/go-restful/v3 v3.13.0 // indirect
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
github.com/gdamore/encoding v1.0.1 // indirect
github.com/go-openapi/jsonpointer v0.22.3 // indirect
github.com/go-openapi/jsonreference v0.21.3 // indirect
github.com/go-openapi/jsonpointer v0.22.4 // indirect
github.com/go-openapi/jsonreference v0.21.4 // indirect
github.com/go-openapi/swag v0.25.4 // indirect
github.com/go-openapi/swag/cmdutils v0.25.4 // indirect
github.com/go-openapi/swag/conv v0.25.4 // indirect
Expand All @@ -49,7 +49,6 @@ require (
github.com/go-openapi/swag/stringutils v0.25.4 // indirect
github.com/go-openapi/swag/typeutils v0.25.4 // indirect
github.com/go-openapi/swag/yamlutils v0.25.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/gnostic-models v0.7.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grycap/cdmi-client-go v0.1.1 // indirect
Expand All @@ -65,25 +64,25 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/stretchr/testify v1.11.1 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.46.0 // indirect
golang.org/x/oauth2 v0.34.0 // indirect
golang.org/x/crypto v0.48.0 // indirect
golang.org/x/oauth2 v0.35.0 // indirect
golang.org/x/time v0.14.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/protobuf v1.36.10 // indirect
google.golang.org/protobuf v1.36.11 // indirect
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
k8s.io/apimachinery v0.34.2 // indirect
k8s.io/client-go v0.34.2 // indirect
k8s.io/kube-openapi v0.0.0-20251125145642-4e65d59e963e // indirect
k8s.io/utils v0.0.0-20251002143259-bc988d571ff4 // indirect
sigs.k8s.io/controller-runtime v0.22.4 // indirect
k8s.io/apimachinery v0.35.3 // indirect
k8s.io/client-go v0.35.3 // indirect
k8s.io/component-helpers v0.35.3 // indirect
k8s.io/kube-openapi v0.0.0-20260127142750-a19766b6e2d4 // indirect
k8s.io/utils v0.0.0-20260210185600-b8788abfbbc2 // indirect
sigs.k8s.io/controller-runtime v0.23.3 // indirect
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 // indirect
sigs.k8s.io/kueue v0.15.0 // indirect
sigs.k8s.io/kueue v0.17.2 // indirect
sigs.k8s.io/randfill v1.0.0 // indirect
sigs.k8s.io/structured-merge-diff/v6 v6.3.1 // indirect
sigs.k8s.io/structured-merge-diff/v6 v6.3.2 // indirect
sigs.k8s.io/yaml v1.6.0 // indirect
)
Loading
Loading