Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Unified Diff: client/cmd/isolate/batch_archive.go

Issue 2937583003: Refactor genjson processing code. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: client/cmd/isolate/batch_archive.go
diff --git a/client/cmd/isolate/batch_archive.go b/client/cmd/isolate/batch_archive.go
index ee9da572f6e938b5a2e79364cad9b4d48cd8d397..17d2af62c8fe20353de1bda85dbd997f209e4963 100644
--- a/client/cmd/isolate/batch_archive.go
+++ b/client/cmd/isolate/batch_archive.go
@@ -134,44 +134,25 @@ func (c *batchArchiveRun) main(a subcommands.Application, args []string) error {
ctx := c.defaultFlags.MakeLoggingContext(os.Stderr)
arch := archiver.New(ctx, isolatedclient.New(nil, client, c.isolatedFlags.ServerURL, c.isolatedFlags.Namespace, nil, nil), out)
CancelOnCtrlC(arch)
- type tmp struct {
+
+ type namedItem struct {
*archiver.Item
name string
}
- items := make(chan *tmp, len(args))
+ items := make(chan *namedItem, len(args))
var wg sync.WaitGroup
for _, arg := range args {
wg.Add(1)
- go func(genJsonPath string) {
+ go func(genJSONPath string) {
defer wg.Done()
- data := &struct {
- Args []string
- Dir string
- Version int
- }{}
- if err := common.ReadJSONFile(genJsonPath, data); err != nil {
+ if opts, err := processGenJSON(genJSONPath); err != nil {
arch.Cancel(err)
- return
- }
- if data.Version != isolate.IsolatedGenJSONVersion {
- arch.Cancel(fmt.Errorf("invalid version %d in %s", data.Version, genJsonPath))
- return
- }
- if !common.IsDirectory(data.Dir) {
- arch.Cancel(fmt.Errorf("invalid dir %s in %s", data.Dir, genJsonPath))
- return
- }
- opts, err := parseArchiveCMD(data.Args, data.Dir)
- if err != nil {
- arch.Cancel(fmt.Errorf("invalid archive command in %s: %s", genJsonPath, err))
- return
+ } else {
+ items <- &namedItem{
+ isolate.Archive(arch, opts),
+ strippedIsolatedName(opts.Isolated),
+ }
}
- name := filepath.Base(opts.Isolated)
- // Strip the extension if there is one.
- if dotIndex := strings.LastIndex(name, "."); dotIndex != -1 {
- name = name[0:dotIndex]
- }
- items <- &tmp{isolate.Archive(arch, opts), name}
}(arg)
}
go func() {
@@ -204,6 +185,39 @@ func (c *batchArchiveRun) main(a subcommands.Application, args []string) error {
return err
}
+// processGenJSON validates a genJSON file and returns the contents.
+func processGenJSON(genJSONPath string) (*isolate.ArchiveOptions, error) {
+ data := &struct {
+ Args []string
+ Dir string
+ Version int
+ }{}
+ if err := common.ReadJSONFile(genJSONPath, data); err != nil {
+ return nil, err
+ }
+ if data.Version != isolate.IsolatedGenJSONVersion {
+ return nil, fmt.Errorf("invalid version %d in %s", data.Version, genJSONPath)
+ }
+ if !common.IsDirectory(data.Dir) {
+ return nil, fmt.Errorf("invalid dir %s in %s", data.Dir, genJSONPath)
+ }
+ opts, err := parseArchiveCMD(data.Args, data.Dir)
+ if err != nil {
+ return nil, fmt.Errorf("invalid archive command in %s: %s", genJSONPath, err)
+ }
+ return opts, nil
+}
+
+// strippedIsolatedName returns the base name of an isolated path, with the extension (if any) removed.
+func strippedIsolatedName(isolated string) string {
+ name := filepath.Base(isolated)
+ // Strip the extension if there is one.
+ if dotIndex := strings.LastIndex(name, "."); dotIndex != -1 {
+ return name[0:dotIndex]
+ }
+ return name
+}
+
func (c *batchArchiveRun) Run(a subcommands.Application, args []string, _ subcommands.Env) int {
if err := c.Parse(a, args); err != nil {
fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698