Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
File renamed without changes.
39 changes: 39 additions & 0 deletions cmd/helpers/resolve/output_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package resolve

import (
"github.com/Smartling/smartling-cli/output"
clierror "github.com/Smartling/smartling-cli/services/helpers/cli_error"

"github.com/spf13/cobra"
)

// OutputParams resolve OutputParams for subcommands
func OutputParams(cmd *cobra.Command, fileConfigMTFileFormat *string) (output.Params, error) {
const outputTemplateFlag = "format"
format, err := cmd.Parent().PersistentFlags().GetString("output")
if err != nil {
return output.Params{}, clierror.UIError{
Operation: "get output",
Err: err,
Description: "unable to get output param",
}
}
template := FallbackString(cmd.Flags().Lookup(outputTemplateFlag), StringParam{
FlagName: outputTemplateFlag,
Config: fileConfigMTFileFormat,
})

mode, err := cmd.Parent().PersistentFlags().GetString("output-mode")
if err != nil {
return output.Params{}, clierror.UIError{
Operation: "get output mode",
Err: err,
Description: "unable to get output mode param",
}
}
return output.Params{
Mode: mode,
Format: format,
Template: template,
}, nil
}
4 changes: 1 addition & 3 deletions cmd/init/cmd_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
"github.com/spf13/cobra"
)

var (
dryRun bool
)
var dryRun bool

