-
Notifications
You must be signed in to change notification settings - Fork 31
/
root.go
77 lines (64 loc) · 1.91 KB
/
root.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
package cmd
import (
"context"
"os"
"github.com/spf13/cobra"
accountApi "github.com/FriendsOfShopware/shopware-cli/account-api"
"github.com/FriendsOfShopware/shopware-cli/cmd/account"
"github.com/FriendsOfShopware/shopware-cli/cmd/extension"
"github.com/FriendsOfShopware/shopware-cli/cmd/project"
"github.com/FriendsOfShopware/shopware-cli/internal/config"
"github.com/FriendsOfShopware/shopware-cli/logging"
)
var (
cfgFile string
version = "dev"
)
var rootCmd = &cobra.Command{
Use: "shopware-cli",
Short: "A cli for common Shopware tasks",
Long: `This application contains some utilities like extension management`,
Version: version,
}
func Execute(ctx context.Context) {
verbose := false
if err := rootCmd.ParseFlags(os.Args); err == nil {
verbose, _ = rootCmd.PersistentFlags().GetBool("verbose")
}
ctx = logging.WithLogger(ctx, logging.NewLogger(verbose))
accountApi.SetUserAgent("shopware-cli/" + version)
if err := rootCmd.ExecuteContext(ctx); err != nil {
logging.FromContext(ctx).Fatalln(err)
}
}
func init() {
rootCmd.SilenceErrors = true
cobra.OnInitialize(func() {
_ = config.InitConfig(cfgFile)
})
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.shopware-cli.yaml)")
rootCmd.PersistentFlags().Bool("verbose", false, "show debug output")
project.Register(rootCmd)
extension.Register(rootCmd)
account.Register(rootCmd, func(commandName string) (*account.ServiceContainer, error) {
err := config.InitConfig(cfgFile)
if err != nil {
return nil, err
}
conf := config.Config{}
if commandName == "login" || commandName == "logout" {
return &account.ServiceContainer{
Conf: conf,
AccountClient: nil,
}, nil
}
client, err := accountApi.NewApi(rootCmd.Context(), conf)
if err != nil {
return nil, err
}
return &account.ServiceContainer{
Conf: conf,
AccountClient: client,
}, nil
})
}