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 capability to run to support -f flag #1161

Merged
merged 18 commits into from
Jan 20, 2023
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
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 {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will later be removed, once the normal dapr run also migrates to using the RunExec 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) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly same as pkg/standalone/run.go#Run function. moved here and renamed.

// 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