-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoliner.go
114 lines (99 loc) · 2.23 KB
/
goliner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Execute some Go one-liners.
//
// For example:
//
// $ goliner 'println("hello world!")'
//
// hello world!
//
// You can also use stdlib modules like:
//
// $ goliner 'fmt.Println(strings.Join([]string{"foo", "bar"}, ", "))'
//
// foo, bar
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"os/exec"
"strings"
goimports "golang.org/x/tools/imports"
)
var (
imports Strings
)
func init() {
flag.CommandLine.SetOutput(os.Stdout)
flag.Var(&imports, "i", "specify import explicitly")
}
func usage() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [ -i <import_path> ] <codeline> ...\n\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), "Run Golang one-liner.\n")
fmt.Fprintf(flag.CommandLine.Output(), "For example: %s 'fmt.Println(\"Hello world!\")'\n\n", os.Args[0])
flag.PrintDefaults()
}
func prepareSourceFile(imports, args []string) (path string, err error) {
const (
pkgHead = "package main"
mainHead = "func main() {"
tail = "}"
)
var buffer bytes.Buffer
fmt.Fprintln(&buffer, pkgHead)
for _, imp := range imports {
fmt.Fprintf(&buffer, "import \"%s\"\n", imp)
}
fmt.Fprintln(&buffer, mainHead)
for _, line := range args {
fmt.Fprintf(&buffer, "%s\n", line)
}
fmt.Fprintln(&buffer, tail)
// run imports
code, err := goimports.Process("/dev/stdin", buffer.Bytes(), nil)
if err != nil {
return "", err
}
// save to file
file, err := os.CreateTemp("", "goliner.src")
if err != nil {
return "", err
}
fmt.Fprintf(file, "%s", code)
if err = file.Close(); err != nil {
os.Remove(file.Name())
}
path = file.Name() + ".go"
if err = os.Rename(file.Name(), path); err != nil {
os.Remove(file.Name())
return "", err
}
return
}
func main() {
flag.Usage = usage
flag.Parse()
if len(flag.Args()) < 1 {
log.Fatal("Not enough arguments")
}
path, err := prepareSourceFile(imports, flag.Args())
if err != nil {
log.Fatal(err)
}
defer os.Remove(path)
cmd := exec.Command("go", "run", path)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
if err = cmd.Run(); err != nil {
log.Fatalf("Failed: %v", err)
}
}
type Strings []string
func (i Strings) String() string {
return strings.Join([]string(i), ", ")
}
func (i *Strings) Set(value string) error {
*i = append(*i, value)
return nil
}