databricks-cli/main.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

57 lines
1.4 KiB
Go
Raw Normal View History

2022-05-13 13:30:22 +00:00
package main
2022-05-14 17:56:09 +00:00
import (
"context"
2025-02-04 15:25:59 +00:00
"encoding/json"
2025-02-03 03:18:38 +00:00
"fmt"
2025-02-04 15:25:59 +00:00
"io"
"os"
"github.com/databricks/cli/cmd"
"github.com/databricks/cli/cmd/root"
2025-02-03 03:18:38 +00:00
"github.com/databricks/cli/libs/telemetry"
2022-05-14 17:56:09 +00:00
)
2022-05-13 13:30:22 +00:00
func main() {
ctx := context.Background()
2025-02-03 03:18:38 +00:00
// Uploading telemetry data spawns a new process. We handle this separately
// from the rest of the CLI commands.
// This is done because [root.Execute] spawns a new process to run the
// "telemetry upload" command if there are logs to be uploaded. Having this outside
// of [root.Execute] ensures that the telemetry upload process is not spawned
// infinitely in a recursive manner.
2025-02-03 03:24:09 +00:00
if len(os.Args) == 3 && os.Args[1] == "telemetry" && os.Args[2] == "upload" {
2025-02-03 03:18:38 +00:00
var err error
2025-02-04 15:25:59 +00:00
// By default, this command should not write anything to stdout or stderr.
outW := io.Discard
errW := io.Discard
2025-02-03 03:18:38 +00:00
// If the environment variable is set, redirect stdout to the file.
// This is useful for testing.
if v := os.Getenv(telemetry.UploadLogsFileEnvVar); v != "" {
2025-02-04 15:25:59 +00:00
outW, _ = os.OpenFile(v, os.O_CREATE|os.O_WRONLY, 0o644)
errW = outW
2025-02-03 03:18:38 +00:00
}
2025-02-04 15:25:59 +00:00
resp, err := telemetry.Upload()
2025-02-03 03:18:38 +00:00
if err != nil {
2025-02-04 15:25:59 +00:00
fmt.Fprintf(errW, "error: %s\n", err)
2025-02-03 03:18:38 +00:00
os.Exit(1)
}
2025-02-04 15:25:59 +00:00
fmt.Fprintf(outW, "Telemetry logs uploaded successfully\n")
fmt.Fprintln(outW, "Response:")
b, err := json.Marshal(resp)
if err == nil {
fmt.Fprintln(outW, string(b))
}
2025-02-03 03:18:38 +00:00
os.Exit(0)
}
err := root.Execute(ctx, cmd.New(ctx))
if err != nil {
os.Exit(1)
}
2022-05-13 13:30:22 +00:00
}