-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
88 lines (75 loc) · 1.77 KB
/
init.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
// Package init provides the micro init command for initialising plugins and imports
package init
import (
"fmt"
"os"
"path"
"strings"
"github.com/micro/micro/v3/cmd"
"github.com/urfave/cli/v2"
)
var (
// The import path we use for imports
Import = "github.com/micro/micro/profile"
// Vesion of micro
Version = "v3"
)
func Run(ctx *cli.Context) error {
var imports []string
for _, val := range ctx.StringSlice("profile") {
for _, profile := range strings.Split(val, ",") {
p := strings.TrimSpace(profile)
if len(p) == 0 {
continue
}
path := path.Join(Import, p, Version)
imports = append(imports, fmt.Sprintf("\t_ \"%s\"\n", path))
}
}
if len(ctx.String("package")) > 0 {
imports = append(imports, fmt.Sprintf("\t_ \"%s\"\n", ctx.String("package")))
}
if len(imports) == 0 {
return nil
}
f := os.Stdout
if v := ctx.String("output"); v != "stdout" {
var err error
f, err = os.Create(v)
if err != nil {
return err
}
}
fmt.Fprint(f, "package main\n\n")
fmt.Fprint(f, "import (\n")
// write the imports
for _, i := range imports {
fmt.Fprint(f, i)
}
fmt.Fprint(f, ")\n")
return nil
}
func init() {
cmd.Register(&cli.Command{
Name: "init",
Usage: "Generate a profile for micro plugins",
Description: `'micro init' generates a profile.go file defining plugins and imports`,
Action: Run,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "package",
Usage: "The package to load, e.g. github.com/m3o/platform/profile/ci",
},
&cli.StringSliceFlag{
Name: "profile",
Usage: "A comma separated list of imports to load",
Value: cli.NewStringSlice(),
},
&cli.StringFlag{
Name: "output",
Usage: "Where to output the file, by default stdout",
Value: "stdout",
},
},
})
}