// NewInitCmd creates a new command to initialize the Smartling CLI.
func NewInitCmd(srvInitializer SrvInitializer) *cobra.Command {
Expand Down
47 changes: 47 additions & 0 deletions cmd/jobs/cmd_jobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package jobs

import (
"fmt"
"slices"
"strings"

"github.com/spf13/cobra"
)

const (
outputFormatFlag = "output"
)

var (
outputFormat string
allowedOutputs = []string{
"json",
"simple",
}
joinedAllowedOutputs = strings.Join(allowedOutputs, ", ")
)

// NewJobsCmd returns new jobs command
func NewJobsCmd() *cobra.Command {
jobsCmd := &cobra.Command{
Use: "jobs",
Short: "Handles job subcommands.",
Long: `Handles job subcommands. Subcommands are a high-level abstraction layer over the underlying Job APIs.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if !slices.Contains(allowedOutputs, outputFormat) {
return fmt.Errorf("invalid output: %s (allowed: %s)", outputFormat, joinedAllowedOutputs)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 && cmd.Flags().NFlag() == 0 {
return cmd.Help()
}
return nil
},
}

jobsCmd.PersistentFlags().StringVar(&outputFormat, outputFormatFlag, "simple", "Output format: "+joinedAllowedOutputs)

return jobsCmd
}
88 changes: 88 additions & 0 deletions cmd/jobs/progress/cmd_progress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package progress

import (
"errors"
"fmt"
"strings"

rootcmd "github.com/Smartling/smartling-cli/cmd"
"github.com/Smartling/smartling-cli/cmd/helpers/resolve"
jobscmd "github.com/Smartling/smartling-cli/cmd/jobs"
"github.com/Smartling/smartling-cli/output"
clierror "github.com/Smartling/smartling-cli/services/helpers/cli_error"
srv "github.com/Smartling/smartling-cli/services/jobs"

"github.com/spf13/cobra"
)

const (
outputFormatFlag = "output"
)

var (
outputFormat string
allowedOutputs = []string{
"json",
"simple",
}
joinedAllowedOutputs = strings.Join(allowedOutputs, ", ")
)
Comment thread
az-smartling marked this conversation as resolved.
Outdated

// NewProgressCmd returns new progress command
func NewProgressCmd(initializer jobscmd.SrvInitializer) *cobra.Command {
progressCmd := &cobra.Command{
Use: "progress <translationJobUid|translationJobName>",
Short: "Get job progress by the translationJobUid or translationJobName.",
Long: `Get job progress by the translationJobUid or translationJobName.`,
Example: `
# Get job progress

smartling-cli jobs progress aabbccdd

`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return clierror.UIError{
Operation: "check args",
Err: errors.New("wrong argument quantity"),
Description: fmt.Sprintf("expected one argument, got: %d", len(args)),
}
}
var idOrName string
if len(args) == 1 {
idOrName = args[0]
}
Comment thread
az-smartling marked this conversation as resolved.
Outdated

ctx := cmd.Context()

cnf, err := rootcmd.Config()
if err != nil {
return err
}

accountUID, err := resolve.FallbackAccount(cmd.Root().PersistentFlags().Lookup("account"), cnf.AccountID)
if err != nil {
return err
}

params := srv.ProgressParams{
AccountUID: accountUID,
ProjectUID: cnf.ProjectID,
JobIDOrName: idOrName,
}
format, err := cmd.Parent().PersistentFlags().GetString("output")
if err != nil {
return err
}
outputParams := output.Params{Format: format}
if err != nil {
return err
}
Comment thread
az-smartling marked this conversation as resolved.
Outdated
return run(ctx, initializer, params, outputParams)
},
}

progressCmd.PersistentFlags().StringVar(&outputFormat, outputFormatFlag, "simple", "Output format: "+joinedAllowedOutputs)
Comment thread
az-smartling marked this conversation as resolved.
Outdated

return progressCmd
}
42 changes: 42 additions & 0 deletions cmd/jobs/progress/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package progress

import (
"context"

jobscmd "github.com/Smartling/smartling-cli/cmd/jobs"
"github.com/Smartling/smartling-cli/output"
"github.com/Smartling/smartling-cli/output/jobs"
clierror "github.com/Smartling/smartling-cli/services/helpers/cli_error"
"github.com/Smartling/smartling-cli/services/helpers/rlog"
srv "github.com/Smartling/smartling-cli/services/jobs"
)

func run(ctx context.Context,
initializer jobscmd.SrvInitializer,
params srv.ProgressParams,
outputParams output.Params,
) error {
rlog.Debugf("running progress with params: %v", params)
jobSrv, err := initializer.InitJobSrv()
if err != nil {
return clierror.UIError{
Operation: "init",
Err: err,
Description: "unable to initialize Jobs service",
}
}

progressOutput, err := jobSrv.RunProgress(ctx, params)
if err != nil {
return err
}

if progressOutput.TranslationJobUID == "" {
rlog.Infof("no jobs found for given translationJobUid or translationJobName: %s", params.JobIDOrName)
return nil
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is the exit code that will be returned in this case? I believe it should be non-zero.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed to return error

}

outputFormat := jobs.GetOutputFormat(outputParams.Format)
outputFormat.FormatAndRender(progressOutput)
return nil
}
31 changes: 31 additions & 0 deletions cmd/jobs/srv_initializer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package jobs

import (
rootcmd "github.com/Smartling/smartling-cli/cmd"
srv "github.com/Smartling/smartling-cli/services/jobs"

jobapi "github.com/Smartling/api-sdk-go/api/job"
)

// SrvInitializer defines files service initializer
type SrvInitializer interface {
InitJobSrv() (srv.Service, error)
}

// NewSrvInitializer returns new SrvInitializer implementation
func NewSrvInitializer() SrvInitializer {
return srvInitializer{}
}

type srvInitializer struct{}

// InitJobSrv initializes `job` service with the client and configuration.
func (i srvInitializer) InitJobSrv() (srv.Service, error) {
client, err := rootcmd.Client()
if err != nil {
return nil, err
}
jobApi := jobapi.NewJob(client.Client)
jobSrv := srv.NewService(jobApi)
return jobSrv, nil
}
3 changes: 2 additions & 1 deletion cmd/mt/detect/cmd_detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"

"github.com/Smartling/smartling-cli/cmd/helpers/resolve"
mtcmd "github.com/Smartling/smartling-cli/cmd/mt"
output "github.com/Smartling/smartling-cli/output/mt"
clierror "github.com/Smartling/smartling-cli/services/helpers/cli_error"
Expand Down Expand Up @@ -69,7 +70,7 @@ func NewDetectCmd(initializer mtcmd.SrvInitializer) *cobra.Command {
}
}

outputParams, err := mtcmd.ResolveOutputParams(cmd, fileConfig.MT.FileFormat)
outputParams, err := resolve.OutputParams(cmd, fileConfig.MT.FileFormat)
if err != nil {
return err
}
Expand Down
10 changes: 6 additions & 4 deletions cmd/mt/detect/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"time"

mtcmd "github.com/Smartling/smartling-cli/cmd/mt"
output "github.com/Smartling/smartling-cli/output/mt"
"github.com/Smartling/smartling-cli/output"
mtoutput "github.com/Smartling/smartling-cli/output/mt"
clierror "github.com/Smartling/smartling-cli/services/helpers/cli_error"
"github.com/Smartling/smartling-cli/services/helpers/rlog"
srv "github.com/Smartling/smartling-cli/services/mt"
Expand All @@ -17,7 +18,8 @@ import (
func run(ctx context.Context,
initializer mtcmd.SrvInitializer,
params srv.DetectParams,
outputParams output.OutputParams) error {
outputParams output.Params,
) error {
rlog.Debugf("running detect with params: %v", params)
mtSrv, err := initializer.InitMTSrv()
if err != nil {
Expand All @@ -35,8 +37,8 @@ func run(ctx context.Context,
Description: "unable to get input files",
}
}
var dataProvider output.DetectDataProvider
render := output.InitRender(outputParams, dataProvider, files, 1)
var dataProvider mtoutput.DetectDataProvider
render := mtoutput.InitRender(outputParams, dataProvider, files, 1)
renderRun := make(chan struct{})
var runGroup errgroup.Group
runGroup.Go(func() error {
Expand Down
6 changes: 3 additions & 3 deletions cmd/mt/detect/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"

cmdmocks "github.com/Smartling/smartling-cli/cmd/mt/mocks"
output "github.com/Smartling/smartling-cli/output/mt"
"github.com/Smartling/smartling-cli/output"
clierror "github.com/Smartling/smartling-cli/services/helpers/cli_error"
srv "github.com/Smartling/smartling-cli/services/mt"
srvmocks "github.com/Smartling/smartling-cli/services/mt/mocks"
Expand All @@ -29,7 +29,7 @@ func TestRunGetFilesError(t *testing.T) {
initializer.On("InitMTSrv").Return(mtSrv, nil)
mtSrv.On("GetFiles", params.InputDirectory, params.FileOrPattern).Return(nil, filesErr)

err := run(ctx, initializer, params, output.OutputParams{})
err := run(ctx, initializer, params, output.Params{})

assert.Error(t, err)
uiErr, ok := err.(clierror.UIError)
Expand All @@ -56,7 +56,7 @@ func TestRun(t *testing.T) {
mtSrv.On("RunDetect", mock.Anything, params, files, mock.Anything).
Return([]srv.DetectOutput{}, nil)

err := run(ctx, initializer, params, output.OutputParams{})
err := run(ctx, initializer, params, output.Params{})

assert.Nil(t, err)
}
34 changes: 0 additions & 34 deletions cmd/mt/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,11 @@ package mt
import (
"os"

"github.com/Smartling/smartling-cli/cmd/helpers/resolve"
output "github.com/Smartling/smartling-cli/output/mt"
clierror "github.com/Smartling/smartling-cli/services/helpers/cli_error"
"github.com/Smartling/smartling-cli/services/helpers/env"

"github.com/spf13/cobra"
)

// ResolveOutputParams resolve OutputParams for subcommands
func ResolveOutputParams(cmd *cobra.Command, fileConfigMTFileFormat *string) (output.OutputParams, error) {
const outputTemplateFlag = "format"
format, err := cmd.Parent().PersistentFlags().GetString("output")
if err != nil {
return output.OutputParams{}, clierror.UIError{
Operation: "get output",
Err: err,
Description: "unable to get output param",
}
}
template := resolve.FallbackString(cmd.Flags().Lookup(outputTemplateFlag), resolve.StringParam{
FlagName: outputTemplateFlag,
Config: fileConfigMTFileFormat,
})

mode, err := cmd.Parent().PersistentFlags().GetString("output-mode")
if err != nil {
return output.OutputParams{}, clierror.UIError{
Operation: "get output mode",
Err: err,
Description: "unable to get output mode param",
}
}
return output.OutputParams{
Mode: mode,
Format: format,
Template: template,
}, nil
}

func resolveConfigDirectory(cmd *cobra.Command) string {
flag := cmd.Root().PersistentFlags().Lookup("operation-directory")
if flag == nil {
Expand Down
Loading