Exemplo n.º 1
0
// newCommandPub creates and returns a pub command.
func newCommandPub(args []string, cli *client.Client) (command, error) {
	// Create a flag set.
	var flg flag.FlagSet

	// Define the flags.
	qos := flg.Uint("q", uint(mqtt.QoS0), "QoS")
	retain := flg.Bool("r", false, "Retain")
	topicName := flg.String("t", "", "Topic Name")
	message := flg.String("m", "", "Application Message")

	// Parse the flag.
	if err := flg.Parse(args); err != nil {
		return nil, errCmdArgsParse
	}

	// Create a pub command.
	cmd := &commandPub{
		cli: cli,
		publishOpts: &client.PublishOptions{
			QoS:       byte(*qos),
			Retain:    *retain,
			TopicName: []byte(*topicName),
			Message:   []byte(*message),
		},
	}

	// Return the command.
	return cmd, nil
}
Exemplo n.º 2
0
// ApplyWithError populates the flag given the flag set and environment
func (f UintFlag) ApplyWithError(set *flag.FlagSet) error {
	if f.EnvVar != "" {
		for _, envVar := range strings.Split(f.EnvVar, ",") {
			envVar = strings.TrimSpace(envVar)
			if envVal, ok := syscall.Getenv(envVar); ok {
				envValInt, err := strconv.ParseUint(envVal, 0, 64)
				if err != nil {
					return fmt.Errorf("could not parse %s as uint value for flag %s: %s", envVal, f.Name, err)
				}

				f.Value = uint(envValInt)
				break
			}
		}
	}

	eachName(f.Name, func(name string) {
		if f.Destination != nil {
			set.UintVar(f.Destination, name, f.Value, f.Usage)
			return
		}
		set.Uint(name, f.Value, f.Usage)
	})

	return nil
}
Exemplo n.º 3
0
// newCommandSub creates and returns a sub command.
func newCommandSub(args []string, cli *client.Client) (command, error) {
	// Create a flag set.
	var flg flag.FlagSet

	// Define the flags.
	topicFilter := flg.String("t", "", "Topic Filter")
	qos := flg.Uint("q", uint(mqtt.QoS0), "QoS")

	// Parse the flag.
	if err := flg.Parse(args); err != nil {
		return nil, errCmdArgsParse
	}

	// Create a sub command.
	cmd := &commandSub{
		cli: cli,
		subscribeOpts: &client.SubscribeOptions{
			SubReqs: []*client.SubReq{
				&client.SubReq{
					TopicFilter: []byte(*topicFilter),
					QoS:         byte(*qos),
					Handler:     messageHandler,
				},
			},
		},
	}

	// Return the command.
	return cmd, nil
}
Exemplo n.º 4
0
Arquivo: parser.go Projeto: qiniu/dyn
func parseFlagArg(fv reflect.Value, f *flag.FlagSet, tag string) (err error) {

	n := 0
	for n < len(tag) {
		if tag[n] == ',' || tag[n] == ' ' {
			break
		}
		n++
	}

	name := tag[:n]
	usage := ""
	pos := strings.Index(tag[n:], " - ")
	if pos >= 0 {
		usage = tag[pos+3:]
	}

	switch fv.Kind() {
	case reflect.Ptr:
		switch fv.Elem().Kind() {
		case reflect.Bool:
			fv.Set(reflect.ValueOf(f.Bool(name, false, usage)))
		case reflect.Int:
			fv.Set(reflect.ValueOf(f.Int(name, 0, usage)))
		case reflect.Uint:
			fv.Set(reflect.ValueOf(f.Uint(name, 0, usage)))
		case reflect.Uint64:
			fv.Set(reflect.ValueOf(f.Uint64(name, 0, usage)))
		default:
			return ErrUnsupportedFlagType
		}
	case reflect.Bool:
		f.BoolVar(fv.Addr().Interface().(*bool), name, false, usage)
	case reflect.Int:
		f.IntVar(fv.Addr().Interface().(*int), name, 0, usage)
	case reflect.Uint:
		f.UintVar(fv.Addr().Interface().(*uint), name, 0, usage)
	case reflect.Uint64:
		f.Uint64Var(fv.Addr().Interface().(*uint64), name, 0, usage)
	default:
		return ErrUnsupportedFlagType
	}
	return nil
}
Exemplo n.º 5
0
Arquivo: flag.go Projeto: nlf/dlite
// Apply populates the flag given the flag set and environment
func (f UintFlag) Apply(set *flag.FlagSet) {
	if f.EnvVar != "" {
		for _, envVar := range strings.Split(f.EnvVar, ",") {
			envVar = strings.TrimSpace(envVar)
			if envVal := os.Getenv(envVar); envVal != "" {
				envValInt, err := strconv.ParseUint(envVal, 0, 64)
				if err == nil {
					f.Value = uint(envValInt)
					break
				}
			}
		}
	}

	eachName(f.Name, func(name string) {
		if f.Destination != nil {
			set.UintVar(f.Destination, name, f.Value, f.Usage)
			return
		}
		set.Uint(name, f.Value, f.Usage)
	})
}
Exemplo n.º 6
0
// newCommandConn creates and returns a conn command.
func newCommandConn(args []string, cli *client.Client) (command, error) {
	// Create a flag set.
	var flg flag.FlagSet

	// Define the flags.
	network := flg.String("n", defaultNetwork, "network on which the Client connects to the Server")
	host := flg.String("h", defaultHost, "host name of the Server which the Client connects to")
	port := flg.Uint("p", defaultPort, "port number of the Server which the Client connects to")
	crtPath := flg.String("crt", "", "the path of the certificate authority file to verify the server connection")
	connackTimeout := flg.Uint(
		"ct",
		defaultCONNACKTimeout,
		"Timeout in seconds for the Client to wait for receiving the CONNACK Packet after sending the CONNECT Packet",
	)
	pingrespTimeout := flg.Uint(
		"pt",
		defaultPINGRESPTimeout,
		"Timeout in seconds for the Client to wait for receiving the PINGRESP Packet after sending the PINGREQ Packet",
	)
	clientID := flg.String("i", "", "Client identifier for the Client")
	userName := flg.String("u", "", "User Name")
	password := flg.String("P", "", "Password")
	cleanSession := flg.Bool("c", defaultCleanSession, "Clean Session")
	keepAlive := flg.Uint("k", defaultKeepAlive, "Keep Alive measured in seconds")
	willTopic := flg.String("wt", "", "Will Topic")
	willMessage := flg.String("wm", "", "Will Message")
	willQoS := flg.Uint("wq", uint(mqtt.QoS0), "Will QoS")
	willRetain := flg.Bool("wr", false, "Will Retain")

	// Parse the flag.
	if err := flg.Parse(args); err != nil {
		return nil, errCmdArgsParse
	}

	var tlsConfig *tls.Config

	// Parse the certificate authority file.
	if *crtPath != "" {
		b, err := ioutil.ReadFile(*crtPath)
		if err != nil {
			return nil, err
		}

		roots := x509.NewCertPool()
		if ok := roots.AppendCertsFromPEM(b); !ok {
			return nil, errParseCrtFailure
		}

		tlsConfig = &tls.Config{
			RootCAs: roots,
		}
	}

	// Create a conn command.
	cmd := &commandConn{
		cli: cli,
		connectOpts: &client.ConnectOptions{
			Network:         *network,
			Address:         *host + ":" + strconv.Itoa(int(*port)),
			TLSConfig:       tlsConfig,
			CONNACKTimeout:  time.Duration(*connackTimeout),
			PINGRESPTimeout: time.Duration(*pingrespTimeout),
			ClientID:        []byte(*clientID),
			UserName:        []byte(*userName),
			Password:        []byte(*password),
			CleanSession:    *cleanSession,
			KeepAlive:       uint16(*keepAlive),
			WillTopic:       []byte(*willTopic),
			WillMessage:     []byte(*willMessage),
			WillQoS:         byte(*willQoS),
			WillRetain:      *willRetain,
		},
	}

	// Return the command.
	return cmd, nil
}