Skip to content

Commit e0ee975

Browse files
committed
Allow to pack imports (-i) with comma
Allow to pack multiple imports with comma: goeval -i=fmt,time 'fmt.Println(time.Now())' Closes #8.
1 parent e0513a7 commit e0ee975

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ func (*imports) String() string {
4646
}
4747

4848
func (imp *imports) Set(s string) error {
49+
// Allow -i fmt,os
50+
// Comma is not allowed in import path
51+
if p1, remainder, ok := strings.Cut(s, ","); ok {
52+
err := imp.Set(p1)
53+
if err == nil {
54+
imp.Set(remainder)
55+
}
56+
return err
57+
}
58+
59+
// Optional aliasing with [alias=]import
4960
var alias, path, version string
5061
var ok bool
5162
if alias, path, ok = strings.Cut(s, "="); !ok {

main_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,18 @@ func Example_flag() {
6565
// -x
6666
// toto.go
6767
}
68+
69+
func Example_import() {
70+
goeval(`-goimports=`, `-i`, `fmt`, `fmt.Println("OK")`)
71+
goeval(`-goimports=`, `-i=fmt`, `fmt.Println("OK")`)
72+
goeval(`-goimports=`, `-i`, `fmt`, `-i`, `time`, `fmt.Println(time.Time{}.In(time.UTC))`)
73+
goeval(`-goimports=`, `-i`, `fmt,time`, `fmt.Println(time.Time{}.In(time.UTC))`)
74+
goeval(`-goimports=`, `-i=fmt,time`, `fmt.Println(time.Time{}.In(time.UTC))`)
75+
76+
// Output:
77+
// OK
78+
// OK
79+
// 0001-01-01 00:00:00 +0000 UTC
80+
// 0001-01-01 00:00:00 +0000 UTC
81+
// 0001-01-01 00:00:00 +0000 UTC
82+
}

0 commit comments

Comments
 (0)