func write(outfile string, image *image.RGBA) { var out io.Writer if outfile == "-" && term.IsTerminal(int(os.Stdout.Fd())) { println("You're on a terminal, and I assume you don't want a face full of PNG binary. " + "Specify -outfile if you want some name other than flame-image.png") outfile = "flame-image.png" } if outfile != "-" { outimg, err := os.Create(outfile) if err != nil { println("Failed to open file for writing", outfile) return } out = outimg defer outimg.Close() } else { out = os.Stdout } png.Encode(out, image) }
func main() { daemonFlag := flag.Bool("daemon", false, "Start rd in daemon mode") colorFlag := flag.Bool("color", term.IsTerminal(syscall.Stdout), "Colorize matches in output") quietFlag := flag.Bool("quiet", false, "Do not print anything if there are no matches") flag.Usage = func() { fmt.Fprintf(os.Stderr, `Usage: %s [options] <command> <args...> Supported commands: query <pattern>|<id> push <path> list Flags: `, os.Args[0]) flag.PrintDefaults() } flag.Parse() // try to connect to the 'rd' daemon connType := "unix" connAddr := os.ExpandEnv("$HOME/.rd.sock") client, err := rpc.Dial(connType, connAddr) if err == nil && *daemonFlag { fmt.Fprintf(os.Stderr, "Daemon is already running\n") os.Exit(1) } else if err != nil && !*daemonFlag { // daemon is not running, try to start it automatically daemonCmd := exec.Command(os.Args[0], "-daemon") err = daemonCmd.Start() if err != nil { fmt.Fprintf(os.Stderr, "Unable to start the 'rd' daemon: %v\n", err) os.Exit(1) } maxWait := time.Now().Add(1 * time.Second) daemonRunning := false for time.Now().Before(maxWait) && !daemonRunning { time.Sleep(10 * time.Millisecond) client, err = rpc.Dial(connType, connAddr) daemonRunning = err == nil } if !daemonRunning { fmt.Fprintf(os.Stderr, "Unable to connect to the 'rd' daemon") os.Exit(1) } } if *daemonFlag { err := os.Remove(connAddr) if err != nil && !os.IsNotExist(err) { log.Fatalf("Unable to remove socket - %v", err) } // server operation server := NewRecentDirServer() go server.serve() rpcServer := rpc.NewServer() rpcServer.Register(&server) listener, err := net.Listen(connType, connAddr) if err != nil { log.Fatal("Listen error:", err) } rpcServer.Accept(listener) } else { // client operation if flag.NArg() < 1 { fmt.Printf("No command given. Use '%s -help' for a list of supported commands\n", os.Args[0]) os.Exit(1) } if err != nil { fmt.Printf( ` Unable to connect to the rd daemon. Use 'rd -daemon' to start it. This should be set up to run at login. `) os.Exit(1) } modeStr := flag.Arg(0) args := []string{} if flag.NArg() > 1 { args = flag.Args()[1:] } switch modeStr { case "query", "q": handleQueryCommand(client, args, *colorFlag, *quietFlag) case "push", "p": handlePushCommand(client, args) case "list": handleListCommand(client) case "stop": client.Call("RecentDirServer.Stop", false /* unused */, nil) default: fmt.Printf("Unknown command '%s', use '%s -help' for a list of supported commands\n", modeStr, os.Args[0]) } } }
func IsRunningInTty() bool { return term.IsTerminal(1) }