Skip to content

Commit 2ce21c0

Browse files
authored
Merge pull request #19 from streammachineio/dev-build
Cleanup
2 parents 7e3c08d + 175f23b commit 2ce21c0

File tree

23 files changed

+800
-99
lines changed

23 files changed

+800
-99
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ protos
88
.env-prod
99
.default-login-token.json
1010
dist
11+
dstrm

Makefile

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
.PHONY: build
1+
.PHONY: build clean all
2+
.DEFAULT_GOAL := all
23

34
SHELL := /bin/bash
45

@@ -8,3 +9,19 @@ build:
89
zsh-completion:
910
/bin/zsh -c 'strm completion zsh > "$${fpath[1]}/_strm"'
1011

12+
# for a speedier build than with goreleaser
13+
source_files := $(shell find . -name "*.go")
14+
15+
targetVar := streammachine.io/strm/cmd.CommandName
16+
17+
target := dstrm
18+
19+
ldflags := -X '${targetVar}=${target}'
20+
21+
${target}: ${source_files} Makefile
22+
go build -ldflags="${ldflags}" -o $@
23+
24+
clean:
25+
rm -f ${target}
26+
27+
all: ${target}

auth/auth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"os"
1717
)
1818

19-
var CommandName string
19+
var RootCommandName string
2020
var TokenFile string
2121

