Example #1
0
func mainClient() {
	opts := goopt.NewMergedSet(genops, cliops)
	args, err := opts.Parse(os.Args)
	if err != nil {
		mainUsage(err)
	} else if len(args) > 0 {
		mainUsage(errors.New("Unparsed arguments: " + strings.Join(args, " ")))
	}

	client, err := ConnectToDBus(*bus)
	if err != nil {
		log.Fatal(err)
	}

	if *list {
		for server := range client.ListServers(*timeout) {
			log.Print(server)
		}
		return
	}

	if *connect != "" {
		out, err := client.RequestOutput(*connect, OutputType(*outType))
		if err != nil {
			log.Fatal(err)
		}

		dumpOutput(out)
		return
	}

	if *command != "" {
		if *send != "" {
			out, err := client.SendCommand(*command, OutputType(*outType), *send)
			if err != nil {
				log.Fatal(err)
			}

			dumpOutput(out)
		} else {
			in, out, err := client.RequestCommand(*command, OutputType(*outType))
			if err != nil {
				log.Fatal(err)
			}

			go sendInput(in)
			dumpOutput(out)
		}
		return
	}

	mainUsage(nil)
}
Example #2
0
func mainServer() {
	opts := goopt.NewMergedSet(genops, serops)
	args, err := opts.Parse(os.Args)
	if err != nil {
		mainUsage(err)
	} else if len(args) > 0 && args[0] != "--" {
		mainUsage(errors.New("Unparsed arguments: " + strings.Join(args, " ")))
	}

	if err := devNull.Close(); err != nil {
		log.Fatal(err)
	}

	if *asDaemon {
		log.Fatal("Golang does not support daemonization")
	}

	validateHandle(&inSrc, os.Stdin)
	validateHandle(&outDest, os.Stdout)
	validateHandle(&errDest, os.Stderr)

	proc, err := StartProcess(args[1], *description, *bus, args[2:]...)
	if err != nil {
		log.Fatal(err)
	}

	if inSrc != nil {
		if err := proc.ConnectInput(inSrc); err != nil {
			log.Panic(err)
		}
	}

	if outDest != nil {
		proc.ConnectOutput(outDest)
	}

	if errDest != nil {
		proc.ConnectError(errDest)
	}

	if err = proc.Start(); err != nil {
		log.Fatal(err)
	}

	if err = proc.Wait(); err != nil {
		log.Fatal(err)
	}
}