/
init.go
57 lines (50 loc) 路 1.39 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
package cmd
import (
"fmt"
"log"
"github.com/b4b4r07/afx/pkg/helpers/templates"
"github.com/spf13/cobra"
)
var (
// initLong is long description of init command
initLong = templates.LongDesc(``)
// initExample is examples for init command
initExample = templates.Examples(`
# show a source file to start packages installed by afx
afx init
# enable plugins/commands in current shell
source <(afx init)
# automatically load configurations
Bash:
echo 'source <(afx init)' ~/.bashrc
Zsh:
echo 'source <(afx init)' ~/.zshrc
Fish:
echo 'afx init | source' ~/.config/fish/config.fish
`)
)
// newInitCmd creates a new init command
func (m metaCmd) newInitCmd() *cobra.Command {
return &cobra.Command{
Use: "init",
Short: "Initialize installed packages",
Long: initLong,
Example: initExample,
DisableFlagsInUseLine: true,
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
for _, pkg := range m.packages {
if err := pkg.Init(); err != nil {
log.Printf("[ERROR] %s: failed to init package: %v\n", pkg.GetName(), err)
// do not return err to continue to load even if failed
continue
}
}
for k, v := range m.main.Env {
fmt.Printf("export %s=%q\n", k, v)
}
},
}
}