-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathroot.go
More file actions
116 lines (97 loc) · 3.47 KB
/
Copy pathroot.go
File metadata and controls
116 lines (97 loc) · 3.47 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package cmd
import (
"context"
"errors"
"os"
"os/signal"
"slices"
"syscall"
"time"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
"github.com/shopware/shopware-cli/cmd/account"
"github.com/shopware/shopware-cli/cmd/ai"
"github.com/shopware/shopware-cli/cmd/extension"
"github.com/shopware/shopware-cli/cmd/project"
accountApi "github.com/shopware/shopware-cli/internal/account-api"
"github.com/shopware/shopware-cli/internal/system"
"github.com/shopware/shopware-cli/internal/tui"
"github.com/shopware/shopware-cli/logging"
)
var 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,
}
// Execute runs the root command and returns the process exit code after cleanup.
func Execute(ctx context.Context) int {
rootCmd.Use = commandNameFromArgs(os.Args)
args := mapAliasArgs(os.Args)
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer stop()
verbose := slices.Contains(args, "--verbose")
ctx = logging.WithLogger(ctx, logging.NewLogger(verbose))
ctx = logging.WithVerbose(ctx, verbose)
ctx = system.WithInteraction(ctx, !slices.Contains(args, "--no-interaction") && !slices.Contains(args, "-n") && isatty.IsTerminal(os.Stdin.Fd()))
tui.AppVersion = version
accountApi.SetUserAgent("shopware-cli/" + version)
rootCmd.SetArgs(args)
updateHandle, updateCancel := startUpdateCheck(ctx, args)
defer updateCancel()
start := time.Now()
err := rootCmd.ExecuteContext(ctx)
trackCommandExecution(ctx, args, start, err)
printUpdateHint(ctx, os.Stderr, updateHandle.Wait(ctx).Release)
return exitCode(ctx, err)
}
// exitCode maps the command error to the process exit code. Status errors are
// already printed by the command as a human-readable status, so they exit 1
// without logging the error again.
func exitCode(ctx context.Context, err error) int {
if err == nil {
return 0
}
if !errors.Is(err, project.ErrEnvironmentDown) && !errors.Is(err, project.ErrProxyNotRegistered) {
logging.FromContext(ctx).Errorln(err)
}
return 1
}
func init() {
rootCmd.SilenceErrors = true
// Cobra prints the usage block for every error a command returns. Flags,
// argument counts and flag groups are validated before the pre-run hooks
// fire, so silencing usage here keeps it for invocation mistakes while
// errors returned from RunE only print the error itself. Traversal is
// enabled so subtrees with their own PersistentPreRunE (account) still run
// this hook.
cobra.EnableTraverseRunHooks = true
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
cmd.SilenceUsage = true
return nil
}
cobra.OnFinalize(func() {
_ = system.CloseCaches()
})
rootCmd.PersistentFlags().Bool("verbose", false, "Show debug output")
rootCmd.PersistentFlags().BoolP("no-interaction", "n", false, "Do not ask any interactive questions")
rootCmd.PersistentFlags().Bool("no-update-hint", false, "Do not show update notifications")
project.Register(rootCmd)
extension.Register(rootCmd)
ai.Register(rootCmd)
account.Register(rootCmd, func(commandName string) (*account.ServiceContainer, error) {
if commandName == "login" || commandName == "logout" {
return &account.ServiceContainer{
AccountClient: nil,
}, nil
}
client, err := accountApi.NewApi(rootCmd.Context())
if err != nil {
return nil, err
}
return &account.ServiceContainer{
AccountClient: client,
}, nil
})
}