2222
const (
@@ -36,9 +36,9 @@ func login(apiHost string, s *string, cmd *cobra.Command) {
3636
authClient := &Auth{Uri: apiHost}
3737
authClient.AuthenticateLogin(s, &password)
3838
_, billingId := authClient.GetToken(false)
39-
fmt.Println("billingId", billingId)
39+
fmt.Println("Billing id:", billingId)
4040
filename := authClient.StoreLogin()
41-
fmt.Println("saved login to", filename)
41+
fmt.Println("Saved login to:", filename)
4242
}
4343

4444
func askPassword() string {

auth/cmd.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ func LoginCmd() *cobra.Command {
2222
func PrintTokenCmd() *cobra.Command {
2323
cmd := &cobra.Command{
2424
Use: "access-token",
25-
Short: "print an access-token to stdout",
25+
Short: "Print your current access-token to stdout",
2626
Long: `Prints an access token that can be used in an http header.
27+
Note that this token might be expired, so a refresh may be required.
2728
Use token as follows:
2829
'Authorization: Bearer <token>'
2930
`,
@@ -38,7 +39,7 @@ func Refresh() *cobra.Command {
3839
cmd := &cobra.Command{
3940
Use: "refresh",
4041
Short: "Refresh an existing access-token",
41-
Long: `Not really necessary, the cli will auto-refresh.
42+
Long: `Not really necessary, the CLI will auto-refresh.
4243
`,
4344
Run: func(cmd *cobra.Command, args []string) {
4445
DoRefresh(apiHost(cmd))

auth/sts.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"github.com/mitchellh/go-homedir"
87
"github.com/spf13/cobra"
98
"io"
109
"io/ioutil"
@@ -16,6 +15,8 @@ import (
1615
"time"
1716
)
1817

18+
var ConfigPath string
19+
1920
// Auth is the entity that interacts with authorization endpoints.
2021
// Used both for user logins and events
2122
type Auth struct {
@@ -42,7 +43,7 @@ type token struct {
4243
func (authorizer *Auth) GetToken(quiet bool) (string, string) {
4344
if int64(authorizer.token.ExpiresAt)-30 < time.Now().Unix() {
4445
if !quiet {
45-
println("Refreshing sts token")
46+
println("Refreshing STS token")
4647
}
4748
authorizer.refresh()
4849
}
@@ -126,12 +127,10 @@ func (authorizer *Auth) StoreLogin() string {
126127

127128
func (authorizer *Auth) getSaveFilename() string {
128129
if TokenFile == "" {
129-
home, err := homedir.Dir()
130-
cobra.CheckErr(err)
131130
u, err := url.Parse(authorizer.Uri)
132131
cobra.CheckErr(err)
133132
filename := fmt.Sprintf("strm-creds-%s.json", u.Hostname())
134-
return path.Join(home, ".config", "stream-machine", filename)
133+
return path.Join(ConfigPath, filename)
135134
} else {
136135
return TokenFile
137136
}
@@ -141,7 +140,7 @@ func (authorizer *Auth) LoadLogin() {
141140
filename := authorizer.getSaveFilename()
142141
b, err := ioutil.ReadFile(filename)
143142
if err != nil {
144-
cobra.CheckErr(fmt.Sprintf("No login information found. Use: `%v auth login` first.", CommandName))
143+
cobra.CheckErr(fmt.Sprintf("No login information found. Use: `%v auth login` first.", RootCommandName))
145144
}
146145
err = json.Unmarshal(b, &authorizer.token)
147146
cobra.CheckErr(err)
@@ -150,6 +149,6 @@ func (authorizer *Auth) LoadLogin() {
150149
func (authorizer *Auth) printToken() {
151150
fmt.Println(authorizer.token.IdToken)
152151
// these go to stderr, so the token is easy to capture in a script
153-
println("Expires at", time.Unix(int64(authorizer.token.ExpiresAt), 0).String())
154-
println("Billing-id", authorizer.token.BillingId)
152+
println("Expires at:", time.Unix(int64(authorizer.token.ExpiresAt), 0).String())
153+
println("Billing id:", authorizer.token.BillingId)
155154
}

cmd/egress.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ import (
1111
var EgressCmd = &cobra.Command{
1212
Use: "egress",
1313
Short: "Read from egress",
14-
Run: func(cmd *cobra.Command, args []string) { egress.Run(cmd, &args[0]) },
14+
Run: func(cmd *cobra.Command, args []string) {
15+
egress.Run(cmd, &args[0])
16+
},
1517
Args: cobra.ExactArgs(1), // the stream name
1618
ValidArgsFunction: stream.ExistingNamesCompletion,
1719
}
1820

1921
func init() {
2022
flags := EgressCmd.Flags()
21-
flags.String(egress.UrlFlag, "wss://out.dev.strm.services/ws",
22-
"where to retrieve the events")
23-
flags.String(sims.ClientIdFlag, "", "client id to be used for sending data")
24-
flags.String(sims.ClientSecretFlag, "", "client secret to be used for sending data")
23+
flags.String(sims.ClientIdFlag, "", "Client id to be used for receiving data")
24+
flags.String(sims.ClientSecretFlag, "", "Client secret to be used for receiving data")
2525

2626
}

cmd/root.go

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package cmd
33
import (
44
"context"
55
"fmt"
6-
"github.com/mitchellh/go-homedir"
76
"github.com/spf13/cobra"
87
"github.com/spf13/pflag"
98
"google.golang.org/grpc"
9+
"os"
1010
"streammachine.io/strm/auth"
1111
"streammachine.io/strm/egress"
1212
"streammachine.io/strm/entity"
@@ -48,8 +48,10 @@ var RootCmd = &cobra.Command{
4848
// You can bind cobra and viper in a few locations,
4949
// but PersistencePreRunE on the root command works well
5050
r := initializeConfig(cmd)
51+
auth.ConfigPath = cfgPath
52+
utils.ConfigPath = cfgPath
5153

52-
auth.CommandName = CommandName
54+
auth.RootCommandName = CommandName
5355

5456
if cmd.Parent() != AuthCmd && cmd != CompletionCmd && cmd != VersionCmd && cmd.Name() != "help" {
5557
apiAuthUrl := utils.GetStringAndErr(cmd.Flags(), auth.ApiAuthUrlFlag)
@@ -125,26 +127,17 @@ func init() {
125127
// Here you will define your flags and configuration settings.
126128
// Cobra supports persistent flags, which, if defined here,
127129
// will be global for your application.
130+
// set the default configuration path
128131
var err error
132+
cfgPath, err = utils.ExpandTilde("~/.config/stream-machine")
133+
cobra.CheckErr(err)
129134

130-
RootCmd.PersistentFlags().StringVar(&cfgPath, "config-path", "",
131-
"config path (default is $HOME/.config/stream-machine/)")
132-
RootCmd.PersistentFlags().String(apiHostFlag, "apis.streammachine.io:443", "API host name")
133-
RootCmd.PersistentFlags().String(auth.EventAuthHostFlag, "auth.strm.services", "Security Token Service for events")
135+
RootCmd.PersistentFlags().String(apiHostFlag, "apis.streammachine.io:443", "API host and port")
136+
RootCmd.PersistentFlags().String(auth.EventAuthHostFlag, "https://auth.strm.services", "Security Token Service for events")
134137
RootCmd.PersistentFlags().String(auth.ApiAuthUrlFlag, "https://api.streammachine.io/v1", "Auth URL for user logins")
135138
RootCmd.PersistentFlags().StringVar(&auth.TokenFile, "token-file", "",
136-
"config file (default is $HOME/.config/stream-machine/strm-creds-<api-auth-host>.json)")
137-
bind := func(f string) {
138-
err := viper.BindPFlag(f, RootCmd.PersistentFlags().Lookup(f))
139-
if err != nil {
140-
return
141-
}
142-
}
143-
bind(apiHostFlag)
144-
bind(auth.EventAuthHostFlag)
145-
bind(auth.ApiAuthUrlFlag)
146-
bind(auth.TokenFile)
147-
cobra.CheckErr(err)
139+
"Token file that contains an access token (default is $HOME/.config/stream-machine/strm-creds-<api-auth-host>.json)")
140+
RootCmd.PersistentFlags().String(egress.UrlFlag, "wss://out.strm.services/ws", "Websocket to receive events from")
148141
setupVerbs()
149142
}
150143

@@ -155,14 +148,18 @@ func initializeConfig(cmd *cobra.Command) error {
155148
v.SetConfigName(defaultConfigFilename)
156149

157150
// Set as many paths as you like where viper should look for the
158-
// config file. We are only looking in the current working directory.
159-
home, err := homedir.Dir()
160-
cobra.CheckErr(err)
161-
if cfgPath != "" {
151+
// config file.
152+
153+
// if we set this environment variable, we work in a completely different configuration directory
154+
e := os.Getenv(envPrefix + "_CONFIG_PATH")
155+
if len(e) != 0 {
156+
p, err := utils.ExpandTilde(e)
157+
cobra.CheckErr(err)
158+
v.AddConfigPath(p)
159+
cfgPath = p
160+
} else {
162161
v.AddConfigPath(cfgPath)
163162
}
164-
v.AddConfigPath(".")
165-
v.AddConfigPath(fmt.Sprintf("%s/.config/stream-machine", home))
166163

167164
// Attempt to read the config file, gracefully ignoring errors
168165
// caused by a config file not being found. Return an error

egress/egress.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
"streammachine.io/strm/utils"
1313
)
1414

15-
const UrlFlag = "egress"
15+
const (
16+
UrlFlag = "egress"
17+
)
1618

1719
var BillingId string
1820

@@ -47,4 +49,4 @@ func Run(cmd *cobra.Command, streamName *string) {
4749
cobra.CheckErr(err)
4850
fmt.Println(string(message))
4951
}
50-
}
52+
}

entity/batch_exporter/cmd.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
func DeleteCmd() *cobra.Command {
1818
batchExporter := &cobra.Command{
1919
Use: "batch-exporter [name]",
20-
Short: "delete batch-exporter by name",
20+
Short: "Delete a Batch exporter by name",
2121
Run: func(cmd *cobra.Command, args []string) {
2222
del(&args[0])
2323
},
@@ -31,7 +31,7 @@ func DeleteCmd() *cobra.Command {
3131
func GetCmd() *cobra.Command {
3232
return &cobra.Command{
3333
Use: "batch-exporter [name]",
34-
Short: "get batch-exporter by name",
34+
Short: "Get Batch exporter by name",
3535
Run: func(cmd *cobra.Command, args []string) {
3636
get(&args[0], cmd)
3737
},
@@ -42,7 +42,7 @@ func GetCmd() *cobra.Command {
4242
func ListCmd() *cobra.Command {
4343
return &cobra.Command{
4444
Use: "batch-exporters",
45-
Short: "List batch-exporters",
45+
Short: "List Batch exporters",
4646
Run: func(cmd *cobra.Command, args []string) {
4747
list()
4848
},

entity/event_contract/cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "github.com/spf13/cobra"
55
func GetCmd() *cobra.Command {
66
return &cobra.Command{
77
Use: "event-contract [name]",
8-
Short: "get event-contract by name",
8+
Short: "Get Event Contract by name",
99
Run: func(cmd *cobra.Command, args []string) {
1010
get(&args[0])
1111
},
@@ -16,7 +16,7 @@ func GetCmd() *cobra.Command {
1616
func ListCmd() *cobra.Command {
1717
return &cobra.Command{
1818
Use: "event-contracts",
19-
Short: "List event-contracts",
19+
Short: "List Event Contracts",
2020
Run: func(cmd *cobra.Command, args []string) {
2121
list()
2222
},

entity/kafka_cluster/cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "github.com/spf13/cobra"
55
func GetCmd() *cobra.Command {
66
return &cobra.Command{
77
Use: "kafka-cluster [name]",
8-
Short: "get kafka-cluster by name",
8+
Short: "Get Kafka cluster by name",
99
Run: func(cmd *cobra.Command, args []string) {
1010
get(&args[0])
1111
},
@@ -16,7 +16,7 @@ func GetCmd() *cobra.Command {
1616
func ListCmd() *cobra.Command {
1717
return &cobra.Command{
1818
Use: "kafka-clusters",
19-
Short: "List kafka-clusters",
19+
Short: "List Kafka clusters",
2020
Run: func(cmd *cobra.Command, args []string) {
2121
list()
2222
},

entity/kafka_exporter/cmd.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ const (
1414
func DeleteCmd() *cobra.Command {
1515
return &cobra.Command{
1616
Use: "kafka-exporter [name]",
17-
Short: "Delete a kafka-exporter",
18-
Long: `Delete a kafka-exporter.
19-
If a kafka-exporter has dependents (like kafka-users), you can use
17+
Short: "Delete a Kafka exporter",
18+
Long: `Delete a Kafka exporter.
19+
If a kafka-exporter has dependents (like Kafka users), you can use
2020
the 'recursive' option to get rid of those also.
2121
Returns everything that was deleted. `,
2222
Run: func(cmd *cobra.Command, args []string) {
@@ -31,7 +31,7 @@ func DeleteCmd() *cobra.Command {
3131
func GetCmd() *cobra.Command {
3232
return &cobra.Command{
3333
Use: "kafka-exporter [name]",
34-
Short: "get kafka-exporter by name",
34+
Short: "Get Kafka exporter by name",
3535
Run: func(cmd *cobra.Command, args []string) {
3636
recursive, _ := cmd.Flags().GetBool("recursive")
3737
get(&args[0], recursive)
@@ -44,7 +44,7 @@ func GetCmd() *cobra.Command {
4444
func ListCmd() *cobra.Command {
4545
return &cobra.Command{
4646
Use: "kafka-exporters",
47-
Short: "List kafka-exporters",
47+
Short: "List Kafka exporters",
4848
Run: func(cmd *cobra.Command, args []string) {
4949
flag, _ := cmd.Root().PersistentFlags().GetBool("recursive")
5050
list(flag)
@@ -56,7 +56,7 @@ func CreateCmd() *cobra.Command {
5656

5757
kafkaExporter := &cobra.Command{
5858
Use: "kafka-exporter [stream-name]",
59-
Short: "create a kafka-exporter",
59+
Short: "Create a Kafka exporter",
6060
Run: func(cmd *cobra.Command, args []string) {
6161
streamName := &args[0]
6262
create(streamName, cmd)

entity/kafka_user/cmd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const (
1313
func DeleteCmd() *cobra.Command {
1414
return &cobra.Command{
1515
Use: "kafka-user [name]",
16-
Short: "Delete a kafka-user",
16+
Short: "Delete a Kafka user",
1717
Run: func(cmd *cobra.Command, args []string) {
1818
del(&args[0])
1919
},
@@ -25,7 +25,7 @@ func DeleteCmd() *cobra.Command {
2525
func GetCmd() *cobra.Command {
2626
return &cobra.Command{
2727
Use: "kafka-user [name]",
28-
Short: "get a kafka-user",
28+
Short: "Get Kafka user",
2929
Run: func(cmd *cobra.Command, args []string) {
3030
get(&args[0])
3131
},
@@ -37,7 +37,7 @@ func GetCmd() *cobra.Command {
3737
func ListCmd() *cobra.Command {
3838
return &cobra.Command{
3939
Use: "kafka-users [kafka-exporter-name]",
40-
Short: "List kafka-users",
40+
Short: "List Kafka users",
4141
Run: func(cmd *cobra.Command, args []string) {
4242
list(&args[0])
4343
},
@@ -49,7 +49,7 @@ func ListCmd() *cobra.Command {
4949
func CreateCmd() *cobra.Command {
5050
kafkaUser := &cobra.Command{
5151
Use: "kafka-user [exporter-name]",
52-
Short: "create a kafka-user on a named kafka-exporter",
52+
Short: "Create a Kafka user on a Kafka exporter",
5353
Run: func(cmd *cobra.Command, args []string) {
5454
streamName := &args[0]
5555
create(streamName, cmd)

0 commit comments

Comments
 (0)