Skip to content

Commit

Permalink
Merge pull request #100 from transifex/silent_flag
Browse files Browse the repository at this point in the history
Silent flag
  • Loading branch information
kbairak authored Oct 11, 2022
2 parents cf763d1 + abde3f0 commit 39a6452
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 100 deletions.
10 changes: 10 additions & 0 deletions cmd/tx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func Main() {
Aliases: []string{"w"},
Value: 5,
},
&cli.BoolFlag{
Name: "silent",
Usage: "Whether to reduce verbosity of the output",
},
},
Action: func(c *cli.Context) error {
cfg, err := config.LoadFromPaths(
Expand Down Expand Up @@ -225,6 +229,7 @@ func Main() {
Branch: c.String("branch"),
All: c.Bool("all"),
Workers: c.Int("workers"),
Silent: c.Bool("silent"),
}

if args.All && len(args.Languages) > 0 {
Expand Down Expand Up @@ -369,6 +374,10 @@ func Main() {
Aliases: []string{"w"},
Value: 5,
},
&cli.BoolFlag{
Name: "silent",
Usage: "Whether to reduce verbosity of the output",
},
},
Action: func(c *cli.Context) error {
cfg, err := config.LoadFromPaths(c.String("root-config"),
Expand Down Expand Up @@ -420,6 +429,7 @@ func Main() {
Branch: c.String("branch"),
MinimumPercentage: c.Int("minimum-perc"),
Workers: c.Int("workers"),
Silent: c.Bool("silent"),
}

if c.Bool("xliff") && c.Bool("json") {
Expand Down
113 changes: 76 additions & 37 deletions internal/txlib/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type PullCommandArguments struct {
Branch string
MinimumPercentage int
Workers int
Silent bool
}

func PullCommand(
Expand All @@ -48,11 +49,13 @@ func PullCommand(
return cfgResources[i].GetAPv3Id() < cfgResources[j].GetAPv3Id()
})

fmt.Print("# Getting info about resources\n\n")
if !args.Silent {
fmt.Print("# Getting info about resources\n\n")
}

filePullTaskChannel := make(chan *FilePullTask)
var filePullTasks []*FilePullTask
pool := worker_pool.New(args.Workers, len(cfgResources))
pool := worker_pool.New(args.Workers, len(cfgResources), args.Silent)
for _, cfgResource := range cfgResources {
pool.Add(&ResourcePullTask{cfgResource, api, args, filePullTaskChannel, cfg})
}
Expand All @@ -71,6 +74,17 @@ func PullCommand(
if pool.IsAborted {
return errors.New("Aborted")
}
if args.Silent {
var names []string
for _, cfgResource := range cfgResources {
names = append(names, fmt.Sprintf(
"%s.%s",
cfgResource.ProjectSlug,
cfgResource.ResourceSlug,
))
}
fmt.Printf("Got info about resources: %s\n", strings.Join(names, ", "))
}

if len(filePullTasks) > 0 {
sort.Slice(filePullTasks, func(i, j int) bool {
Expand All @@ -83,8 +97,10 @@ func PullCommand(
}
})

fmt.Print("\n# Pulling files\n\n")
pool = worker_pool.New(args.Workers, len(filePullTasks))
if !args.Silent {
fmt.Print("\n# Pulling files\n\n")
}
pool = worker_pool.New(args.Workers, len(filePullTasks), args.Silent)
for _, task := range filePullTasks {
pool.Add(task)
}
Expand All @@ -94,6 +110,23 @@ func PullCommand(
if pool.IsAborted {
return errors.New("Aborted")
}
if args.Silent {
var names []string
for _, filePullTask := range filePullTasks {
var languageCode string
if filePullTask.languageCode == "" {
languageCode = "source"
} else {
languageCode = filePullTask.languageCode
}
names = append(names, fmt.Sprintf(
"%s: %s",
filePullTask.cfgResource.ResourceSlug,
languageCode,
))
}
fmt.Printf("Pulled files: %s\n", strings.Join(names, ", "))
}
}

return nil
Expand All @@ -114,15 +147,18 @@ func (task *ResourcePullTask) Run(send func(string), abort func()) {
filePullTaskChannel := task.filePullTaskChannel
cfg := task.cfg

sendMessage := func(body string) {
sendMessage := func(body string, force bool) {
if args.Silent && !force {
return
}
send(fmt.Sprintf(
"%s.%s - %s",
cfgResource.ProjectSlug,
cfgResource.ResourceSlug,
body,
))
}
sendMessage("Getting info")
sendMessage("Getting info", false)

localToRemoteLanguageMappings := makeLocalToRemoteLanguageMappings(
*cfg,
Expand All @@ -135,24 +171,27 @@ func (task *ResourcePullTask) Run(send func(string), abort func()) {
var err error
resource, err := txapi.GetResourceById(api, cfgResource.GetAPv3Id())
if err != nil {
sendMessage(err.Error())
sendMessage(err.Error(), true)
if !args.Skip {
abort()
}
return
}
if resource == nil {
sendMessage(fmt.Sprintf(
"Resource %s.%s does not exist",
cfgResource.ProjectSlug,
cfgResource.ResourceSlug,
))
sendMessage(
fmt.Sprintf(
"Resource %s.%s does not exist",
cfgResource.ProjectSlug,
cfgResource.ResourceSlug,
),
true,
)
return
}

projectRelationship, err := resource.Fetch("project")
if err != nil {
sendMessage(err.Error())
sendMessage(err.Error(), true)
if !args.Skip {
abort()
}
Expand All @@ -168,7 +207,7 @@ func (task *ResourcePullTask) Run(send func(string), abort func()) {
stats, err = txapi.GetResourceStats(api, resource, nil)
}
if err != nil {
sendMessage(err.Error())
sendMessage(err.Error(), true)
if !args.Skip {
abort()
}
Expand Down Expand Up @@ -197,7 +236,7 @@ func (task *ResourcePullTask) Run(send func(string), abort func()) {
// Local stuff
err = checkFileFilter(cfgResource.FileFilter)
if err != nil {
sendMessage(err.Error())
sendMessage(err.Error(), true)
if !args.Skip {
abort()
}
Expand Down Expand Up @@ -279,7 +318,7 @@ func (task *ResourcePullTask) Run(send func(string), abort func()) {
}
}
}
sendMessage("Done")
sendMessage("Done", false)
}

type FilePullTask struct {
Expand All @@ -303,7 +342,10 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
filePath := task.filePath
remoteToLocalLanguageMapping := task.remoteToLocalLanguageMappings

sendMessage := func(body string) {
sendMessage := func(body string, force bool) {
if args.Silent && !force {
return
}
var code string
if languageCode == "" {
code = "source"
Expand All @@ -320,17 +362,14 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
body,
))
}
sendMessage("Pulling file")
sendMessage("Pulling file", false)

if languageCode == "" {
sourceFile := setFileTypeExtensions(args.FileType, cfgResource.SourceFile)

_, err := os.Stat(sourceFile)
if err == nil && args.DisableOverwrite {
sendMessage("Disable Overwrite is enabled, skipping")
if !args.Skip {
abort()
}
sendMessage("Disable Overwrite is enabled, skipping", false)
return
}

Expand All @@ -341,14 +380,14 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
args.UseGitTimestamps,
)
if err != nil {
sendMessage(err.Error())
sendMessage(err.Error(), true)
if !args.Skip {
abort()
}
return
}
if shouldSkip {
sendMessage("Local file is newer than remote, skipping")
sendMessage("Local file is newer than remote, skipping", false)
return
}
}
Expand All @@ -368,10 +407,10 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
return err
},
"Creating download job",
sendMessage,
func(msg string) { sendMessage(msg, false) },
)
if err != nil {
sendMessage(err.Error())
sendMessage(err.Error(), true)
if !args.Skip {
abort()
}
Expand All @@ -389,10 +428,10 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
)
},
"",
sendMessage,
func(msg string) { sendMessage(msg, false) },
)
if err != nil {
sendMessage(err.Error())
sendMessage(err.Error(), true)
if !args.Skip {
abort()
}
Expand All @@ -402,7 +441,7 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
if filePath != "" {
// Remote language file exists and so does local
if args.DisableOverwrite {
sendMessage("Disable overwrite enabled, skipping")
sendMessage("Disable overwrite enabled, skipping", false)
return
}
} else {
Expand All @@ -415,7 +454,7 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
if !args.All &&
(!stringSliceContains(args.Languages, remoteLanguageCode) &&
!stringSliceContains(args.Languages, localLanguageCode)) {
sendMessage("File was not found locally, skipping")
sendMessage("File was not found locally, skipping", false)
return
}
filePath = strings.Replace(
Expand All @@ -441,14 +480,14 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
args.Force,
)
if err != nil {
sendMessage(err.Error())
sendMessage(err.Error(), true)
if !args.Skip {
abort()
}
return
}
if shouldSkip {
sendMessage("Local file is newer than remote, skipping")
sendMessage("Local file is newer than remote, skipping", false)
return
}

Expand All @@ -469,10 +508,10 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
return err
},
"Creating download job",
sendMessage,
func(msg string) { sendMessage(msg, false) },
)
if err != nil {
sendMessage(err.Error())
sendMessage(err.Error(), true)
if !args.Skip {
abort()
}
Expand All @@ -486,17 +525,17 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
return txapi.PollTranslationDownload(download, filePath)
},
"",
sendMessage,
func(msg string) { sendMessage(msg, false) },
)
if err != nil {
sendMessage(err.Error())
sendMessage(err.Error(), true)
if !args.Skip {
abort()
}
return
}
}
sendMessage("Done")
sendMessage("Done", false)
}

func shouldSkipDownload(
Expand Down
Loading

0 comments on commit 39a6452

Please sign in to comment.