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

Side by Side Diff: tools/cmd/gorun/main.go

Issue 2963503003: [errors] Greatly simplify common/errors package. (Closed)
Patch Set: fix nits Created 3 years, 5 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 unified diff | Download patch
« no previous file with comments | « tokenserver/appengine/impl/utils/policy/policy.go ('k') | vpython/application/application.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The LUCI Authors. All rights reserved. 1 // Copyright 2016 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 // Package main defines the `gorun` tool, a shorthand tool to extend the 5 // Package main defines the `gorun` tool, a shorthand tool to extend the
6 // "go run"-like convenience to packages. 6 // "go run"-like convenience to packages.
7 // 7 //
8 // Unfortunately, "go run" is hard to use when the target has more than one 8 // Unfortunately, "go run" is hard to use when the target has more than one
9 // ".go" file, and even harder when the target has "_test.go" files. 9 // ".go" file, and even harder when the target has "_test.go" files.
10 // 10 //
11 // This is just a bootstrap to "go build /path/to/X && /path/to/X" using a 11 // This is just a bootstrap to "go build /path/to/X && /path/to/X" using a
12 // temporary directory for build storage. 12 // temporary directory for build storage.
13 package main 13 package main
14 14
15 import ( 15 import (
16 "io/ioutil" 16 "io/ioutil"
17 "os" 17 "os"
18 "os/exec" 18 "os/exec"
19 "path/filepath" 19 "path/filepath"
20 "runtime" 20 "runtime"
21 21
22 "github.com/luci/luci-go/common/errors" 22 "github.com/luci/luci-go/common/errors"
23 "github.com/luci/luci-go/common/system/exitcode" 23 "github.com/luci/luci-go/common/system/exitcode"
24 ) 24 )
25 25
26 func mainImpl(args []string) (int, error) { 26 func mainImpl(args []string) (int, error) {
27 if len(args) < 1 { 27 if len(args) < 1 {
28 » » return 1, errors.Reason("accepts one argument: package").Err() 28 » » return 1, errors.New("accepts one argument: package")
29 } 29 }
30 pkg, args := args[0], args[1:] 30 pkg, args := args[0], args[1:]
31 31
32 // Create a temporary output directory to build into. 32 // Create a temporary output directory to build into.
33 tmpdir, err := ioutil.TempDir("", "luci-gorun") 33 tmpdir, err := ioutil.TempDir("", "luci-gorun")
34 if err != nil { 34 if err != nil {
35 » » return 1, errors.Annotate(err).Reason("failed to create temporar y directory").Err() 35 » » return 1, errors.Annotate(err, "failed to create temporary direc tory").Err()
36 } 36 }
37 defer os.RemoveAll(tmpdir) 37 defer os.RemoveAll(tmpdir)
38 38
39 exePath := filepath.Join(tmpdir, "gorun_target") 39 exePath := filepath.Join(tmpdir, "gorun_target")
40 if runtime.GOOS == "windows" { 40 if runtime.GOOS == "windows" {
41 exePath += ".exe" 41 exePath += ".exe"
42 } 42 }
43 43
44 // Build the package. 44 // Build the package.
45 cmd := exec.Command("go", "build", "-o", exePath, pkg) 45 cmd := exec.Command("go", "build", "-o", exePath, pkg)
46 cmd.Stdout = os.Stdout 46 cmd.Stdout = os.Stdout
47 cmd.Stderr = os.Stderr 47 cmd.Stderr = os.Stderr
48 if err := cmd.Run(); err != nil { 48 if err := cmd.Run(); err != nil {
49 » » return 1, errors.Annotate(err).Reason("failed to build: %(packag e)s").D("package", pkg).Err() 49 » » return 1, errors.Annotate(err, "failed to build: %s", pkg).Err()
50 } 50 }
51 51
52 // Run the package. 52 // Run the package.
53 cmd = exec.Command(exePath, args...) 53 cmd = exec.Command(exePath, args...)
54 cmd.Stdin = os.Stdin 54 cmd.Stdin = os.Stdin
55 cmd.Stdout = os.Stdout 55 cmd.Stdout = os.Stdout
56 cmd.Stderr = os.Stderr 56 cmd.Stderr = os.Stderr
57 if err := cmd.Run(); err != nil { 57 if err := cmd.Run(); err != nil {
58 if rc, ok := exitcode.Get(err); ok { 58 if rc, ok := exitcode.Get(err); ok {
59 return rc, nil 59 return rc, nil
60 } 60 }
61 » » return 1, errors.Annotate(err).Reason("failed to run: %(package) s").D("package", pkg).Err() 61 » » return 1, errors.Annotate(err, "failed to run: %s", pkg).Err()
62 } 62 }
63 63
64 return 0, nil 64 return 0, nil
65 } 65 }
66 66
67 func main() { 67 func main() {
68 rc, err := mainImpl(os.Args[1:]) 68 rc, err := mainImpl(os.Args[1:])
69 if err != nil { 69 if err != nil {
70 » » _, _ = errors.RenderStack(err).DumpTo(os.Stderr) 70 » » for _, line := range errors.RenderStack(err) {
71 » » » os.Stderr.WriteString(line + "\n")
72 » » }
71 } 73 }
72 os.Exit(rc) 74 os.Exit(rc)
73 } 75 }
OLDNEW
« no previous file with comments | « tokenserver/appengine/impl/utils/policy/policy.go ('k') | vpython/application/application.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698