github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/cmd/autogazelle/client_unix.go (about) 1 //go:build darwin || dragonfly || freebsd || linux || nacl || netbsd || openbsd || solaris || windows 2 // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows 3 4 /* Copyright 2018 The Bazel Authors. All rights reserved. 5 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 */ 18 19 package main 20 21 import ( 22 "fmt" 23 "io" 24 "log" 25 "net" 26 "os" 27 "time" 28 ) 29 30 // runClient performs the main work of the client. It attempts to connect 31 // to the server via a UNIX-domain socket. If the server is not running, 32 // it starts the server and tries again. The server does all the work, so 33 // the client just waits for the server to complete, then exits. 34 func runClient() error { 35 startTime := time.Now() 36 conn, err := net.Dial("unix", *socketPath) 37 if err != nil { 38 if err := startServer(); err != nil { 39 return fmt.Errorf("error starting server: %v", err) 40 } 41 for retry := 0; retry < 3; retry++ { 42 conn, err = net.Dial("unix", *socketPath) 43 if err == nil { 44 break 45 } 46 // Wait for server to start listening. 47 time.Sleep(1 * time.Second) 48 } 49 if err != nil { 50 return fmt.Errorf("failed to connect to server: %v", err) 51 } 52 } 53 defer conn.Close() 54 55 if _, err := io.Copy(os.Stderr, conn); err != nil { 56 log.Print(err) 57 } 58 59 elapsedTime := time.Since(startTime) 60 log.Printf("ran gazelle in %.3f s", elapsedTime.Seconds()) 61 return nil 62 }