github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/client/cli/init/init.go (about) 1 // Package init provides the micro init command for initialising plugins and imports 2 package init 3 4 import ( 5 "fmt" 6 "os" 7 "path" 8 "strings" 9 10 "github.com/tickoalcantara12/micro/v3/cmd" 11 "github.com/urfave/cli/v2" 12 ) 13 14 var ( 15 // The import path we use for imports 16 Import = "github.com/tickoalcantara12/micro/profile" 17 // Vesion of micro 18 Version = "v3" 19 ) 20 21 func Run(ctx *cli.Context) error { 22 var imports []string 23 24 for _, val := range ctx.StringSlice("profile") { 25 for _, profile := range strings.Split(val, ",") { 26 p := strings.TrimSpace(profile) 27 if len(p) == 0 { 28 continue 29 } 30 path := path.Join(Import, p, Version) 31 imports = append(imports, fmt.Sprintf("\t_ \"%s\"\n", path)) 32 } 33 } 34 35 if len(ctx.String("package")) > 0 { 36 imports = append(imports, fmt.Sprintf("\t_ \"%s\"\n", ctx.String("package"))) 37 } 38 39 if len(imports) == 0 { 40 return nil 41 } 42 43 f := os.Stdout 44 45 if v := ctx.String("output"); v != "stdout" { 46 var err error 47 f, err = os.Create(v) 48 if err != nil { 49 return err 50 } 51 } 52 53 fmt.Fprint(f, "package main\n\n") 54 fmt.Fprint(f, "import (\n") 55 56 // write the imports 57 for _, i := range imports { 58 fmt.Fprint(f, i) 59 } 60 61 fmt.Fprint(f, ")\n") 62 return nil 63 } 64 65 func init() { 66 cmd.Register(&cli.Command{ 67 Name: "init", 68 Usage: "Generate a profile for micro plugins", 69 Description: `'micro init' generates a profile.go file defining plugins and imports`, 70 Action: Run, 71 Flags: []cli.Flag{ 72 &cli.StringFlag{ 73 Name: "package", 74 Usage: "The package to load, e.g. github.com/m3o/platform/profile/ci", 75 }, 76 &cli.StringSliceFlag{ 77 Name: "profile", 78 Usage: "A comma separated list of imports to load", 79 Value: cli.NewStringSlice(), 80 }, 81 &cli.StringFlag{ 82 Name: "output", 83 Usage: "Where to output the file, by default stdout", 84 Value: "stdout", 85 }, 86 }, 87 }) 88 }