Skip to content

Commit

Permalink
Add capability to run to support -f flag (#1161)
Browse files Browse the repository at this point in the history
* refactor run into functions

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* renamed Apps struct as App

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* redirect logs to files. set informational logs. set command dirs

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* fix linter errors

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* fix e2e test on non-existent resources-path

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* more fixes

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* fix merge conflict

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* set default ports for zero values

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* fix linter errors

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* add more details in error, fix logs in run -f

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* address review comments

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* address review comments

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

* add more fixes in template run based on e2e tests

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>

Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>
  • Loading branch information
mukundansundar authored Jan 20, 2023
1 parent b51eda4 commit 36893d9
Show file tree
Hide file tree
Showing 9 changed files with 765 additions and 99 deletions.
467 changes: 464 additions & 3 deletions cmd/run.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkg/kubernetes/configurations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
v1alpha1 "github.com/dapr/dapr/pkg/apis/configuration/v1alpha1"
)

//nolint:dupword
func TestConfigurations(t *testing.T) {
now := meta_v1.Now()
formattedNow := now.Format("2006-01-02 15:04.05")
Expand Down
49 changes: 43 additions & 6 deletions pkg/print/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"encoding/json"
"fmt"
"io"
"os"
"runtime"
"sync"
"time"
Expand All @@ -29,6 +30,16 @@ const (
windowsOS = "windows"
)

type logStatus string

const (
LogSuccess logStatus = "success"
LogFailure logStatus = "failure"
LogWarning logStatus = "warning"
LogInfo logStatus = "info"
LogPending logStatus = "pending"
)

type Result bool

const (
Expand Down Expand Up @@ -56,10 +67,36 @@ func IsJSONLogEnabled() bool {
return logAsJSON
}

// StatusEvent reports a event log with given status.
func StatusEvent(w io.Writer, status logStatus, fmtstr string, a ...any) {
if logAsJSON {
logJSON(w, string(status), fmt.Sprintf(fmtstr, a...))
return
}
if (w != os.Stdout && w != os.Stderr) || runtime.GOOS == windowsOS {
fmt.Fprintf(w, "%s\n", fmt.Sprintf(fmtstr, a...))
return
}
switch status {
case LogSuccess:
fmt.Fprintf(w, "✅ %s\n", fmt.Sprintf(fmtstr, a...))
case LogFailure:
fmt.Fprintf(w, "❌ %s\n", fmt.Sprintf(fmtstr, a...))
case LogWarning:
fmt.Fprintf(w, "⚠ %s\n", fmt.Sprintf(fmtstr, a...))
case LogPending:
fmt.Fprintf(w, "⌛ %s\n", fmt.Sprintf(fmtstr, a...))
case LogInfo:
fmt.Fprintf(w, "ℹ️ %s\n", fmt.Sprintf(fmtstr, a...))
default:
fmt.Fprintf(w, "%s\n", fmt.Sprintf(fmtstr, a...))
}
}

// SuccessStatusEvent reports on a success event.
func SuccessStatusEvent(w io.Writer, fmtstr string, a ...interface{}) {
if logAsJSON {
logJSON(w, "success", fmt.Sprintf(fmtstr, a...))
logJSON(w, string(LogSuccess), fmt.Sprintf(fmtstr, a...))
} else if runtime.GOOS == windowsOS {
fmt.Fprintf(w, "%s\n", fmt.Sprintf(fmtstr, a...))
} else {
Expand All @@ -70,7 +107,7 @@ func SuccessStatusEvent(w io.Writer, fmtstr string, a ...interface{}) {
// FailureStatusEvent reports on a failure event.
func FailureStatusEvent(w io.Writer, fmtstr string, a ...interface{}) {
if logAsJSON {
logJSON(w, "failure", fmt.Sprintf(fmtstr, a...))
logJSON(w, string(LogFailure), fmt.Sprintf(fmtstr, a...))
} else if runtime.GOOS == windowsOS {
fmt.Fprintf(w, "%s\n", fmt.Sprintf(fmtstr, a...))
} else {
Expand All @@ -81,7 +118,7 @@ func FailureStatusEvent(w io.Writer, fmtstr string, a ...interface{}) {
// WarningStatusEvent reports on a failure event.
func WarningStatusEvent(w io.Writer, fmtstr string, a ...interface{}) {
if logAsJSON {
logJSON(w, "warning", fmt.Sprintf(fmtstr, a...))
logJSON(w, string(LogWarning), fmt.Sprintf(fmtstr, a...))
} else if runtime.GOOS == windowsOS {
fmt.Fprintf(w, "%s\n", fmt.Sprintf(fmtstr, a...))
} else {
Expand All @@ -92,7 +129,7 @@ func WarningStatusEvent(w io.Writer, fmtstr string, a ...interface{}) {
// PendingStatusEvent reports on a pending event.
func PendingStatusEvent(w io.Writer, fmtstr string, a ...interface{}) {
if logAsJSON {
logJSON(w, "pending", fmt.Sprintf(fmtstr, a...))
logJSON(w, string(LogPending), fmt.Sprintf(fmtstr, a...))
} else if runtime.GOOS == windowsOS {
fmt.Fprintf(w, "%s\n", fmt.Sprintf(fmtstr, a...))
} else {
Expand All @@ -103,7 +140,7 @@ func PendingStatusEvent(w io.Writer, fmtstr string, a ...interface{}) {
// InfoStatusEvent reports status information on an event.
func InfoStatusEvent(w io.Writer, fmtstr string, a ...interface{}) {
if logAsJSON {
logJSON(w, "info", fmt.Sprintf(fmtstr, a...))
logJSON(w, string(LogInfo), fmt.Sprintf(fmtstr, a...))
} else if runtime.GOOS == windowsOS {
fmt.Fprintf(w, "%s\n", fmt.Sprintf(fmtstr, a...))
} else {
Expand All @@ -117,7 +154,7 @@ func Spinner(w io.Writer, fmtstr string, a ...interface{}) func(result Result) {
var s *spinner.Spinner

if logAsJSON {
logJSON(w, "pending", msg)
logJSON(w, string(LogPending), msg)
} else if runtime.GOOS == windowsOS {
fmt.Fprintf(w, "%s\n", msg)

Expand Down
131 changes: 131 additions & 0 deletions pkg/runexec/runexec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
Copyright 2023 The Dapr 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 runexec

import (
"fmt"
"io"
"os/exec"

"github.com/dapr/cli/pkg/standalone"
)

type CmdProcess struct {
Command *exec.Cmd
CommandErr error
OutputWriter io.Writer
ErrorWriter io.Writer
}

type RunExec struct {
DaprCMD *CmdProcess
AppCMD *CmdProcess
AppID string
DaprHTTPPort int
DaprGRPCPort int
DaprMetricPort int
}

// RunOutput represents the run execution.
type RunOutput struct {
DaprCMD *exec.Cmd
DaprErr error
DaprHTTPPort int
DaprGRPCPort int
AppID string
AppCMD *exec.Cmd
AppErr error
}

func New(config *standalone.RunConfig, daprCmdProcess *CmdProcess, appCmdProcess *CmdProcess) *RunExec {
return &RunExec{
DaprCMD: daprCmdProcess,
AppCMD: appCmdProcess,
AppID: config.AppID,
DaprHTTPPort: config.HTTPPort,
DaprGRPCPort: config.GRPCPort,
DaprMetricPort: config.MetricsPort,
}
}

func GetDaprCmdProcess(config *standalone.RunConfig) (*CmdProcess, error) {
daprCMD, err := standalone.GetDaprCommand(config)
if err != nil {
return nil, err
}
return &CmdProcess{
Command: daprCMD,
}, nil
}

func GetAppCmdProcess(config *standalone.RunConfig) (*CmdProcess, error) {
//nolint
var appCMD *exec.Cmd = standalone.GetAppCommand(config)
return &CmdProcess{
Command: appCMD,
}, nil
}

func (c *CmdProcess) WithOutputWriter(w io.Writer) {
c.OutputWriter = w
}

// SetStdout should be called after WithOutputWriter.
func (c *CmdProcess) SetStdout() error {
if c.Command == nil {
return fmt.Errorf("command is nil")
}
c.Command.Stdout = c.OutputWriter
return nil
}

func (c *CmdProcess) WithErrorWriter(w io.Writer) {
c.ErrorWriter = w
}

// SetStdErr should be called after WithErrorWriter.
func (c *CmdProcess) SetStderr() error {
if c.Command == nil {
return fmt.Errorf("command is nil")
}
c.Command.Stderr = c.ErrorWriter
return nil
}

func NewOutput(config *standalone.RunConfig) (*RunOutput, error) {
// set default values from RunConfig struct's tag.
config.SetDefaultFromSchema()
//nolint
err := config.Validate()
if err != nil {
return nil, err
}

daprCMD, err := standalone.GetDaprCommand(config)
if err != nil {
return nil, err
}

//nolint
var appCMD *exec.Cmd = standalone.GetAppCommand(config)
return &RunOutput{
DaprCMD: daprCMD,
DaprErr: nil,
AppCMD: appCMD,
AppErr: nil,
AppID: config.AppID,
DaprHTTPPort: config.HTTPPort,
DaprGRPCPort: config.GRPCPort,
}, nil
}
Loading

0 comments on commit 36893d9

Please sign in to comment.