Skip to content

Commit 80f3af6

Browse files
committed
[misc]: Factorize SetFlagsFromEnvVars and add support for CREDENTIALS_DIRECTORY
The code for setting up command flags values from environment variables was copied in three different places, this puts the helper function in the util package so that every component can use it. Furthermore, it now supports reading values from files in `$CREDENTIALS_DIRECTORY`, which allows direct integration with systemd's `LoadCredential` mechanism (but any platform can define such a variable and pass values as files).
1 parent abd152e commit 80f3af6

File tree

16 files changed

+77
-121
lines changed

16 files changed

+77
-121
lines changed

client/cmd/down.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var downCmd = &cobra.Command{
1616
Use: "down",
1717
Short: "down netbird connections",
1818
RunE: func(cmd *cobra.Command, args []string) error {
19-
SetFlagsFromEnvVars(rootCmd)
19+
util.SetFlagsFromEnvVars(rootCmd)
2020

2121
cmd.SetOut(cmd.OutOrStdout())
2222

client/cmd/login.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ func isUnixRunningDesktop() bool {
371371
}
372372

373373
func setEnvAndFlags(cmd *cobra.Command) error {
374-
SetFlagsFromEnvVars(rootCmd)
374+
util.SetFlagsFromEnvVars(rootCmd)
375375

376376
cmd.SetOut(cmd.OutOrStdout())
377377

client/cmd/logout.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
"github.com/spf13/cobra"
1010

1111
"github.com/netbirdio/netbird/client/proto"
12+
"github.com/netbirdio/netbird/util"
1213
)
1314

1415
var logoutCmd = &cobra.Command{
1516
Use: "logout",
1617
Short: "logout from the Netbird Management Service and delete peer",
1718
RunE: func(cmd *cobra.Command, args []string) error {
18-
SetFlagsFromEnvVars(rootCmd)
19+
util.SetFlagsFromEnvVars(rootCmd)
1920

2021
cmd.SetOut(cmd.OutOrStdout())
2122

client/cmd/profile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ var profileSelectCmd = &cobra.Command{
5353
}
5454

5555
func setupCmd(cmd *cobra.Command) error {
56-
SetFlagsFromEnvVars(rootCmd)
57-
SetFlagsFromEnvVars(cmd)
56+
util.SetFlagsFromEnvVars(rootCmd)
57+
util.SetFlagsFromEnvVars(cmd)
5858

5959
cmd.SetOut(cmd.OutOrStdout())
6060

client/cmd/root.go

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import (
1818
"github.com/cenkalti/backoff/v4"
1919
log "github.com/sirupsen/logrus"
2020
"github.com/spf13/cobra"
21-
"github.com/spf13/pflag"
2221
"google.golang.org/grpc"
2322
"google.golang.org/grpc/credentials/insecure"
2423

2524
"github.com/netbirdio/netbird/client/internal/profilemanager"
25+
"github.com/netbirdio/netbird/util"
2626
)
2727

2828
const (
@@ -194,38 +194,6 @@ func SetupCloseHandler(ctx context.Context, cancel context.CancelFunc) {
194194
}()
195195
}
196196

197-
// SetFlagsFromEnvVars reads and updates flag values from environment variables with prefix WT_
198-
func SetFlagsFromEnvVars(cmd *cobra.Command) {
199-
flags := cmd.PersistentFlags()
200-
flags.VisitAll(func(f *pflag.Flag) {
201-
oldEnvVar := FlagNameToEnvVar(f.Name, "WT_")
202-
203-
if value, present := os.LookupEnv(oldEnvVar); present {
204-
err := flags.Set(f.Name, value)
205-
if err != nil {
206-
log.Infof("unable to configure flag %s using variable %s, err: %v", f.Name, oldEnvVar, err)
207-
}
208-
}
209-
210-
newEnvVar := FlagNameToEnvVar(f.Name, "NB_")
211-
212-
if value, present := os.LookupEnv(newEnvVar); present {
213-
err := flags.Set(f.Name, value)
214-
if err != nil {
215-
log.Infof("unable to configure flag %s using variable %s, err: %v", f.Name, newEnvVar, err)
216-
}
217-
}
218-
})
219-
}
220-
221-
// FlagNameToEnvVar converts flag name to environment var name adding a prefix,
222-
// replacing dashes and making all uppercase (e.g. setup-keys is converted to NB_SETUP_KEYS according to the input prefix)
223-
func FlagNameToEnvVar(cmdFlag string, prefix string) string {
224-
parsed := strings.ReplaceAll(cmdFlag, "-", "_")
225-
upper := strings.ToUpper(parsed)
226-
return prefix + upper
227-
}
228-
229197
// DialClientGRPCServer returns client connection to the daemon server.
230198
func DialClientGRPCServer(ctx context.Context, addr string) (*grpc.ClientConn, error) {
231199
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
@@ -382,7 +350,7 @@ func migrateToNetbird(oldPath, newPath string) bool {
382350
}
383351

384352
func getClient(cmd *cobra.Command) (*grpc.ClientConn, error) {
385-
SetFlagsFromEnvVars(rootCmd)
353+
util.SetFlagsFromEnvVars(rootCmd)
386354
cmd.SetOut(cmd.OutOrStdout())
387355

388356
conn, err := DialClientGRPCServer(cmd.Context(), daemonAddr)

client/cmd/root_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/spf13/cobra"
99

1010
"github.com/netbirdio/netbird/client/iface"
11+
"github.com/netbirdio/netbird/util"
1112
)
1213

1314
func TestInitCommands(t *testing.T) {
@@ -45,7 +46,7 @@ func TestSetFlagsFromEnvVars(t *testing.T) {
4546
Long: "test",
4647
SilenceUsage: true,
4748
Run: func(cmd *cobra.Command, args []string) {
48-
SetFlagsFromEnvVars(cmd)
49+
util.SetFlagsFromEnvVars(cmd)
4950
},
5051
}
5152

client/cmd/service_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ func (p *program) Stop(srv service.Service) error {
103103

104104
// Common setup for service control commands
105105
func setupServiceControlCommand(cmd *cobra.Command, ctx context.Context, cancel context.CancelFunc) (service.Service, error) {
106-
SetFlagsFromEnvVars(rootCmd)
107-
SetFlagsFromEnvVars(serviceCmd)
106+
util.SetFlagsFromEnvVars(rootCmd)
107+
util.SetFlagsFromEnvVars(serviceCmd)
108108

109109
cmd.SetOut(cmd.OutOrStdout())
110110

client/cmd/service_installer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ var ErrGetServiceStatus = fmt.Errorf("failed to get service status")
2020

2121
// Common service command setup
2222
func setupServiceCommand(cmd *cobra.Command) error {
23-
SetFlagsFromEnvVars(rootCmd)
24-
SetFlagsFromEnvVars(serviceCmd)
23+
util.SetFlagsFromEnvVars(rootCmd)
24+
util.SetFlagsFromEnvVars(serviceCmd)
2525
cmd.SetOut(cmd.OutOrStdout())
2626
return handleRebrand(cmd)
2727
}

client/cmd/ssh.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ var sshCmd = &cobra.Command{
4242
},
4343
Short: "connect to a remote SSH server",
4444
RunE: func(cmd *cobra.Command, args []string) error {
45-
SetFlagsFromEnvVars(rootCmd)
46-
SetFlagsFromEnvVars(cmd)
45+
util.SetFlagsFromEnvVars(rootCmd)
46+
util.SetFlagsFromEnvVars(cmd)
4747

4848
cmd.SetOut(cmd.OutOrStdout())
4949

client/cmd/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func init() {
5151
}
5252

5353
func statusFunc(cmd *cobra.Command, args []string) error {
54-
SetFlagsFromEnvVars(rootCmd)
54+
util.SetFlagsFromEnvVars(rootCmd)
5555

5656
cmd.SetOut(cmd.OutOrStdout())
5757

0 commit comments

Comments
 (0)