forked from joshuarose/aws-iot-device-sdk-go
/
main.go
84 lines (70 loc) · 1.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/patrickjmcd/go-version"
"github.com/spf13/cobra"
)
var (
thingName string
)
func init() {
openTunnelCmd.Flags().StringVarP(&thingName, "thing-name", "n", "", "thing name")
rootCmd.AddCommand(createCertsAndKeysCmd)
rootCmd.AddCommand(openTunnelCmd)
}
// createCertsAndKeysCmd is the command to create certs and keys
var createCertsAndKeysCmd = &cobra.Command{
Use: "create-cert-and-keys",
Short: "Create Certificate and Keys needed for device provisioning",
Long: `Create the certificate and keys needed for just-in-time device provisioning`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
outputPath := args[0]
ctx := context.Background()
err := prepProvisioningCertificateAndKey(ctx, outputPath)
if err != nil {
log.Fatal(err)
}
log.Printf("Certificate and Keys created in %s", outputPath)
},
}
var openTunnelCmd = &cobra.Command{
Use: "open-tunnel",
Short: "Open a tunnel to the AWS IoT Core",
Long: `Open a tunnel to the AWS IoT Core`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if len(thingName) == 0 {
log.Fatal("--thing-name is required")
}
err := createTunnel(thingName)
if err != nil {
log.Fatal(err)
}
},
}
var rootCmd = &cobra.Command{
Use: "aws-provision",
Short: "AWS Provisioning Tools",
Long: `Complete documentation is available at http://github.com/meshifyiot/aws-iot-core-register-api`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("you didn't specify a command")
},
}
// execute adds all child commands to the root command sets flags appropriately.
func execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func main() {
log.Printf(
"Starting the service...\ncommit: %s, build time: %s, release: %s",
version.Commit, version.BuildTime, version.Release,
)
execute()
}