-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.go
More file actions
107 lines (92 loc) · 2.61 KB
/
service.go
File metadata and controls
107 lines (92 loc) · 2.61 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"fmt"
"os"
"text/tabwriter"
"github.com/spf13/cobra"
)
func serviceLs(cmd *cobra.Command, args []string) {
services, err := host.ServiceList()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to fetch services:", err)
os.Exit(1)
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, columnPadding, ' ', 0)
fmt.Fprintln(w, "NAME\tDESCRIPTION\tID\tOWNER NAME\tOWNER EMAIL\t")
for _, s := range services {
fmt.Fprintf(w, "%s\t", s.Name)
fmt.Fprintf(w, "%s\t", s.Description)
fmt.Fprintf(w, "%s\t", s.ID)
fmt.Fprintf(w, "%s\t", s.Owner.Name)
fmt.Fprintf(w, "%s\t", s.Owner.Email)
fmt.Fprintf(w, "\n")
}
w.Flush()
}
func serviceCreate(cmd *cobra.Command, args []string) {
name := args[0]
description := args[1]
s, err := host.ServiceCreate(name, description, nil, nil)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to create service:", err)
os.Exit(1)
}
fmt.Println(s.ID)
}
func serviceRm(cmd *cobra.Command, args []string) {
serviceID := args[0]
err := host.ServiceDelete(serviceID)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to delete service:", err)
os.Exit(1)
}
}
func serviceMonitor(cmd *cobra.Command, args []string) {
serviceID := args[0]
service, err := host.ServiceGet(serviceID)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to fetch service information:", err)
os.Exit(1)
}
monitor(service.Pubsub.Topic + "/#")
}
func serviceTokenGenerate(cmd *cobra.Command, args []string) {
serviceID := args[0]
token, err := host.ServiceTokenGenerate(serviceID)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to generate token:", err)
os.Exit(1)
}
if envTrue, _ := cmd.Flags().GetBool("env"); envTrue {
fmt.Printf("FRAMEWORK_SERVER=\"%s\"\n", frameworkHost)
fmt.Printf("MQTT_SERVER=\"%s\"\n", mqttServer)
fmt.Printf("SERVICE_ID=\"%s\"\n", serviceID)
fmt.Printf("SERVICE_TOKEN=\"%s\"\n", token)
} else {
fmt.Println(token)
}
}
func serviceTokenRegenerate(cmd *cobra.Command, args []string) {
serviceID := args[0]
token, err := host.ServiceTokenRegenerate(serviceID)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to regenerate token:", err)
os.Exit(1)
}
if envTrue, _ := cmd.Flags().GetBool("env"); envTrue {
fmt.Printf("FRAMEWORK_SERVER=\"%s\"\n", frameworkHost)
fmt.Printf("MQTT_SERVER=\"%s\"\n", mqttServer)
fmt.Printf("SERVICE_ID=\"%s\"\n", serviceID)
fmt.Printf("SERVICE_TOKEN=\"%s\"\n", token)
} else {
fmt.Println(token)
}
}
func serviceTokenRm(cmd *cobra.Command, args []string) {
serviceID := args[0]
err := host.ServiceTokenDelete(serviceID)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to delete token:", err)
os.Exit(1)
}
}