| 123 | } |
| 124 | |
| 125 | func Execute() (code exitCode) { |
| 126 | start := time.Now() |
| 127 | hasDebug := os.Getenv("DEBUG") != "" |
| 128 | hasTelemetry := os.Getenv("ALGOLIA_CLI_TELEMETRY") != "0" |
| 129 | |
| 130 | // Set up the command factory. |
| 131 | cfg := config.Config{} |
| 132 | cfg.InitConfig() |
| 133 | cmdFactory := factory.New(version.Version, &cfg) |
| 134 | stderr := cmdFactory.IOStreams.ErrOut |
| 135 | |
| 136 | // Set up the update notifier. |
| 137 | updateMessageChan := make(chan *update.ReleaseInfo) |
| 138 | go func() { |
| 139 | rel, err := checkForUpdate(cfg, version.Version) |
| 140 | if err != nil && hasDebug { |
| 141 | fmt.Fprintf(stderr, "Error checking for update: %s\n", err) |
| 142 | } |
| 143 | updateMessageChan <- rel |
| 144 | }() |
| 145 | |
| 146 | // Set up the root command. |
| 147 | rootCmd := NewRootCmd(cmdFactory) |
| 148 | |
| 149 | // Pre-command auth check and telemetry setup. |
| 150 | authError := errors.New("authError") |
| 151 | rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { |
| 152 | if auth.IsAuthCheckEnabled(cmd) { |
| 153 | if err := auth.CheckAuth(cfg); err != nil { |
| 154 | fmt.Fprintf(stderr, "Authentication error: %s\n", err) |
| 155 | fmt.Fprintln( |
| 156 | stderr, |
| 157 | "Please run `algolia profile add` to configure your first profile.", |
| 158 | ) |
| 159 | return authError |
| 160 | } |
| 161 | |
| 162 | if err := auth.CheckACLs(cmd, cmdFactory); err != nil { |
| 163 | fmt.Fprint(stderr, err) |
| 164 | return authError |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if !cmdutil.ShouldTrackUsage(cmd) { |
| 169 | return nil |
| 170 | } |
| 171 | |
| 172 | // Initialize telemetry context. |
| 173 | appID, err := cfg.Profile().GetApplicationID() |
| 174 | if err != nil { |
| 175 | appID = "" |
| 176 | } |
| 177 | telemetryMetadata := telemetry.GetEventMetadata(cmd.Context()) |
| 178 | telemetryMetadata.SetCobraCommandContext(cmd) |
| 179 | telemetryMetadata.SetAppID(appID) |
| 180 | telemetryMetadata.SetConfiguredApplicationsNb(len(cfg.ConfiguredProfiles())) |
| 181 | |
| 182 | if token := auth.LoadToken(); token != nil { |