|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/linuxsuren/http-downloader/pkg/installer" |
| 5 | + "github.com/spf13/cobra" |
| 6 | + "runtime" |
| 7 | +) |
| 8 | + |
| 9 | +type initOption struct { |
| 10 | + require, optional, fetch bool |
| 11 | + |
| 12 | + // inner fields |
| 13 | + requireTools, optionalTools map[string]string |
| 14 | +} |
| 15 | + |
| 16 | +// NewInitCommand returns a command for init |
| 17 | +func NewInitCommand(requireTools, optionalTools map[string]string) (cmd *cobra.Command) { |
| 18 | + opt := &initOption{ |
| 19 | + requireTools: requireTools, |
| 20 | + optionalTools: optionalTools, |
| 21 | + } |
| 22 | + |
| 23 | + cmd = &cobra.Command{ |
| 24 | + Use: "init", |
| 25 | + Short: "Init your command", |
| 26 | + RunE: opt.runE, |
| 27 | + } |
| 28 | + |
| 29 | + flags := cmd.Flags() |
| 30 | + flags.BoolVarP(&opt.require, "require", "r", true, |
| 31 | + "Indicate if you want to install required tools") |
| 32 | + flags.BoolVarP(&opt.optional, "optional", "o", false, |
| 33 | + "Indicate if you want to install optional tools") |
| 34 | + flags.BoolVarP(&opt.fetch, "fetch", "", true, |
| 35 | + "Indicate if fetch the latest config of tools") |
| 36 | + return |
| 37 | +} |
| 38 | + |
| 39 | +func (o *initOption) runE(_ *cobra.Command, _ []string) (err error) { |
| 40 | + is := installer.Installer{ |
| 41 | + Provider: "github", |
| 42 | + OS: runtime.GOOS, |
| 43 | + Arch: runtime.GOARCH, |
| 44 | + Fetch: o.fetch, |
| 45 | + } |
| 46 | + if o.require { |
| 47 | + err = is.CheckDepAndInstall(o.requireTools) |
| 48 | + } |
| 49 | + if err == nil && o.optional { |
| 50 | + err = is.CheckDepAndInstall(o.optionalTools) |
| 51 | + } |
| 52 | + return |
| 53 | +} |
0 commit comments