blob: 688bff41a035d838f576e59ee15a3a8aa95529a4 [file] [log] [blame]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// slcli device configuration.
package main
import (
"fmt"
"v.io/v23/context"
"v.io/x/lib/cmdline"
"v.io/x/ref/lib/v23cmd"
"v.io/x/sensorlog/internal/client"
"v.io/x/sensorlog/internal/sbmodel"
"v.io/x/sensorlog/internal/sbutil"
)
var cmdSLDevice = &cmdline.Command{
Name: "device",
Short: "Manage measuring devices",
Long: `
Add measuring devices.
TODO(ivanpi): List.
`,
Children: []*cmdline.Command{cmdSLDeviceAdd /*, cmdSLDeviceList */},
}
var cmdSLDeviceAdd = &cmdline.Command{
Runner: v23cmd.RunnerFunc(runSLDeviceAdd),
Name: "add",
Short: "Add new measuring device",
Long: `
Adds a new measuring device and outputs its identifier.
`,
ArgsName: "<publish_sb> <device_id> [<device_desc>]",
ArgsLong: `
<publish_sb> is the rooted name of the Syncbase instance where the device
syncgroup is published.
<device_id> is the identifier of the device to add.
<device_desc> is a human-readable description of the device.
It doesn't need to be unique.
`,
}
func runSLDeviceAdd(ctx *context.T, env *cmdline.Env, args []string) error {
if len(args) < 2 || len(args) > 3 {
return env.UsageErrorf("expects between 2 and 3 arguments")
}
sgPublishSb := args[0]
devId := args[1]
desc := ""
if len(args) > 2 {
desc = args[2]
}
db, err := sbutil.CreateOrOpenDB(ctx, *flagSbService, sbmodel.MasterCollections)
if err != nil {
return fmt.Errorf("failed opening Syncbase db: %v", err)
}
devKey, err := client.AddDevice(ctx, db, devId, sgPublishSb, desc)
if err != nil {
return err
}
fmt.Fprintf(env.Stdout, "%s\n", devKey.DevId)
return nil
}